Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stringconverter.php
1<?php
2
4
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 {
48 $delimiter = ', ';
49 break;
51 $delimiter = "\n";
52 break;
54 $delimiter = '<br />';
55 break;
56 }
57
59 $address,
60 $format->getTemplate(),
61 $contentType,
62 $delimiter,
63 $format
64 );
65 }
66 elseif($strategyType === self::STRATEGY_TYPE_FIELD_SORT)
67 {
68 $result = self::convertToStringByField($address, $format, $contentType);
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,
115 Format\Template $template,
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)
static convertToStringByField(Address $address, Format $format, string $contentType, callable $fieldSorter=null)
static convertToString(Address $address, Format $format, string $strategyType, string $contentType)
getTemplate(string $type=TemplateType::DEFAULT)
Definition format.php:154