1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Definition of attribute |
8
|
|
|
* |
9
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition |
10
|
|
|
*/ |
11
|
|
|
class Attribute |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Name of property contains related object. |
15
|
|
|
* Value is optional. Can be set only for real properties. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $propertyName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Unique name of serialized attribute |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Data-type |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $type; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Parameters for data-type handler |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $typeParameters = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Getter-method to access value |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $getter; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Attribute constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $name |
53
|
|
|
*/ |
54
|
7 |
|
public function __construct(string $name, string $getter) |
55
|
|
|
{ |
56
|
7 |
|
$this->name = $name; |
57
|
7 |
|
$this->getter = $getter; |
58
|
7 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get name |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
3 |
|
public function getName(): string |
66
|
|
|
{ |
67
|
3 |
|
return $this->name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get name of getter-method to access value of attribute |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
1 |
|
public function getGetter(): string |
76
|
|
|
{ |
77
|
1 |
|
return $this->getter; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set name of data-type |
82
|
|
|
* |
83
|
|
|
* @param string $type |
84
|
|
|
*/ |
85
|
3 |
|
public function setType(string $type) |
86
|
|
|
{ |
87
|
3 |
|
$this->type = $type; |
88
|
3 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Has data-type defined ? |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
1 |
|
public function hasType(): bool |
96
|
|
|
{ |
97
|
1 |
|
return $this->type !== null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get name of data-type |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
4 |
|
public function getType(): string |
106
|
|
|
{ |
107
|
4 |
|
return $this->type; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set parameters of data-type handling |
112
|
|
|
* |
113
|
|
|
* @param array $parameters |
114
|
|
|
*/ |
115
|
3 |
|
public function setTypeParameters(array $parameters) |
116
|
|
|
{ |
117
|
3 |
|
$this->typeParameters = $parameters; |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get parameters of data-type handling |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
4 |
|
public function getTypeParameters(): array |
126
|
|
|
{ |
127
|
4 |
|
return $this->typeParameters; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set name of property contains related object |
132
|
|
|
* |
133
|
|
|
* @param string $name |
134
|
|
|
*/ |
135
|
3 |
|
public function setPropertyName(string $name) |
136
|
|
|
{ |
137
|
3 |
|
$this->propertyName = $name; |
138
|
3 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Has name of property ? |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
1 |
|
public function hasPropertyName(): bool |
146
|
|
|
{ |
147
|
1 |
|
return $this->propertyName !== null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get name of property contains related object |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
4 |
|
public function getPropertyName(): string |
156
|
|
|
{ |
157
|
4 |
|
return $this->propertyName; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Merge a attribute into this one |
162
|
|
|
* |
163
|
|
|
* @param self $attribute |
164
|
|
|
*/ |
165
|
1 |
|
public function merge(self $attribute) |
166
|
|
|
{ |
167
|
1 |
|
if ($this->propertyName === null && $attribute->hasPropertyName()) { |
168
|
1 |
|
$this->propertyName = $attribute->getPropertyName(); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
if ($this->type === null && $attribute->hasType()) { |
172
|
1 |
|
$this->type = $attribute->getType(); |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
if (empty($this->typeParameters)) { |
176
|
1 |
|
$this->typeParameters = $attribute->getTypeParameters(); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |