Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
content.php
1<?php
3
7
13{
14 const TEXT = 0;
15 const EMAIL = 1;
16 const PHONE = 2;
17 const USER = 3;
18 const HTML_LAYOUT = 4;
19
21 private $data = [];
22
24 private $callbacks = [];
25
32 public static function encodeText($text)
33 {
34 return str_rot13(mb_strtoupper(trim($text)));
35 }
36
42 public function getString()
43 {
44 foreach ($this->callbacks as $callback)
45 {
46 call_user_func_array($callback, [$this]);
47 }
48
49 return implode(' ', array_map([$this, 'encodeText'], $this->data));
50 }
51
59 public function addCallback($callback)
60 {
61 if (!is_callable($callback))
62 {
63 throw new ArgumentException('Parameter \'callback\' does not callable.');
64 }
65 $this->callbacks[] = $callback;
66 return $this;
67 }
68
74 public function clear()
75 {
76 $this->data = array();
77 return $this;
78 }
79
88 public function add($value, $typeId = self::TEXT, $length = null)
89 {
90 switch ($typeId)
91 {
92 case self::EMAIL:
93 $this->addEmail($value);
94 break;
95
96 case self::PHONE:
97 $this->addPhone($value);
98 break;
99
100 case self::USER:
101 $this->addUserByID($value);
102 break;
103
105 $this->addHtmlLayout($value, $length);
106 break;
107
108 case self::TEXT:
109 default:
110 $this->addText($value, $length);
111 }
112
113 return $this;
114 }
115
125 public function addField(array $fields, $name, $typeId = self::TEXT, $length = null)
126 {
127 $value = isset($fields[$name]) ? $fields[$name] : '';
128 return $this->add($value, $typeId, $length);
129 }
130
138 public function addText($text, $length = null)
139 {
140 if(!is_string($text))
141 {
142 $text = (string) $text;
143 }
144
145 $text = trim($text);
146 if($length > 0)
147 {
148 $text = mb_substr($text, 0, $length);
149 }
150
151 if($text !== '' && !in_array($text, $this->data))
152 {
153 $this->data[] = $text;
154 }
155
156 return $this;
157 }
158
166 public function addHtmlLayout($layout, $length = null)
167 {
168 return $this->addText(self::convertBodyHtmlToText($layout), $length);
169 }
170
178 public function addRecipient($code, $typeId)
179 {
180 $code = Recipient\Normalizer::normalize($code, $typeId);
181 if($code && Recipient\Validator::validate($code, $typeId))
182 {
183 $this->addText($code);
184 }
185
186 return $this;
187 }
188
195 public function addEmail($email)
196 {
197 return $this->addRecipient($email, Recipient\Type::EMAIL);
198 }
199
206 public function addPhone($phone)
207 {
208 return $this->addRecipient($phone, Recipient\Type::PHONE);
209 }
210
217 public function addUserById($userID)
218 {
219 $userData = UserTable::getRow([
220 'select' => ['ID', 'LOGIN', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'TITLE'],
221 'filter' => ['=ID'=> $userID]
222 ]);
223
224 if(is_array($userData))
225 {
226 $value = \CUser::formatName(\CSite::getNameFormat(), $userData, true, false);
227 $this->addText($value);
228 }
229
230 return $this;
231 }
232
233 protected static function convertBodyHtmlToText($body)
234 {
235 // remove tags
236 $innerBody = preg_replace('/<(script|iframe|style)(.*?)>(.*?)(<\\/\\1.*?>)/is', '', $body);
237 $body = $innerBody ?: $body;
238
239 // get <body> inner html if exists
240 $innerBody = trim(preg_replace('/(.*?<body[^>]*>)(.*?)(<\/body>.*)/is', '$2', $body));
241 $body = $innerBody ?: $body;
242
243 // modify links to text version
244 $body = preg_replace_callback(
245 "/<a[^>]*?>([^>]*?)<\\/a>/i",
246 function ($matches)
247 {
248 return $matches[1];
249 },
250 $body
251 );
252
253 // change <br> to new line
254 $body = preg_replace('/<br(\s*)?\/?>/i', "\n", $body);
255
256 // remove tags
257 $body = strip_tags($body);
258
259 // format text to the left side
260 $lines = [];
261 foreach (explode("\n", trim($body)) as $line)
262 {
263 $lines[] = trim($line);
264 }
265
266 // remove redundant new lines
267 $body = preg_replace("/[\\n]{2,}/", " ", implode(" ", $lines));
268
269 // remove redundant spaces
270 $body = preg_replace("/[ \\t]{2,}/", " ", $body);
271
272 // decode html-entities
273 return html_entity_decode($body);
274 }
275}
static validate($code, $typeId=Type::EMAIL)
Definition validator.php:27
addField(array $fields, $name, $typeId=self::TEXT, $length=null)
Definition content.php:125
addHtmlLayout($layout, $length=null)
Definition content.php:166
add($value, $typeId=self::TEXT, $length=null)
Definition content.php:88
addRecipient($code, $typeId)
Definition content.php:178
addText($text, $length=null)
Definition content.php:138
static convertBodyHtmlToText($body)
Definition content.php:233