Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
componentcreator.php
1<?php
2
3
5
6
9
11{
12 private $content;
13
14 public function __construct(Content $content)
15 {
16 $this->content = $content;
17 }
18
19 public function build(): string
20 {
21 $lines = [];
22
23 foreach ($this->buildComponent() as $line) {
24 $lines = array_merge($lines, $this->chipLine($line));
25 }
26
27 return implode("\r\n", $lines)."\r\n";
28 }
29
30 public function buildComponent(): array
31 {
32 $lines[] = "BEGIN:{$this->content->getType()}";
33
34 $lines = array_merge(
35 $lines,
36 $this->buildProperties(),
37 $this->buildSubComponents()
38 );
39
40 $lines[] = "END:{$this->content->getType()}";
41
42 return $lines;
43 }
44
45 private function buildProperties(): array
46 {
47 $lines = [];
48
49 foreach ($this->content->getProperties() as $key => $property)
50 {
51 $builder = new PropertyCreator($property);
52
53 $lines = array_merge(
54 $lines,
55 $builder->build()
56 );
57 }
58
59 return $lines;
60 }
61
62 private function buildSubComponents(): array
63 {
64 $lines = [];
65
66 foreach ($this->content->getSubComponents() as $component) {
67 $builder = new ComponentCreator($component->accessContent());
68
69 $lines = array_merge(
70 $lines,
71 $builder->buildComponent()
72 );
73 }
74
75 return $lines;
76 }
77
78 private function chipLine(string $line): array
79 {
80 $chippedLines = [];
81
82 while (strlen($line) > 0)
83 {
84 if (strlen($line) > 75)
85 {
86 $chippedLines[] = mb_strcut($line, 0, 75, 'utf-8');
87 $line = ' '.mb_strcut($line, 75, strlen($line), 'utf-8');
88 }
89 else
90 {
91 $chippedLines[] = $line;
92
93 break;
94 }
95 }
96
97 return $chippedLines;
98 }
99}