1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
dataconverter.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\TradingPlatform\Vk\Feed\Data\Converters;
4
5use Bitrix\Main\Localization\Loc;
6use Bitrix\Main\Text\Encoding;
7
8Loc::loadMessages(__FILE__);
9
10abstract class DataConverter
11{
12 const PAD_STRING = '_';
13 const END_STRING = ' ...';
14
15 protected $exportId;
16
17 abstract public function convert($data);
18
19 private static $specialCharsLength = [
20// commented chars has no effect on the length
21 '"' => [
22 'count' => 5,
23 'regexp' => '"',
24 ],
25 '&' => [
26 'count' => 5,
27 'regexp' => '&',
28 ],
29 '\\' => [
30 'count' => 6,
31 'regexp' => '\\\\',
32 ],
33 '\'' => [
34 'count' => 5,
35 'regexp' => '\\\'',
36 ],
37 '>' => [
38 'count' => 4,
39 'regexp' => '>',
40 ],
41 '<' => [
42 'count' => 4,
43 'regexp' => '<',
44 ],
45// ',' => [
46// 'count' => 5,
47// 'regexp' => ',',
48// ],
49 '!' => [
50 'count' => 5,
51 'regexp' => '!',
52 ],
53 '$' => [
54 'count' => 6,
55 'regexp' => '$',
56 ],
57// '№' => [
58// 'count' => 7,
59// 'regexp' => '№',
60// ],
61 ];
62
63 protected static function matchLength($string)
64 {
65// base length
66 $length = mb_strlen($string);
67
68// construct regexp for find all special chars
69 $regexp = '';
70 foreach (self::$specialCharsLength as $char)
71 {
72 $regexp .= $char['regexp'];
73 }
74 $regexp = '/[' . $regexp . ']/';
75 preg_match_all($regexp, $string, $matches);
76
77// correct length by special chars
78 foreach ($matches[0] as $m)
79 {
80 $length += self::$specialCharsLength[$m]['count'] - 1; //once already matches
81 }
82
83 return $length;
84 }
85
94 protected static function extendString($string, $currLength, $needLength)
95 {
96 if ($currLength >= $needLength)
97 {
98 return $string;
99 }
100
101 return self::mb_str_pad($string, $needLength, self::PAD_STRING);
102 }
103
104 protected static function mb_str_pad($string, $padLength, $padString = " ", $padType = STR_PAD_RIGHT)
105 {
106 if (method_exists("\Bitrix\Main\Text\UtfSafeString", "pad"))
107 {
108 return \Bitrix\Main\Text\UtfSafeString::pad($string, $padLength, $padString, $padType);
109 }
110 else
111 {
112 $newPadLength = strlen($string) - mb_strlen($string) + $padLength;
113
114 return str_pad($string, $newPadLength, $padString, $padType);
115 }
116 }
117
126 protected static function reduceString($string, $currLength, $needLength)
127 {
128 if ($currLength <= $needLength)
129 {
130 return $string;
131 }
132
133 $cropLength = $currLength - $needLength + mb_strlen(self::END_STRING);
134 $substrLength = mb_strlen($string) - $cropLength;
135
136// if so more spechialchars, can't match correct new length.
137// Use hack and find minimal 100% correct length
138 if($substrLength <= 0 )
139 {
140 $maxSpecialCharLength = 1;
141 foreach(self::$specialCharsLength as $char)
142 {
143 $maxSpecialCharLength = max($maxSpecialCharLength, $char['count']);
144 }
145
146 $substrLength = floor($needLength / $maxSpecialCharLength);
147 }
148
149 return mb_substr($string, 0, $substrLength).self::END_STRING;
150 }
151}
152
153
static extendString($string, $currLength, $needLength)
Определения dataconverter.php:94
static mb_str_pad($string, $padLength, $padString=" ", $padType=STR_PAD_RIGHT)
Определения dataconverter.php:104
static reduceString($string, $currLength, $needLength)
Определения dataconverter.php:126
$data['IS_AVAILABLE']
Определения .description.php:13
$matches
Определения index.php:22