diff --git a/lib/classes/JsonApi/JsonApiIntegration/Factory.php b/lib/classes/JsonApi/JsonApiIntegration/Factory.php index afdd4317a162ffbca9da37cdd84fad0527d9475a..8cdc6e625800c07318032cd36b9fbed00377c726 100644 --- a/lib/classes/JsonApi/JsonApiIntegration/Factory.php +++ b/lib/classes/JsonApi/JsonApiIntegration/Factory.php @@ -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 */ diff --git a/lib/classes/JsonApi/JsonApiIntegration/FieldsetFilter.php b/lib/classes/JsonApi/JsonApiIntegration/FieldsetFilter.php new file mode 100644 index 0000000000000000000000000000000000000000..42a5842edbe5eb11d5a7d98d259907957b6ec87f --- /dev/null +++ b/lib/classes/JsonApi/JsonApiIntegration/FieldsetFilter.php @@ -0,0 +1,45 @@ +<?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; + } +}