Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
parser.php
1<?php
2
3
5
6
7use Generator;
8
9class Parser
10{
14 private $content;
18 private $linesGenerator;
22 private $component;
23
28 public static function createInstance(string $content): Parser
29 {
30 return new self($content);
31 }
32
37 public function __construct(string $content)
38 {
39 $this->content = $content;
40 }
41
45 public function parse(): Parser
46 {
47 $this->linesGenerator = $this->getLinesGenerator();
48 $this->component = $this->handle();
49
50 return $this;
51 }
52
56 private function getLinesGenerator(): ?Generator
57 {
58 $tmp = explode("\r\n", $this->content);
59
60 for ($i = 0, $length = count($tmp); $i < $length; $i++)
61 {
62 $line = rtrim($tmp[$i]);
63
64 while (isset($tmp[$i + 1]) && mb_strlen($tmp[$i + 1]) > 0 && ($tmp[$i + 1][0] === ' ' || $tmp[$i + 1][0] === "\t" ))
65 {
66 $line .= rtrim(mb_substr($tmp[++$i],1));
67 }
68
69 yield $line;
70 }
71
72 return null;
73 }
74
78 private function handle(): ?ParserComponent
79 {
80 $componentName = '';
81 $properties = [];
82 $componentsCollection = new ComponentsCollection();
83
84 while ($str = $this->linesGenerator->current())
85 {
86 $line = Line::createInstance($str);
87 $line->parse();
88
89 if ($line->isBegin())
90 {
91 if ($componentName)
92 {
93 $componentsCollection->add($this->handle());
94 }
95 else
96 {
97 $componentName = mb_strtolower($line->getValue());
98 }
99 }
100 elseif ($line->isEnd())
101 {
102 return FactoryComponents::createInstance($componentName)
103 ->createComponent($properties, $componentsCollection)
104 ->getComponent();
105 }
106 elseif (in_array($line->getName(), ['attendee', 'attach']))
107 {
108 $properties[$line->getName()][] = ParserPropertyType::createInstance($line->getName())
109 ->addParameters($line->getParams())
110 ->setValue($line->getValue());
111 }
112 else
113 {
114 $properties[$line->getName()] = ParserPropertyType::createInstance($line->getName())
115 ->addParameters($line->getParams())
116 ->setValue($line->getValue());
117 }
118
119 $this->linesGenerator->next();
120 }
121
122 return null;
123 }
124
128 public function getCalendarComponent(): ?Calendar
129 {
130 return ($this->component instanceof Calendar)
131 ? $this->component
132 : null
133 ;
134 }
135
136// private static function _ValidUtf8( $data ) {
137// $rx = '[\xC0-\xDF]([^\x80-\xBF]|$)';
138// $rx .= '|[\xE0-\xEF].{0,1}([^\x80-\xBF]|$)';
139// $rx .= '|[\xF0-\xF7].{0,2}([^\x80-\xBF]|$)';
140// $rx .= '|[\xF8-\xFB].{0,3}([^\x80-\xBF]|$)';
141// $rx .= '|[\xFC-\xFD].{0,4}([^\x80-\xBF]|$)';
142// $rx .= '|[\xFE-\xFE].{0,5}([^\x80-\xBF]|$)';
143// $rx .= '|[\x00-\x7F][\x80-\xBF]';
144// $rx .= '|[\xC0-\xDF].[\x80-\xBF]';
145// $rx .= '|[\xE0-\xEF]..[\x80-\xBF]';
146// $rx .= '|[\xF0-\xF7]...[\x80-\xBF]';
147// $rx .= '|[\xF8-\xFB]....[\x80-\xBF]';
148// $rx .= '|[\xFC-\xFD].....[\x80-\xBF]';
149// $rx .= '|[\xFE-\xFE]......[\x80-\xBF]';
150// $rx .= '|^[\x80-\xBF]';
151//
152// return ( ! (bool) preg_match('!'.$rx.'!', $data) );
153// }
154}
static createInstance(string $line)
Definition line.php:20
static createInstance(string $content)
Definition parser.php:28