Skip to content

Commit cda345d

Browse files
committed
Support adding document meta via Context
1 parent 8029c6f commit cda345d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/Endpoint/Index.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Tobyz\JsonApiServer\Context;
2727
use Tobyz\JsonApiServer\Schema\HasMany;
2828
use Tobyz\JsonApiServer\Schema\HasOne;
29+
use Tobyz\JsonApiServer\Schema\Meta;
2930
use Tobyz\JsonApiServer\Serializer;
3031
use function Tobyz\JsonApiServer\evaluate;
3132
use function Tobyz\JsonApiServer\json_api_response;
@@ -79,7 +80,11 @@ public function handle(Context $context): ResponseInterface
7980
foreach ($models as $model) {
8081
$serializer->add($this->resource, $model, $include);
8182
}
82-
83+
84+
$meta = array_values(array_map(function (Meta $meta) use ($context) {
85+
return new Structure\Meta($meta->getName(), $meta->getValue()($context));
86+
}, $context->getMeta()));
87+
8388
return json_api_response(
8489
new Structure\CompoundDocument(
8590
new Structure\PaginatedCollection(
@@ -90,7 +95,8 @@ public function handle(Context $context): ResponseInterface
9095
new Structure\Link\SelfLink($this->buildUrl($context->getRequest())),
9196
new Structure\Meta('offset', $offset),
9297
new Structure\Meta('limit', $limit),
93-
...($total !== null ? [new Structure\Meta('total', $total)] : [])
98+
...($total !== null ? [new Structure\Meta('total', $total)] : []),
99+
...$meta
94100
)
95101
);
96102
}

0 commit comments

Comments
 (0)