Skip to content
Snippets Groups Projects
Commit 55dc29ed authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

enable attributes in Schema::getAttributes() to be defined as a callable for...

Closes #2406

Merge request studip/studip!1601
parent f8574aa9
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ namespace JsonApi\JsonApiIntegration;
use Neomerx\JsonApi\Contracts\Parser\EditableContextInterface;
use Neomerx\JsonApi\Contracts\Parser\ParserInterface;
use Neomerx\JsonApi\Contracts\Representation\FieldSetFilterInterface;
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
use Neomerx\JsonApi\Factories\Factory as NeomerxFactory;
......@@ -22,6 +23,14 @@ use Neomerx\JsonApi\Schema\Link;
*/
class Factory extends NeomerxFactory
{
/**
* @inheritdoc
*/
public function createFieldSetFilter(array $fieldSets): FieldSetFilterInterface
{
return new FieldsetFilter($fieldSets);
}
/**
* @inheritdoc
*/
......
<?php
namespace JsonApi\JsonApiIntegration;
class FieldsetFilter extends \Neomerx\JsonApi\Representation\FieldSetFilter
{
/**
* @param string $type
* @param iterable $fields
*
* @return iterable
*/
protected function filterFields(string $type, iterable $fields): iterable
{
if ($this->hasFilter($type) === false) {
foreach ($fields as $name => $value) {
yield $name => $this->resolveValue($value);
}
return;
}
$allowedFields = $this->getAllowedFields($type);
foreach ($fields as $name => $value) {
if (isset($allowedFields[$name]) === true) {
yield $name => $this->resolveValue($value);
}
}
}
/**
* Resolves a given by either calling it if it's a callable. Otherwise
* just return the value itself.
*
* @param mixed $value
* @return mixed
*/
private function resolveValue($value)
{
if (is_callable($value)) {
return $value();
}
return $value;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment