Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dataconverter.php
1<?php
2
4
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 protected static function convertToUtf8($value): string
20 {
21 return Encoding::convertEncoding($value, SITE_CHARSET, 'UTF-8');
22 }
23
24 private static $specialCharsLength = [
25// commented chars has no effect on the length
26 '"' => [
27 'count' => 5,
28 'regexp' => '"',
29 ],
30 '&' => [
31 'count' => 5,
32 'regexp' => '&',
33 ],
34 '\\' => [
35 'count' => 6,
36 'regexp' => '\\\\',
37 ],
38 '\'' => [
39 'count' => 5,
40 'regexp' => '\\\'',
41 ],
42 '>' => [
43 'count' => 4,
44 'regexp' => '>',
45 ],
46 '<' => [
47 'count' => 4,
48 'regexp' => '<',
49 ],
50// ',' => [
51// 'count' => 5,
52// 'regexp' => ',',
53// ],
54 '!' => [
55 'count' => 5,
56 'regexp' => '!',
57 ],
58 '$' => [
59 'count' => 6,
60 'regexp' => '$',
61 ],
62// '' => [
63// 'count' => 7,
64// 'regexp' => '',
65// ],
66 ];
67
68 protected static function matchLength($string)
69 {
70// base length
71 $length = mb_strlen($string);
72
73// construct regexp for find all special chars
74 $regexp = '';
75 foreach (self::$specialCharsLength as $char)
76 {
77 $regexp .= $char['regexp'];
78 }
79 $regexp = '/[' . $regexp . ']/';
80 preg_match_all($regexp, $string, $matches);
81
82// correct length by special chars
83 foreach ($matches[0] as $m)
84 {
85 $length += self::$specialCharsLength[$m]['count'] - 1; //once already matches
86 }
87
88 return $length;
89 }
90
99 protected static function extendString($string, $currLength, $needLength)
100 {
101 if ($currLength >= $needLength)
102 {
103 return $string;
104 }
105
106 return self::mb_str_pad($string, $needLength, self::PAD_STRING);
107 }
108
109 protected static function mb_str_pad($string, $padLength, $padString = " ", $padType = STR_PAD_RIGHT)
110 {
111 if (method_exists("\Bitrix\Main\Text\UtfSafeString", "pad"))
112 {
113 return \Bitrix\Main\Text\UtfSafeString::pad($string, $padLength, $padString, $padType);
114 }
115 else
116 {
117 $newPadLength = \Bitrix\Main\Text\BinaryString::getLength($string) - mb_strlen($string) + $padLength;
118
119 return str_pad($string, $newPadLength, $padString, $padType);
120 }
121 }
122
131 protected static function reduceString($string, $currLength, $needLength)
132 {
133 if ($currLength <= $needLength)
134 {
135 return $string;
136 }
137
138 $cropLength = $currLength - $needLength + mb_strlen(self::END_STRING);
139 $substrLength = mb_strlen($string) - $cropLength;
140
141// if so more spechialchars, can't match correct new length.
142// Use hack and find minimal 100% correct length
143 if($substrLength <= 0 )
144 {
145 $maxSpecialCharLength = 1;
146 foreach(self::$specialCharsLength as $char)
147 {
148 $maxSpecialCharLength = max($maxSpecialCharLength, $char['count']);
149 }
150
151 $substrLength = floor($needLength / $maxSpecialCharLength);
152 }
153
154 return mb_substr($string, 0, $substrLength).self::END_STRING;
155 }
156}
157
158
static loadMessages($file)
Definition loc.php:64
static mb_str_pad($string, $padLength, $padString=" ", $padType=STR_PAD_RIGHT)