Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ormconverter.php
1<?php
2
4
6use Bitrix\Location\Model\EO_Address;
7use Bitrix\Location\Model\EO_AddressField;
8use Bitrix\Location\Model\EO_Address_Collection;
9use Bitrix\Location\Model\EO_AddressField_Collection;
10use Bitrix\Location\Model\EO_AddressLink;
11use Bitrix\Location\Model\EO_AddressLink_Collection;
12
18final class OrmConverter
19{
26 public static function convertLinksToOrm(Address $address): EO_AddressLink_Collection
27 {
28 $result = new EO_AddressLink_Collection();
29
31 foreach ($address->getLinks() as $link)
32 {
33 $result->add(
34 (new EO_AddressLink())
35 ->setAddressId($address->getId())
36 ->setEntityId($link->getAddressLinkEntityId())
37 ->setEntityType($link->getAddressLinkEntityType())
38 );
39 }
40
41 return $result;
42 }
43
50 public static function convertFieldsToOrm(Address $address): EO_AddressField_Collection
51 {
52 $result = new EO_AddressField_Collection();
53 $normalizer = Address\Normalizer\Builder::build($address->getLanguageId());
54
56 foreach ($address->getFieldCollection() as $field)
57 {
58 $value = $field->getValue();
59 $result->add(
60 (new EO_AddressField())
61 ->setType($field->getType())
62 ->setValue($field->getValue())
63 ->setAddressId($address->getId())
64 ->setValueNormalized( $normalizer->normalize($value))
65 );
66 }
67
68 return $result;
69 }
70
79 public static function convertFromOrm(EO_Address $ormAddress): Address
80 {
81 $result = new Address($ormAddress->getLanguageId());
82 $result->setId($ormAddress->getId())
83 ->setLatitude($ormAddress->getLatitude())
84 ->setLongitude($ormAddress->getLongitude());
85
87 foreach ($ormAddress->getFields() as $field)
88 {
89 $result->setFieldValue($field->getType(), $field->getValue());
90 }
91
92 if($ormLocation = $ormAddress->getLocation())
93 {
94 $location = \Bitrix\Location\Entity\Location\Converter\OrmConverter::createLocation(
95 $ormLocation,
96 $ormAddress->getLanguageId()
97 );
98
99 if($location)
100 {
101 $result->setLocation($location);
102 }
103 }
104
105 if($links = $ormAddress->getLinks())
106 {
108 foreach ($links as $link)
109 {
110 $result->addLink($link->getEntityId(), $link->getEntityType());
111 }
112 }
113
114 return $result;
115 }
116
126 public static function convertCollectionFromOrm(EO_Address_Collection $collection): Address\AddressCollection
127 {
128 $result = new Address\AddressCollection();
129
131 foreach ($collection as $item)
132 {
133 $result->addItem(self::convertFromOrm($item));
134 }
135
136 return $result;
137 }
138}