From 74a6edf3000d4820b9323d00afc89601ced1eeeb Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 17 Jan 2021 11:29:13 +1030 Subject: [PATCH] Polyfill old behavior --- src/Context.php | 16 ++++++++++++++++ src/Handler/Index.php | 12 +++++++++++- src/Schema/Type.php | 11 +++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/Context.php diff --git a/src/Context.php b/src/Context.php new file mode 100644 index 0000000..dcac9ed --- /dev/null +++ b/src/Context.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tobyz\JsonApiServer; + +class Context +{ + public $query; +} diff --git a/src/Handler/Index.php b/src/Handler/Index.php index b0302db..4e71004 100644 --- a/src/Handler/Index.php +++ b/src/Handler/Index.php @@ -19,6 +19,7 @@ use JsonApiPhp\JsonApi\Link\PrevLink; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\RequestHandlerInterface; +use Tobyz\JsonApiServer\Context; use Tobyz\JsonApiServer\Exception\BadRequestException; use Tobyz\JsonApiServer\JsonApi; use Tobyz\JsonApiServer\JsonApiResponse; @@ -75,6 +76,14 @@ class Index implements RequestHandlerInterface foreach ($models as $model) { $serializer->add($this->resource, $model, $include); } + + $context = new Context; + $context->query = $query; + $documentMeta = []; + + foreach ($schema->getDocumentMeta() as $key => $value) { + $documentMeta[] = new Structure\Meta($key, $value($context)); + } return new JsonApiResponse( new Structure\CompoundDocument( @@ -86,7 +95,8 @@ class Index implements RequestHandlerInterface new Structure\Link\SelfLink($this->buildUrl($request)), new Structure\Meta('offset', $offset), new Structure\Meta('limit', $limit), - ...($total !== null ? [new Structure\Meta('total', $total)] : []) + ...($total !== null ? [new Structure\Meta('total', $total)] : []), + ...$documentMeta ) ); } diff --git a/src/Schema/Type.php b/src/Schema/Type.php index 78d50fd..4789413 100644 --- a/src/Schema/Type.php +++ b/src/Schema/Type.php @@ -35,6 +35,7 @@ final class Type private $updatable = false; private $deletable = false; private $deleteCallback; + private $documentMeta = []; /** * Set the description of the type for documentation generation. @@ -483,4 +484,14 @@ final class Type { return $this->defaultFilter; } + + public function documentMeta($key, $value) + { + $this->documentMeta[$key] = $value; + } + + public function getDocumentMeta() + { + return $this->documentMeta; + } }