1 | <?php |
||
2 | |||
3 | namespace Siren; |
||
4 | |||
5 | use Siren\Field; |
||
6 | use LogicException; |
||
7 | |||
8 | class Action |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $name; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $title; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $method; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $href; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $type; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $fields = array(); |
||
39 | |||
40 | /** |
||
41 | * Get name |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function getName() |
||
46 | { |
||
47 | return $this->name; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Set name |
||
52 | * |
||
53 | * @param string $name |
||
54 | * @return $this |
||
55 | */ |
||
56 | View Code Duplication | public function setName($name) |
|
0 ignored issues
–
show
|
|||
57 | { |
||
58 | if (!is_string($name)) { |
||
59 | $exMsg = sprintf('Provided name `%s` is not a string', $name); |
||
60 | throw new LogicException($exMsg); |
||
61 | } |
||
62 | |||
63 | $this->name = $name; |
||
64 | |||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Get title |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getTitle() |
||
74 | { |
||
75 | return $this->title; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Set title |
||
80 | * |
||
81 | * @param string $title |
||
82 | * @return $this |
||
83 | */ |
||
84 | View Code Duplication | public function setTitle($title) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
85 | { |
||
86 | if (!is_string($title)) { |
||
87 | $exMsg = sprintf('Provided title `%s` is not a string', $title); |
||
88 | throw new LogicException($exMsg); |
||
89 | } |
||
90 | |||
91 | $this->title = $title; |
||
92 | |||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get method |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getMethod() |
||
102 | { |
||
103 | return $this->method; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Set method |
||
108 | * |
||
109 | * @param string $method |
||
110 | * @return $this |
||
111 | */ |
||
112 | View Code Duplication | public function setMethod($method) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
113 | { |
||
114 | if (!is_string($method)) { |
||
115 | $exMsg = sprintf('Provided method `%s` is not a string', $method); |
||
116 | throw new LogicException($exMsg); |
||
117 | } |
||
118 | |||
119 | $this->method = $method; |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get href |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getHref() |
||
130 | { |
||
131 | return $this->href; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Set href |
||
136 | * |
||
137 | * @param string $href |
||
138 | * @return $this |
||
139 | * @throws LogicException |
||
140 | */ |
||
141 | View Code Duplication | public function setHref($href) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
142 | { |
||
143 | if (!is_string($href)) { |
||
144 | $exMsg = sprintf('Provided href `%s` is not a string', $href); |
||
145 | throw new LogicException($exMsg); |
||
146 | } |
||
147 | |||
148 | $this->href = $href; |
||
149 | |||
150 | return $this; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Get type |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getType() |
||
159 | { |
||
160 | return $this->type; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Set type |
||
165 | * |
||
166 | * @param string $type |
||
167 | * @return $this |
||
168 | */ |
||
169 | View Code Duplication | public function setType($type) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
170 | { |
||
171 | if (!is_string($type)) { |
||
172 | $exMsg = sprintf('Provided type `%s` is not a string', $type); |
||
173 | throw new LogicException($exMsg); |
||
174 | } |
||
175 | |||
176 | $this->type = $type; |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Get fields |
||
183 | * |
||
184 | * @return Field[] |
||
185 | */ |
||
186 | public function getFields() |
||
187 | { |
||
188 | return $this->fields; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Set fields |
||
193 | * |
||
194 | * @param Field[] $fields |
||
195 | * @return $this |
||
196 | * @throws LogicException |
||
197 | */ |
||
198 | public function setFields(array $fields) |
||
199 | { |
||
200 | foreach ($fields as $key => $field) { |
||
201 | if (!($field instanceof Field)) { |
||
202 | $exMsg = sprintf( |
||
203 | 'Array key `%s` is not an instance of Field', |
||
204 | $key |
||
205 | ); |
||
206 | throw new LogicException($exMsg); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | $this->fields = $fields; |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Add field |
||
217 | * |
||
218 | * @param Field $field |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function addfield(Field $field) |
||
222 | { |
||
223 | $this->fields[] = $field; |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Convert object to array |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function toArray() |
||
234 | { |
||
235 | $data = array(); |
||
236 | |||
237 | $name = $this->getName(); |
||
238 | if ($name !== null) { |
||
239 | $data['name'] = $name; |
||
240 | } |
||
241 | |||
242 | $title = $this->getTitle(); |
||
243 | if ($title !== null) { |
||
244 | $data['title'] = $title; |
||
245 | } |
||
246 | |||
247 | $method = $this->getMethod(); |
||
248 | if ($method !== null) { |
||
249 | $data['method'] = $method; |
||
250 | } |
||
251 | |||
252 | $href = $this->getHref(); |
||
253 | if ($href !== null) { |
||
254 | $data['href'] = $href; |
||
255 | } |
||
256 | |||
257 | $type = $this->getType(); |
||
258 | if ($type !== null) { |
||
259 | $data['type'] = $type; |
||
260 | } |
||
261 | |||
262 | $fields = $this->getFields(); |
||
263 | if (!empty($fields)) { |
||
264 | $data['fields'] = array(); |
||
265 | foreach ($fields as $field) { |
||
266 | $data['fields'][] = $field->toArray(); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | return $data; |
||
271 | } |
||
272 | } |
||
273 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.