Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dispute.php
1<?php
2
4
10
11abstract class Dispute
12{
14 protected $currentRegion;
15
16 public function __construct()
17 {
18 $this->currentRegion = CurrentRegionFinderService::getInstance()->getRegion();
19 }
20
24 public function adjustLocation(Location $location): void
25 {
26 $info = $this->getInfo();
27 if (!$info)
28 {
29 return;
30 }
31
36 $locationFieldCollection = $location->getFieldCollection();
37
38 if (isset($info['country']))
39 {
40 $this->adjustLocationField($locationFieldCollection, Type::COUNTRY, $info['country']);
41 }
42 if (isset($info['state']))
43 {
44 $this->adjustLocationField($locationFieldCollection, Type::ADM_LEVEL_1, $info['state']);
45 }
46
50 $address = $location->getAddress();
51 if ($address)
52 {
54 $addressFieldCollection = $address->getFieldCollection();
55
56 if (isset($info['country']))
57 {
58 $this->adjustAddressField(
59 $addressFieldCollection,
60 Entity\Address\FieldType::COUNTRY,
61 $info['country']
62 );
63 }
64
65 if (isset($info['state']))
66 {
67 $this->adjustAddressField(
68 $addressFieldCollection,
69 Entity\Address\FieldType::ADM_LEVEL_1,
70 $info['state']
71 );
72 }
73
75 $item = $addressFieldCollection->getItemByType(Entity\Address\FieldType::POSTAL_CODE);
76 if ($item)
77 {
78 $item->setValue('');
79 }
80 }
81 }
82
86 public function adjustAutocompleteItem(array &$item): void
87 {
88 $info = $this->getInfo();
89 if (!$info)
90 {
91 return;
92 }
93
94 unset($item['countrycode']);
95 unset($item['postcode']);
96
97 if (isset($info['country']))
98 {
99 $item['country'] = $info['country'];
100 }
101
102 if (isset($info['state']))
103 {
104 $item['state'] = $info['state'];
105 }
106 }
107
112 abstract protected function getInfo(): array;
113
119 private function adjustLocationField(Entity\Location\FieldCollection $fieldCollection, int $type, $value)
120 {
121 $item = $fieldCollection->getItemByType($type);
122 if ($item)
123 {
124 $item->setValue($value);
125 }
126 else
127 {
128 $fieldCollection->addItem(new Entity\Location\Field($type, $value));
129 }
130 }
131
137 private function adjustAddressField(Entity\Address\FieldCollection $fieldCollection, int $type, $value)
138 {
139 $item = $fieldCollection->getItemByType($type);
140 if ($item)
141 {
142 $item->setValue($value);
143 }
144 else
145 {
146 $fieldCollection->addItem(new Entity\Address\Field($type, $value));
147 }
148 }
149}