Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseconverter.php
1<?php
2
4
11
16abstract class BaseConverter
17{
18 protected $languageId = '';
19
23 public function __construct(string $languageId)
24 {
25 $this->languageId = $languageId;
26 }
27
32 abstract public function convert(array $data);
33
34 public function isPostCode(array $types)
35 {
36 return in_array('postal_code', $types);
37 }
38
44 public function convertTypes(array $types, string $typesClass): int
45 {
46 $result = $typesClass::UNKNOWN;
47
48 foreach($types as $type)
49 {
50 $result = PlaceTypeConverter::convert($type);
51
52 if(!$typesClass::isValueExist($result))
53 {
54 $result = $typesClass::UNKNOWN;
55 continue;
56 }
57
58 if($result !== $typesClass::UNKNOWN)
59 {
60 break;
61 }
62 }
63
64 return $result;
65 }
66
67 protected function createAddress(array $addressComponents): Address
68 {
69 $address = new Address($this->languageId);
70
71 foreach ($addressComponents as $item)
72 {
73 if ($type = $this->convertTypes($item['types'], FieldType::class))
74 {
75 $address->setFieldValue((int)$type, (string)$item['long_name']);
76 }
77 }
78
79 $format = FormatService::getInstance()->findDefault($this->languageId);
80
81 $addressLine1 = StringConverter::convertToStringTemplate(
82 $address,
83 $format->getTemplate(TemplateType::ADDRESS_LINE_1),
84 StringConverter::STRATEGY_TYPE_TEMPLATE,
85 StringConverter::CONTENT_TYPE_TEXT
86 );
87
88 if($addressLine1)
89 {
90 $address->setFieldValue(FieldType::ADDRESS_LINE_1, $addressLine1);
91 }
92
93 return $address;
94 }
95}