Polyfill old behavior

This commit is contained in:
Toby Zerner 2021-01-17 11:29:13 +10:30
parent 7681ec78ca
commit 74a6edf300
3 changed files with 38 additions and 1 deletions

16
src/Context.php Normal file
View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of tobyz/json-api-server.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}

View File

@ -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;
@ -76,6 +77,14 @@ class Index implements RequestHandlerInterface
$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(
new Structure\PaginatedCollection(
@ -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
)
);
}

View File

@ -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;
}
}