1 | <?php |
||
2 | |||
3 | namespace Siren; |
||
4 | |||
5 | use Siren\Action; |
||
6 | use Siren\Document; |
||
7 | use Siren\Entity; |
||
8 | use Siren\Field; |
||
9 | use Siren\Link; |
||
10 | |||
11 | class Handler |
||
12 | { |
||
13 | /** |
||
14 | * Method to convert a Siren document to a JSON string |
||
15 | * |
||
16 | * @param Document $document |
||
17 | * @return string |
||
18 | */ |
||
19 | public function toJson(Document $document) |
||
20 | { |
||
21 | $documentArray = $document->toArray(); |
||
22 | |||
23 | return json_encode($documentArray); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Method to convert a siren JSON string into a Siren document object |
||
28 | * |
||
29 | * @param string $json |
||
30 | * @return Document |
||
31 | */ |
||
32 | public function toDocument($json) |
||
33 | { |
||
34 | $data = json_decode($json, true); |
||
35 | |||
36 | $document = new Document(); |
||
37 | |||
38 | View Code Duplication | if (isset($data['class']) && is_array($data['class'])) { |
|
0 ignored issues
–
show
|
|||
39 | $document->setClass($data['class']); |
||
40 | } |
||
41 | |||
42 | View Code Duplication | if (isset($data['properties']) && is_array($data['properties'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
43 | $document->setProperties($data['properties']); |
||
44 | } |
||
45 | |||
46 | if (isset($data['entities']) && is_array($data['entities'])) { |
||
47 | $entities = $this->getEntitiesFromDataArray($data['entities']); |
||
48 | $document->setEntities($entities); |
||
49 | } |
||
50 | |||
51 | if (isset($data['actions']) && is_array($data['actions'])) { |
||
52 | $actions = $this->getActionsFromDataArray($data['actions']); |
||
53 | $document->setActions($actions); |
||
54 | } |
||
55 | |||
56 | View Code Duplication | if (isset($data['links']) && is_array($data['links'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
57 | $links = $this->getLinksFromDataArray($data['links']); |
||
58 | $document->setLinks($links); |
||
59 | } |
||
60 | |||
61 | return $document; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Helper method to convert an entities data array to an array of entity objects |
||
66 | * |
||
67 | * @param array $entitiesArray |
||
68 | * @return Entity[] |
||
69 | */ |
||
70 | protected function getEntitiesFromDataArray(array $entitiesArray) |
||
71 | { |
||
72 | $entities = array(); |
||
73 | foreach ($entitiesArray as $entityArray) { |
||
74 | $entity = new Entity(); |
||
75 | |||
76 | View Code Duplication | if (isset($entityArray['class']) && is_array($entityArray['class'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
77 | $entity->setClass($entityArray['class']); |
||
78 | } |
||
79 | |||
80 | if (isset($entityArray['rel']) && is_array($entityArray['rel'])) { |
||
81 | $entity->setRel($entityArray['rel']); |
||
82 | } |
||
83 | |||
84 | if (isset($entityArray['href']) && is_string($entityArray['href'])) { |
||
85 | $entity->setHref($entityArray['href']); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | if (isset($entityArray['properties']) && is_array($entityArray['properties'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
89 | $entity->setProperties($entityArray['properties']); |
||
90 | } |
||
91 | |||
92 | View Code Duplication | if (isset($entityArray['links']) && is_array($entityArray['links'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
93 | $links = $this->getLinksFromDataArray($entityArray['links']); |
||
94 | $entity->setLinks($links); |
||
95 | } |
||
96 | |||
97 | $entities[] = $entity; |
||
98 | } |
||
99 | |||
100 | return $entities; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Helper method to convert an action data array to an array of action objects |
||
105 | * |
||
106 | * @param array $actionsArray |
||
107 | * @return Action[] |
||
108 | */ |
||
109 | protected function getActionsFromDataArray(array $actionsArray) |
||
110 | { |
||
111 | $actions = array(); |
||
112 | foreach ($actionsArray as $actionArray) { |
||
113 | $action = new Action(); |
||
114 | |||
115 | View Code Duplication | if (isset($actionArray['name']) && is_string($actionArray['name'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
116 | $action->setName($actionArray['name']); |
||
117 | } |
||
118 | |||
119 | if (isset($actionArray['title']) && is_string($actionArray['title'])) { |
||
120 | $action->setTitle($actionArray['title']); |
||
121 | } |
||
122 | |||
123 | View Code Duplication | if (isset($actionArray['method']) && is_string($actionArray['method'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
124 | $action->setMethod($actionArray['method']); |
||
125 | } |
||
126 | |||
127 | View Code Duplication | if (isset($actionArray['href']) && is_string($actionArray['href'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
128 | $action->setHref($actionArray['href']); |
||
129 | } |
||
130 | |||
131 | View Code Duplication | if (isset($actionArray['type']) && is_string($actionArray['type'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
132 | $action->setType($actionArray['type']); |
||
133 | } |
||
134 | |||
135 | if (isset($actionArray['fields']) && is_array($actionArray['fields'])) { |
||
136 | $fields = $this->getFieldsFromDataArray($actionArray['fields']); |
||
137 | $action->setFields($fields); |
||
138 | } |
||
139 | |||
140 | $actions[] = $action; |
||
141 | } |
||
142 | |||
143 | return $actions; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Helper method to convert a fields data array to an array of field objects |
||
148 | * |
||
149 | * @param array $fieldsArray |
||
150 | * @return Field[] |
||
151 | */ |
||
152 | protected function getFieldsFromDataArray(array $fieldsArray) |
||
153 | { |
||
154 | $fields = array(); |
||
155 | foreach ($fieldsArray as $fieldArray) { |
||
156 | $field = new Field(); |
||
157 | |||
158 | View Code Duplication | if (isset($fieldArray['name']) && is_string($fieldArray['name'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
159 | $field->setName($fieldArray['name']); |
||
160 | } |
||
161 | |||
162 | View Code Duplication | if (isset($fieldArray['type']) && is_string($fieldArray['type'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
163 | $field->setType($fieldArray['type']); |
||
164 | } |
||
165 | |||
166 | if (isset($fieldArray['value'])) { |
||
167 | $field->setValue($fieldArray['value']); |
||
168 | } |
||
169 | |||
170 | $fields[] = $field; |
||
171 | } |
||
172 | |||
173 | return $fields; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Helper method to convert a link data array to an array of link objects |
||
178 | * |
||
179 | * @param array $linksArray |
||
180 | * @return Link[] |
||
181 | */ |
||
182 | protected function getLinksFromDataArray(array $linksArray) |
||
183 | { |
||
184 | $links = array(); |
||
185 | foreach ($linksArray as $linkArray) { |
||
186 | $link = new Link(); |
||
187 | |||
188 | if (isset($linkArray['rel']) && is_array($linkArray['rel'])) { |
||
189 | $link->setRel($linkArray['rel']); |
||
190 | } |
||
191 | |||
192 | if (isset($linkArray['href']) && is_string($linkArray['href'])) { |
||
193 | $link->setHref($linkArray['href']); |
||
194 | } |
||
195 | |||
196 | $links[] = $link; |
||
197 | } |
||
198 | |||
199 | return $links; |
||
200 | } |
||
201 | } |
||
202 |
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.