Skip to content

Commit e5f9a62

Browse files
committed
wip
1 parent 5d76c0f commit e5f9a62

File tree

16 files changed

+353
-345
lines changed

16 files changed

+353
-345
lines changed

README.md

Lines changed: 98 additions & 105 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "tobyz/json-api-server",
3+
"description": "A fully automated framework-agnostic JSON:API server implementation in PHP.",
34
"require": {
45
"php": "^7.2",
56
"doctrine/inflector": "^1.3",
@@ -19,7 +20,10 @@
1920
"psr-4": {
2021
"Tobyz\\JsonApiServer\\": "src/"
2122
},
22-
"files": ["src/functions.php"]
23+
"files": [
24+
"src/functions.php",
25+
"src/functions_laravel.php"
26+
]
2327
},
2428
"autoload-dev": {
2529
"psr-4": {
@@ -31,6 +35,9 @@
3135
"helmich/phpunit-json-assert": "^3.0",
3236
"phpunit/phpunit": "^8.0"
3337
},
38+
"scripts": {
39+
"test": "phpunit"
40+
},
3441
"config": {
3542
"sort-packages": true
3643
}

src/Handler/Concerns/SavesData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ private function setValues(array $data, $model, Request $request)
208208

209209
$value = get_value($data, $field);
210210

211-
if ($setter = $field->getSetter()) {
212-
$setter($model, $value, $request);
211+
if ($setCallback = $field->getSetCallback()) {
212+
$setCallback($model, $value, $request);
213213
continue;
214214
}
215215

216-
if ($field->getSaver()) {
216+
if ($field->getSaveCallback()) {
217217
continue;
218218
}
219219

src/Handler/Create.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ private function createModel(Request $request)
7878
private function fillDefaultValues(array &$data, Request $request)
7979
{
8080
foreach ($this->resource->getSchema()->getFields() as $field) {
81-
if (! has_value($data, $field) && ($default = $field->getDefault())) {
82-
set_value($data, $field, $default($request));
81+
if (! has_value($data, $field) && ($defaultCallback = $field->getDefaultCallback())) {
82+
set_value($data, $field, $defaultCallback($request));
8383
}
8484
}
8585
}

src/Handler/Show.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function handle(Request $request): Response
4949
run_callbacks($this->resource->getSchema()->getListeners('show'), [$this->model, $request]);
5050

5151
$serializer = new Serializer($this->api, $request);
52-
$serializer->addSingle($this->resource, $this->model, $include);
52+
$serializer->add($this->resource, $this->model, $include, true);
5353

5454
return new JsonApiResponse(
5555
new CompoundDocument(

src/JsonApi.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
use Psr\Http\Server\RequestHandlerInterface;
1818
use Tobyz\JsonApiServer\Adapter\AdapterInterface;
1919
use Tobyz\JsonApiServer\Exception\BadRequestException;
20+
use Tobyz\JsonApiServer\Exception\ForbiddenException;
2021
use Tobyz\JsonApiServer\Exception\InternalServerErrorException;
2122
use Tobyz\JsonApiServer\Exception\MethodNotAllowedException;
2223
use Tobyz\JsonApiServer\Exception\NotAcceptableException;
2324
use Tobyz\JsonApiServer\Exception\NotImplementedException;
2425
use Tobyz\JsonApiServer\Exception\ResourceNotFoundException;
26+
use Tobyz\JsonApiServer\Exception\UnauthorizedException;
2527
use Tobyz\JsonApiServer\Exception\UnsupportedMediaTypeException;
2628
use Tobyz\JsonApiServer\Handler\Concerns\FindsResources;
2729
use Tobyz\JsonApiServer\Http\MediaTypes;
@@ -34,6 +36,7 @@ final class JsonApi implements RequestHandlerInterface
3436

3537
private $resources = [];
3638
private $baseUrl;
39+
private $authenticated = false;
3740

3841
public function __construct(string $baseUrl)
3942
{
@@ -50,6 +53,8 @@ public function resource(string $type, AdapterInterface $adapter, callable $buil
5053

5154
/**
5255
* Get defined resource types.
56+
*
57+
* @return ResourceType[]
5358
*/
5459
public function getResources(): array
5560
{
@@ -209,6 +214,10 @@ public function error($e)
209214
$e = new InternalServerErrorException;
210215
}
211216

217+
if (! $this->authenticated && $e instanceof ForbiddenException) {
218+
$e = new UnauthorizedException;
219+
}
220+
212221
$errors = $e->getJsonApiErrors();
213222
$status = $e->getJsonApiStatus();
214223

@@ -226,4 +235,12 @@ public function getBaseUrl(): string
226235
{
227236
return $this->baseUrl;
228237
}
238+
239+
/**
240+
* Indicate that the consumer is authenticated.
241+
*/
242+
public function authenticated(): void
243+
{
244+
$this->authenticated = true;
245+
}
229246
}

src/Schema/Concerns/HasMeta.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Forust.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tobyz\JsonApiServer\Schema\Concerns;
13+
14+
use Tobyz\JsonApiServer\Schema\Meta;
15+
16+
trait HasMeta
17+
{
18+
private $meta = [];
19+
20+
/**
21+
* Add a meta attribute.
22+
*/
23+
public function meta(string $name, callable $value): Meta
24+
{
25+
return $this->meta[$name] = new Meta($name, $value);
26+
}
27+
28+
/**
29+
* Remove a meta attribute.
30+
*/
31+
public function removeMeta(string $name): void
32+
{
33+
unset($this->meta[$name]);
34+
}
35+
36+
/**
37+
* Get the meta attributes.
38+
*
39+
* @return Meta[]
40+
*/
41+
public function getMeta(): array
42+
{
43+
return $this->meta;
44+
}
45+
}

src/Schema/Relationship.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace Tobyz\JsonApiServer\Schema;
1313

14+
use Tobyz\JsonApiServer\Schema\Concerns\HasMeta;
15+
1416
abstract class Relationship extends Field
1517
{
18+
use HasMeta;
19+
1620
private $type;
1721
private $linkage = false;
1822
private $links = true;

src/Schema/Type.php

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
namespace Tobyz\JsonApiServer\Schema;
1313

1414
use Tobyz\JsonApiServer\Schema\Concerns\HasListeners;
15+
use Tobyz\JsonApiServer\Schema\Concerns\HasMeta;
1516
use function Tobyz\JsonApiServer\negate;
1617

1718
final class Type
1819
{
19-
use HasListeners;
20+
use HasListeners, HasMeta;
2021

2122
private $fields = [];
22-
private $meta = [];
2323
private $filters = [];
2424
private $sortFields = [];
2525
private $perPage = 20;
@@ -95,32 +95,6 @@ public function getFields(): array
9595
return $this->fields;
9696
}
9797

98-
/**
99-
* Add a meta attribute to the resource type.
100-
*/
101-
public function meta(string $name, callable $value): Meta
102-
{
103-
return $this->meta[$name] = new Meta($name, $value);
104-
}
105-
106-
/**
107-
* Remove a meta attribute from the resource type.
108-
*/
109-
public function removeMeta(string $name): void
110-
{
111-
unset($this->meta[$name]);
112-
}
113-
114-
/**
115-
* Get the resource type's meta attributes.
116-
*
117-
* @return Meta[]
118-
*/
119-
public function getMeta(): array
120-
{
121-
return $this->meta;
122-
}
123-
12498
/**
12599
* Add a filter to the resource type.
126100
*/

0 commit comments

Comments
 (0)