Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
line.php
1<?php
2
3
5
6
7class Line
8{
9 private const COMPONENT_PROPERTY_NAME_BEGIN = 'begin';
10 private const COMPONENT_PROPERTY_NAME_END = 'end';
11 private $name;
12 private $value;
13 private $params = [];
14 private $line;
15
20 public static function createInstance(string $line): Line
21 {
22 return new self($line);
23 }
24
29 public function __construct(string $line)
30 {
31 $this->line = $line;
32 }
33
34
38 public function parse(): Line
39 {
40 $line = $this->line;
41 $valuePos = (int) mb_strpos($line, ':');
42 $parts = explode(';', mb_substr($line, 0, $valuePos));
43 $name = mb_strtolower(array_shift($parts));
44 if ($name === 'attendee')
45 {
46 $valuePos = (int) mb_strrpos($this->line, ':');
47 }
48 $value = $this->getValueFromString($valuePos);
49
50 $params = [];
51 foreach($parts as $v)
52 {
53 [$k, $v] = explode('=', $v);
54 $params[mb_strtolower($k)] = trim($v, '"');
55 }
56
57 $this->value = $value;
58 $this->params = $params;
59 $this->name = $name;
60
61 return $this;
62 }
63
67 public function isBegin(): bool
68 {
69 return $this->name === self::COMPONENT_PROPERTY_NAME_BEGIN;
70 }
71
75 public function isEnd(): bool
76 {
77 return $this->name === self::COMPONENT_PROPERTY_NAME_END;
78 }
79
83 public function getValue(): string
84 {
85 return $this->value;
86 }
87
91 public function getName(): string
92 {
93 return $this->name;
94 }
95
99 public function getParams(): array
100 {
101 return $this->params;
102 }
103
107 public function getValueAsArray(): array
108 {
109 if (mb_strpos($this->value,",") !== false)
110 {
111 return explode(",",$this->value);
112 }
113
114 return [$this->value];
115 }
116
120 public function __toString(): string
121 {
122 return $this->getValue();
123 }
124
128 public function count(): int
129 {
130 return count($this->params);
131 }
132
137 private function getValueFromString(int $valuePos): string
138 {
139 $replacements = array('from'=>['\\,', '\\n', '\\;', '\\:', '\\"'], 'to'=>[',', "\n", ';', ':', '"']);
140 $tmp = trim(mb_substr($this->line, $valuePos+1));
141
142 return str_replace($replacements['from'], $replacements['to'], $tmp);
143 }
144}
static createInstance(string $line)
Definition line.php:20