1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Factory; |
6
|
|
|
|
7
|
|
|
use VGirol\JsonApiAssert\Members; |
8
|
|
|
|
9
|
|
|
class ResourceObjectFactory extends BaseFactory |
10
|
|
|
{ |
11
|
|
|
use HasIdentification; |
12
|
|
|
use HasMeta; |
13
|
|
|
use HasLinks; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Undocumented variable |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
public $attributes; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Undocumented variable |
24
|
|
|
* |
25
|
|
|
* @var array<string, RelationshipFactory> |
26
|
|
|
*/ |
27
|
|
|
public $relationships; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Undocumented function |
31
|
|
|
* |
32
|
|
|
* @param array $attributes |
33
|
|
|
* @return static |
34
|
|
|
*/ |
35
|
|
|
public function setAttributes(array $attributes) |
36
|
|
|
{ |
37
|
|
|
$this->attributes = $attributes; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Undocumented function |
44
|
|
|
* |
45
|
|
|
* @param array $attributes |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
|
|
public function addAttributes(array $attributes) |
49
|
|
|
{ |
50
|
|
|
foreach ($attributes as $name => $value) { |
51
|
|
|
$this->addAttribute($name, $value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Undocumented function |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @param mixed $value |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
|
|
public function addAttribute(string $name, $value) |
65
|
|
|
{ |
66
|
|
|
$this->addToObject('attributes', $name, $value); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Undocumented function |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @param RelationshipFactory $relationship |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
|
|
public function addRelationship(string $name, $relationship) |
79
|
|
|
{ |
80
|
|
|
$this->addToObject('relationships', $name, $relationship); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Undocumented function |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function toArray(): array |
91
|
|
|
{ |
92
|
|
|
$resource = $this->getIdentification(); |
93
|
|
|
|
94
|
|
|
$resource[Members::ATTRIBUTES] = $this->attributes; |
95
|
|
|
|
96
|
|
|
if (isset($this->meta)) { |
97
|
|
|
$resource[Members::META] = $this->meta; |
98
|
|
|
} |
99
|
|
|
if (isset($this->links)) { |
100
|
|
|
$resource[Members::LINKS] = $this->links; |
101
|
|
|
} |
102
|
|
|
if (isset($this->relationships)) { |
103
|
|
|
$resource[Members::RELATIONSHIPS] = array_map( |
104
|
|
|
function ($relationship) { |
105
|
|
|
return $relationship->toArray(); |
106
|
|
|
}, |
107
|
|
|
$this->relationships |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $resource; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|