Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

HashConverter.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Page;
8
9 use eZ\Publish\Core\FieldType\Page\Parts\Page;
10 use eZ\Publish\Core\FieldType\Page\Parts\Zone;
11 use eZ\Publish\Core\FieldType\Page\Parts\Block;
12 use eZ\Publish\Core\FieldType\Page\Parts\Item;
13
18 class HashConverter
19 {
27 public function convertFromValue(Value $value)
28 {
29 $hash = [];
30
31 foreach ($value->page->getState() as $propName => $propValue) {
32 switch ($propName) {
33 case 'layout':
34 if ($propValue !== null) {
35 $hash['layout'] = $propValue;
36 }
37 break;
38 case 'attributes':
39 if ($propValue !== null && $propValue !== []) {
40 $hash['attributes'] = $propValue;
41 }
42 break;
43 case 'zones':
44 foreach ($propValue as $zone) {
45 $hash['zones'][] = $this->convertZoneToHash($zone);
46 }
47 break;
48 }
49 }
50
51 return $hash;
52 }
53
61 protected function convertZoneToHash(Zone $zone)
62 {
63 $hash = [];
64
65 foreach ($zone->getState() as $propName => $propValue) {
66 switch ($propName) {
67 case 'id':
68 case 'identifier':
69 case 'action':
70 if ($propValue !== null) {
71 $hash[$propName] = $propValue;
72 }
73 break;
74 case 'attributes':
75 if ($propValue !== null && $propValue !== []) {
76 $hash['attributes'] = $propValue;
77 }
78 break;
79 case 'blocks':
80 foreach ($propValue as $block) {
81 $hash['blocks'][] = $this->convertBlockToHash($block);
82 }
83 break;
84 }
85 }
86
87 return $hash;
88 }
89
97 protected function convertBlockToHash(Block $block)
98 {
99 $hash = [];
100
101 foreach ($block->getState() as $propName => $propValue) {
102 switch ($propName) {
103 case 'id':
104 case 'name':
105 case 'type':
106 case 'view':
107 case 'overflowId':
108 case 'customAttributes':
109 case 'action':
110 case 'rotation':
111 case 'zoneId':
112 if ($propValue !== null) {
113 $hash[$propName] = $propValue;
114 }
115 break;
116 case 'attributes':
117 if ($propValue !== null && $propValue !== []) {
118 $hash['attributes'] = $propValue;
119 }
120 break;
121 case 'items':
122 foreach ($propValue as $item) {
123 $hash['items'][] = $this->convertItemToHash($item);
124 }
125 break;
126 }
127 }
128
129 return $hash;
130 }
131
139 protected function convertItemToHash(Item $item)
140 {
141 $hash = [];
142
143 foreach ($item->getState() as $propName => $propValue) {
144 switch ($propName) {
145 case 'blockId':
146 case 'contentId':
147 case 'locationId':
148 case 'priority':
149 case 'movedTo':
150 case 'action':
151 if ($propValue !== null) {
152 $hash[$propName] = $propValue;
153 }
154 break;
155 case 'attributes':
156 if ($propValue !== null && $propValue !== []) {
157 $hash['attributes'] = $propValue;
158 }
159 break;
160 case 'publicationDate':
161 case 'visibilityDate':
162 case 'hiddenDate':
163 case 'rotationUntilDate':
164 if ($propValue !== null) {
165 /* @var $propValue \DateTime */
166 $hash[$propName] = $propValue->format(\DateTime::RFC850);
167 }
168 break;
169 }
170 }
171
172 return $hash;
173 }
174
182 public function convertToValue($hash)
183 {
184 if (isset($hash['zones'])) {
185 $zones = [];
186
187 foreach ($hash['zones'] as $zone) {
188 $zones[] = $this->convertZoneFromHash($zone);
189 }
190
191 $hash['zones'] = $zones;
192 }
193
194 return new Value(new Page($hash));
195 }
196
204 protected function convertZoneFromHash($hash)
205 {
206 if (isset($hash['blocks'])) {
207 $blocks = [];
208
209 foreach ($hash['blocks'] as $block) {
210 $blocks[] = $this->convertBlockFromHash($block);
211 }
212
213 $hash['blocks'] = $blocks;
214 }
215
216 return new Zone($hash);
217 }
218
226 protected function convertBlockFromHash($hash)
227 {
228 if (isset($hash['items'])) {
229 $items = [];
230
231 foreach ($hash['items'] as $item) {
232 $items[] = $this->convertItemFromHash($item);
233 }
234
235 $hash['items'] = $items;
236 }
237
238 return new Block($hash);
239 }
240
248 protected function convertItemFromHash($hash)
249 {
250 foreach ($hash as $key => &$item) {
251 switch ($key) {
252 case 'publicationDate':
253 case 'visibilityDate':
254 case 'hiddenDate':
255 case 'rotationUntilDate':
256 // $item is expected to be a date string in RFC850 format
257 $item = new \DateTime($item);
258 break;
259 }
260 }
261
262 return new Item($hash);
263 }
264 }
eZ\Publish\Core\FieldType\Page\HashConverter
Class HashConverter converts between a Page field type Value object and a representation of the same ...
Definition: HashConverter.php:19
eZ\Publish\Core\FieldType\Page\HashConverter\convertZoneFromHash
convertZoneFromHash($hash)
Converts the given $hash to a Zone node.
Definition: HashConverter.php:204
eZ\Publish\Core\FieldType\Page\HashConverter\convertToValue
convertToValue($hash)
Converts the given $hash to a Value defined by the field type.
Definition: HashConverter.php:182
eZ\Publish\Core\FieldType\Page\HashConverter\convertZoneToHash
convertZoneToHash(Zone $zone)
Converts the given $zone into a plain hash format.
Definition: HashConverter.php:61
eZ\Publish\Core\FieldType\Page\HashConverter\convertFromValue
convertFromValue(Value $value)
Converts the given $value into a plain hash format.
Definition: HashConverter.php:27
eZ\Publish\Core\FieldType\Page\HashConverter\convertItemFromHash
convertItemFromHash($hash)
Converts the given $hash to a Item node.
Definition: HashConverter.php:248
eZ\Publish\Core\FieldType\Page\HashConverter\convertBlockToHash
convertBlockToHash(Block $block)
Converts the given $block into a plain hash format.
Definition: HashConverter.php:97
eZ\Publish\Core\FieldType\Page\HashConverter\convertItemToHash
convertItemToHash(Item $item)
Converts the given $item into a plain hash format.
Definition: HashConverter.php:139
eZ\Publish\Core\FieldType\Page\HashConverter\convertBlockFromHash
convertBlockFromHash($hash)
Converts the given $hash to a Block node.
Definition: HashConverter.php:226
eZ\Publish\Core\FieldType\Page\Parts\Base\getState
getState()
Returns available properties with their values as a simple hash.
Definition: FieldType/Page/Parts/Base.php:42
eZ\Publish\Core\FieldType\Page\Parts\Block
Definition: FieldType/Page/Parts/Block.php:22
eZ\Publish\Core\FieldType\Page\Parts\Item
Definition: Item.php:24
eZ\Publish\Core\FieldType\Page\Parts\Item\getState
getState()
Returns available properties with their values as a simple hash.
Definition: Item.php:71
eZ\Publish\Core\FieldType\Page\Parts\Page
Definition: Publish/Core/FieldType/Page/Parts/Page.php:15
eZ\Publish\Core\FieldType\Page\Parts\Zone
Definition: FieldType/Page/Parts/Zone.php:17
eZ\Publish\Core\FieldType\Page\Value
Definition: Core/FieldType/Page/Value.php:13
eZ\Publish\Core\FieldType\Page
Definition: HashConverter.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.