Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
arrayconverter.php
1<?php
2
4
7
13final class ArrayConverter
14{
22 public static function convertToArray(Address $address, bool $convertLocation = true): array
23 {
24 $result = [
25 'id' => $address->getId(),
26 'latitude' => $address->getLatitude(),
27 'longitude' => $address->getLongitude(),
28 'languageId' => $address->getLanguageId(),
29 'fieldCollection' => self::convertFieldsToArray($address->getAllFieldsValues()),
30 'links' => self::convertLinksToArray($address)
31 ];
32
33 if ($convertLocation && $location = $address->getLocation())
34 {
35 $result['location'] = Location\Converter\ArrayConverter::convertToArray($location);
36 }
37
38 return $result;
39 }
40
47 public static function convertFromArray(array $data): Address
48 {
49 $languageId = (string)($data['languageId'] ?? '');
50
51 $result = new Address($languageId);
52
53 if (isset($data['id']))
54 {
55 $result->setId((int)$data['id']);
56 }
57
58 if (isset($data['latitude']))
59 {
60 $result->setLatitude((string)$data['latitude']);
61 }
62
63 if (isset($data['longitude']))
64 {
65 $result->setLongitude((string)$data['longitude']);
66 }
67
68 if (isset($data['fieldCollection']) && is_array($data['fieldCollection']))
69 {
70 foreach ($data['fieldCollection'] as $itemType => $itemValue)
71 {
72 $result->setFieldValue((int)$itemType, (string)$itemValue);
73 }
74 }
75
76 if (isset($data['links']) && is_array($data['links']))
77 {
78 foreach ($data['links'] as $link)
79 {
80 $result->addLink((string)$link['entityId'], (string)$link['entityType']);
81 }
82 }
83
84 if (isset($data['location']) && is_array($data['location']))
85 {
86 if ($location = Location::fromArray($data['location']))
87 {
88 $result->setLocation($location);
89 }
90 }
91
92 return $result;
93 }
94
99 protected static function convertFieldsToArray(array $fieldsValues): array
100 {
101 $result = [];
102
103 foreach ($fieldsValues as $type => $value)
104 {
105 $result[$type] = $value;
106 }
107
108 return $result;
109 }
110
115 protected static function convertLinksToArray(Address $address): array
116 {
117 $result = [];
118
119 foreach ($address->getLinks() as $link)
120 {
121 $result[] = [
122 'entityId' => $link->getAddressLinkEntityId(),
123 'entityType' => $link->getAddressLinkEntityType(),
124 ];
125 }
126
127 return $result;
128 }
129}
static convertToArray(Address $address, bool $convertLocation=true)
static fromArray(array $location)
Definition location.php:550