1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
stringconverter.php
См. документацию.
1<?php
2
3namespace Bitrix\Location\Entity\Address\Converter;
4
5use Bitrix\Location\Entity\Address;
6use Bitrix\Location\Entity\Format;
7use Bitrix\Main\ArgumentOutOfRangeException;
8
13final class StringConverter
14{
15 public const STRATEGY_TYPE_TEMPLATE = 'template';
16 public const STRATEGY_TYPE_TEMPLATE_COMMA = 'template_comma';
17 public const STRATEGY_TYPE_TEMPLATE_NL = 'template_nl';
18 public const STRATEGY_TYPE_TEMPLATE_BR = 'template_br';
19 public const STRATEGY_TYPE_FIELD_SORT = 'field_sort';
20 public const STRATEGY_TYPE_FIELD_TYPE = 'field_type';
21
22 public const CONTENT_TYPE_HTML = 'html';
23 public const CONTENT_TYPE_TEXT = 'text';
24
35 public static function convertToString(Address $address, Format $format, string $strategyType, string $contentType): string
36 {
37 if($strategyType === self::STRATEGY_TYPE_TEMPLATE
38 || $strategyType === self::STRATEGY_TYPE_TEMPLATE_COMMA
39 || $strategyType === self::STRATEGY_TYPE_TEMPLATE_NL
40 || $strategyType === self::STRATEGY_TYPE_TEMPLATE_BR
41 )
42 {
43 $delimiter = null;
44
45 switch ($strategyType)
46 {
47 case self::STRATEGY_TYPE_TEMPLATE_COMMA:
48 $delimiter = ', ';
49 break;
50 case self::STRATEGY_TYPE_TEMPLATE_NL:
51 $delimiter = "\n";
52 break;
53 case self::STRATEGY_TYPE_TEMPLATE_BR:
54 $delimiter = '<br />';
55 break;
56 }
57
59 $address,
60 $format->getTemplate(),
62 $delimiter,
63 $format
64 );
65 }
66 elseif($strategyType === self::STRATEGY_TYPE_FIELD_SORT)
67 {
69 }
70 elseif($strategyType === self::STRATEGY_TYPE_FIELD_TYPE)
71 {
72 $fieldSorter = static function(Format\Field $a, Format\Field $b): int
73 {
74 $aType = $a->getType();
75 $bType = $b->getType();
76
77 if($aType === 0)
78 {
79 $result = -1;
80 }
81 elseif ($bType === 0)
82 {
83 $result = 1;
84 }
85 else
86 {
87 $result = $aType - $bType;
88 }
89
90 return $result;
91 };
92
93 $result = self::convertToStringByField($address, $format, $contentType, $fieldSorter);
94 }
95 else
96 {
97 throw new ArgumentOutOfRangeException('strategyType');
98 }
99
100 return $result;
101 }
102
113 public static function convertToStringTemplate(
114 Address $address,
116 string $contentType,
117 string $delimiter = null,
118 Format $format = null
119 ): string
120 {
121 $needHtmlEncode = ($contentType === self::CONTENT_TYPE_HTML);
122
123 if ($delimiter === null)
124 {
125 $delimiter = $needHtmlEncode ? '<br />' : "\n";
126 }
127
128 $templateConverter = new StringTemplateConverter(
129 $template->getTemplate(),
130 $delimiter,
131 $needHtmlEncode,
132 $format
133 );
134
135 return $templateConverter->convert($address);
136 }
137
147 protected static function convertToStringByField(Address $address, Format $format, string $contentType, callable $fieldSorter = null): string
148 {
149 $result = '';
150 $fields = array_values($format->getFieldCollection()->getItems());
151
152 if($fieldSorter !== null)
153 {
154 usort($fields, $fieldSorter);
155 }
156
157 foreach($fields as $field)
158 {
159 $fieldValue = $address->getFieldValue($field->getType());
160
161 if($fieldValue === null)
162 {
163 continue;
164 }
165
166 if($contentType === self::CONTENT_TYPE_HTML)
167 {
168 $fieldValue = htmlspecialcharsbx($fieldValue);
169 }
170
171 if($result !== '')
172 {
173 $result .= $format->getDelimiter();
174 }
175
176 $result .= $fieldValue;
177 }
178
179 return $result;
180 }
181}
static convertToStringTemplate(Address $address, Format\Template $template, string $contentType, string $delimiter=null, Format $format=null)
Определения stringconverter.php:113
static convertToStringByField(Address $address, Format $format, string $contentType, callable $fieldSorter=null)
Определения stringconverter.php:147
static convertToString(Address $address, Format $format, string $strategyType, string $contentType)
Определения stringconverter.php:35
getFieldValue(int $type)
Определения address.php:197
getTemplate(string $type=TemplateType::DEFAULT)
Определения format.php:154
getFieldCollection()
Определения format.php:134
$template
Определения file_edit.php:49
$result
Определения get_property_values.php:14
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$contentType
Определения quickway.php:301
else $a
Определения template.php:137
$fields
Определения yandex_run.php:501