|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi\objects; |
|
4
|
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\helpers\AtMemberManager; |
|
6
|
|
|
use alsvanzelf\jsonapi\helpers\Converter; |
|
7
|
|
|
use alsvanzelf\jsonapi\helpers\ExtensionMemberManager; |
|
8
|
|
|
use alsvanzelf\jsonapi\helpers\Validator; |
|
9
|
|
|
use alsvanzelf\jsonapi\interfaces\ObjectInterface; |
|
10
|
|
|
|
|
11
|
|
|
class AttributesObject implements ObjectInterface { |
|
12
|
|
|
use AtMemberManager, ExtensionMemberManager; |
|
13
|
|
|
|
|
14
|
|
|
/** @var array */ |
|
15
|
|
|
protected $attributes = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* human api |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @note if an `id` is set inside $attributes, it is removed from there |
|
23
|
|
|
* it is common to find it inside, and not doing so will cause an exception |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $attributes |
|
26
|
|
|
* @return AttributesObject |
|
27
|
|
|
*/ |
|
28
|
12 |
|
public static function fromArray(array $attributes) { |
|
29
|
12 |
|
unset($attributes['id']); |
|
30
|
|
|
|
|
31
|
12 |
|
$attributesObject = new self(); |
|
32
|
|
|
|
|
33
|
12 |
|
foreach ($attributes as $key => $value) { |
|
34
|
12 |
|
$attributesObject->add($key, $value); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
12 |
|
return $attributesObject; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param object $attributes |
|
42
|
|
|
* @return AttributesObject |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function fromObject($attributes) { |
|
45
|
1 |
|
$array = Converter::objectToArray($attributes); |
|
46
|
|
|
|
|
47
|
1 |
|
return self::fromArray($array); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* spec api |
|
52
|
|
|
*/ |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $key |
|
56
|
|
|
* @param mixed $value |
|
57
|
|
|
*/ |
|
58
|
35 |
|
public function add($key, $value) { |
|
59
|
35 |
|
Validator::checkMemberName($key); |
|
60
|
|
|
|
|
61
|
34 |
|
if (is_object($value)) { |
|
62
|
1 |
|
$value = Converter::objectToArray($value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
34 |
|
$this->attributes[$key] = $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* internal api |
|
70
|
|
|
*/ |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @internal |
|
74
|
|
|
* |
|
75
|
|
|
* @return string[] |
|
76
|
|
|
*/ |
|
77
|
14 |
|
public function getKeys() { |
|
78
|
14 |
|
return array_keys($this->attributes); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* ObjectInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
*/ |
|
88
|
28 |
|
public function isEmpty() { |
|
89
|
28 |
|
if ($this->attributes !== []) { |
|
90
|
25 |
|
return false; |
|
91
|
|
|
} |
|
92
|
3 |
|
if ($this->hasAtMembers()) { |
|
93
|
1 |
|
return false; |
|
94
|
|
|
} |
|
95
|
2 |
|
if ($this->hasExtensionMembers()) { |
|
96
|
1 |
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritDoc |
|
104
|
|
|
*/ |
|
105
|
30 |
|
public function toArray() { |
|
106
|
30 |
|
$array = []; |
|
107
|
|
|
|
|
108
|
30 |
|
if ($this->hasAtMembers()) { |
|
109
|
1 |
|
$array = array_merge($array, $this->getAtMembers()); |
|
110
|
|
|
} |
|
111
|
30 |
|
if ($this->hasExtensionMembers()) { |
|
112
|
2 |
|
$array = array_merge($array, $this->getExtensionMembers()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
30 |
|
return array_merge($array, $this->attributes); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|