1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Document; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\AttributesContainer; |
7
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksAwareInterface; |
8
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksContainer; |
9
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataAwareInterface; |
10
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataContainer; |
11
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\RelationshipsAwareInterface; |
12
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\RelationshipsContainer; |
13
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\ResourceBehaviour; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Resource Object |
17
|
|
|
* |
18
|
|
|
* @see http://jsonapi.org/format/#document-resource-objects |
19
|
|
|
* |
20
|
|
|
* @package Mikemirten\Component\JsonApi\Document |
21
|
|
|
*/ |
22
|
|
|
class ResourceObject implements MetadataAwareInterface, LinksAwareInterface, RelationshipsAwareInterface |
23
|
|
|
{ |
24
|
|
|
use ResourceBehaviour; |
25
|
|
|
use AttributesContainer; |
26
|
|
|
use MetadataContainer; |
27
|
|
|
use LinksContainer; |
28
|
|
|
use RelationshipsContainer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ResourceObject constructor. |
32
|
|
|
* |
33
|
|
|
* @param string $id |
34
|
|
|
* @param string $type |
35
|
|
|
* @param array $attributes |
36
|
|
|
* @param array $metadata |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $id, string $type, array $attributes = [], array $metadata = []) |
39
|
|
|
{ |
40
|
|
|
$this->id = $id; |
41
|
|
|
$this->type = $type; |
42
|
|
|
$this->attributes = $attributes; |
43
|
|
|
$this->metadata = $metadata; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Cast to an array |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function toArray(): array |
52
|
|
|
{ |
53
|
|
|
$data = $this->resourceToArray(); |
54
|
|
|
|
55
|
|
|
if ($this->hasMetadata()) { |
56
|
|
|
$data['meta'] = $this->getMetadata(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($this->hasLinks()) { |
60
|
|
|
$data['links'] = $this->linksToArray(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->hasAttributes()) { |
64
|
|
|
$data['attributes'] = $this->getAttributes(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->hasRelationships()) { |
68
|
|
|
$data['relationships'] = $this->relationshipsToArray(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $data; |
72
|
|
|
} |
73
|
|
|
} |