Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sliceconverter.php
1<?
9
12
13Loc::loadMessages(__FILE__);
14
15class SliceConverter implements IConverter
16{
17 CONST SLICE_SECTION_ID = 'BX_BLOCK_EDITOR_EDITABLE_SECTION';
18
25 public static function isValid($string)
26 {
27 $result = true;
28 $string = mb_strtoupper($string);
29 if(mb_strpos($string, '<!--START '.static::SLICE_SECTION_ID.'/') === false)
30 {
31 $result = false;
32 }
33 if(mb_strpos($string, '<!--END '.static::SLICE_SECTION_ID.'/') === false)
34 {
35 $result = false;
36 }
37
38 return $result;
39 }
40
47 public static function toArray($string)
48 {
49 $blockContent = new BlockContent();
50 $pattern = '#<!--START '
51 . static::SLICE_SECTION_ID . '/([\w]+?)/([\w]+?)/-->'
52 . '([\s\S,\n]*?)'
53 . '<!--END ' . static::SLICE_SECTION_ID . '[/\w]+?-->#';
54
55 $matches = array();
56 if(preg_match_all($pattern, $string, $matches))
57 {
58 $matchesCount = count($matches[0]);
59 for($i = 0; $i < $matchesCount; $i++)
60 {
61 $section = trim($matches[1][$i]);
62 $place = trim($matches[2][$i]);
63 $value = trim($matches[3][$i]);
64
65 $blockContent->add($section, $place, $value);
66 }
67 }
68
69 return $blockContent;
70 }
71
78 public static function toString(BlockContent $content)
79 {
80 $result = '';
81 foreach ($content->getList() as $item)
82 {
83 $result .= '<!--START '
84 . static::SLICE_SECTION_ID . "/{$item['type']}/{$item['place']}/-->\n"
85 . $item['value'] . "\n"
86 . '<!--END '
87 . static::SLICE_SECTION_ID . "/{$item['type']}/{$item['place']}/-->\n";
88 }
89
90 return trim($result);
91 }
92
99 public static function sanitize($string)
100 {
101 if (!self::isValid($string))
102 {
103 return Block\Sanitizer::clean($string);
104 }
105
106 $content = self::toArray($string);
107 $list = $content->getList();
108 foreach ($list as $index => $item)
109 {
110 if ($item['type'] !== BlockContent::TYPE_BLOCKS)
111 {
112 continue;
113 }
114
115 $item['value'] = Block\Sanitizer::clean($item['value']);
116 $list[$index] = $item;
117 }
118
119 $content->setList($list);
120 return self::toString($content);
121 }
122}
static toString(BlockContent $content)
static loadMessages($file)
Definition loc.php:64