1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Document; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\ErrorsAwareInterface; |
7
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\ErrorsContainer; |
8
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\IncludedResourcesAwareInterface; |
9
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\IncludedResourcesContainer; |
10
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksAwareInterface; |
11
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksContainer; |
12
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataAwareInterface; |
13
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataContainer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract Document |
17
|
|
|
* |
18
|
|
|
* @see http://jsonapi.org/format/#document-structure |
19
|
|
|
* |
20
|
|
|
* Represents base JsonAPI-document structure |
21
|
|
|
* Supposed to be extended by case-based documents |
22
|
|
|
* |
23
|
|
|
* @package Mikemirten\Component\JsonApi\Document |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractDocument implements |
26
|
|
|
MetadataAwareInterface, |
27
|
|
|
LinksAwareInterface, |
28
|
|
|
ErrorsAwareInterface, |
29
|
|
|
IncludedResourcesAwareInterface |
30
|
|
|
{ |
31
|
|
|
use MetadataContainer; |
32
|
|
|
use LinksContainer; |
33
|
|
|
use ErrorsContainer; |
34
|
|
|
use IncludedResourcesContainer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* JsonAPI-object |
38
|
|
|
* |
39
|
|
|
* @see http://jsonapi.org/format/#document-jsonapi-object |
40
|
|
|
* |
41
|
|
|
* @var JsonApiObject |
42
|
|
|
*/ |
43
|
|
|
protected $jsonApi; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set JsonAPI-object |
47
|
|
|
* |
48
|
|
|
* @param JsonApiObject $jsonApi |
49
|
|
|
*/ |
50
|
10 |
|
public function setJsonApi(JsonApiObject $jsonApi) |
51
|
|
|
{ |
52
|
10 |
|
$this->jsonApi = $jsonApi; |
53
|
10 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set JsonAPI-object |
57
|
|
|
* |
58
|
|
|
* @return JsonApiObject |
59
|
|
|
*/ |
60
|
5 |
|
public function getJsonApi(): JsonApiObject |
61
|
|
|
{ |
62
|
5 |
|
return $this->jsonApi; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Cast to an array |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
29 |
|
public function toArray(): array |
71
|
|
|
{ |
72
|
|
|
$data = [ |
73
|
29 |
|
'meta' => $this->getMetadata(), |
74
|
29 |
|
'links' => $this->linksToArray(), |
75
|
29 |
|
'errors' => $this->errorsToArray(), |
76
|
29 |
|
'included' => $this->includedResourcesToArray() |
77
|
|
|
]; |
78
|
|
|
|
79
|
29 |
|
$data = array_filter($data, 'count'); |
80
|
|
|
|
81
|
29 |
|
if ($this->jsonApi !== null) { |
82
|
5 |
|
$data['jsonapi'] = $this->jsonApi->toArray(); |
83
|
|
|
} |
84
|
|
|
|
85
|
29 |
|
return $data; |
86
|
|
|
} |
87
|
|
|
} |