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