Support adding document meta via Context

This commit is contained in:
Toby Zerner 2021-05-05 14:16:17 +09:30
parent 8029c6fd4a
commit cda345d3ad
1 changed files with 8 additions and 2 deletions

View File

@ -26,6 +26,7 @@ use Tobyz\JsonApiServer\Schema\Attribute;
use Tobyz\JsonApiServer\Context; use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Schema\HasMany; use Tobyz\JsonApiServer\Schema\HasMany;
use Tobyz\JsonApiServer\Schema\HasOne; use Tobyz\JsonApiServer\Schema\HasOne;
use Tobyz\JsonApiServer\Schema\Meta;
use Tobyz\JsonApiServer\Serializer; use Tobyz\JsonApiServer\Serializer;
use function Tobyz\JsonApiServer\evaluate; use function Tobyz\JsonApiServer\evaluate;
use function Tobyz\JsonApiServer\json_api_response; use function Tobyz\JsonApiServer\json_api_response;
@ -79,7 +80,11 @@ class Index
foreach ($models as $model) { foreach ($models as $model) {
$serializer->add($this->resource, $model, $include); $serializer->add($this->resource, $model, $include);
} }
$meta = array_values(array_map(function (Meta $meta) use ($context) {
return new Structure\Meta($meta->getName(), $meta->getValue()($context));
}, $context->getMeta()));
return json_api_response( return json_api_response(
new Structure\CompoundDocument( new Structure\CompoundDocument(
new Structure\PaginatedCollection( new Structure\PaginatedCollection(
@ -90,7 +95,8 @@ class Index
new Structure\Link\SelfLink($this->buildUrl($context->getRequest())), new Structure\Link\SelfLink($this->buildUrl($context->getRequest())),
new Structure\Meta('offset', $offset), new Structure\Meta('offset', $offset),
new Structure\Meta('limit', $limit), new Structure\Meta('limit', $limit),
...($total !== null ? [new Structure\Meta('total', $total)] : []) ...($total !== null ? [new Structure\Meta('total', $total)] : []),
...$meta
) )
); );
} }