diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-03-21 09:08:37 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-03-21 09:08:37 +0000 |
| commit | 55dc29ed2a2beafb0ae0facfca5869e75715ac76 (patch) | |
| tree | 25064efa22513ef00d365d125243f45e07c5e694 /lib/classes | |
| parent | f8574aa964b803a65c2c9446ac85a67011bfb0e5 (diff) | |
enable attributes in Schema::getAttributes() to be defined as a callable for...
Closes #2406
Merge request studip/studip!1601
Diffstat (limited to 'lib/classes')
| -rw-r--r-- | lib/classes/JsonApi/JsonApiIntegration/Factory.php | 9 | ||||
| -rw-r--r-- | lib/classes/JsonApi/JsonApiIntegration/FieldsetFilter.php | 45 |
2 files changed, 54 insertions, 0 deletions
diff --git a/lib/classes/JsonApi/JsonApiIntegration/Factory.php b/lib/classes/JsonApi/JsonApiIntegration/Factory.php index afdd431..8cdc6e6 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; @@ -25,6 +26,14 @@ class Factory extends NeomerxFactory /** * @inheritdoc */ + public function createFieldSetFilter(array $fieldSets): FieldSetFilterInterface + { + return new FieldsetFilter($fieldSets); + } + + /** + * @inheritdoc + */ public function createParser( SchemaContainerInterface $container, EditableContextInterface $context diff --git a/lib/classes/JsonApi/JsonApiIntegration/FieldsetFilter.php b/lib/classes/JsonApi/JsonApiIntegration/FieldsetFilter.php new file mode 100644 index 0000000..42a5842 --- /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; + } +} |
