1 | <?php |
||
19 | abstract class Component |
||
20 | { |
||
21 | /** |
||
22 | * Array of Components. |
||
23 | * |
||
24 | * @var Component[] |
||
25 | */ |
||
26 | protected $components = []; |
||
27 | |||
28 | /** |
||
29 | * The order in which the components will be rendered during build. |
||
30 | * |
||
31 | * Not defined components will be appended at the end. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $componentsBuildOrder = ['VTIMEZONE', 'DAYLIGHT', 'STANDARD']; |
||
36 | |||
37 | /** |
||
38 | * The type of the concrete Component. |
||
39 | * |
||
40 | * @abstract |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | abstract public function getType(); |
||
45 | |||
46 | /** |
||
47 | * Building the PropertyBag. |
||
48 | * |
||
49 | * @abstract |
||
50 | * |
||
51 | * @return PropertyBag |
||
52 | */ |
||
53 | abstract public function buildPropertyBag(); |
||
54 | |||
55 | /** |
||
56 | * Adds a Component. |
||
57 | * |
||
58 | * If $key is given, the component at $key will be replaced else the component will be append. |
||
59 | * |
||
60 | * @param Component $component The Component that will be added |
||
61 | * @param null $key The key of the Component |
||
62 | */ |
||
63 | 8 | public function addComponent(self $component, $key = null) |
|
64 | { |
||
65 | 8 | if (null == $key) { |
|
66 | 7 | $this->components[] = $component; |
|
67 | } else { |
||
68 | 1 | $this->components[$key] = $component; |
|
69 | } |
||
70 | 8 | } |
|
71 | |||
72 | /** |
||
73 | * Renders an array containing the lines of the iCal file. |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | 17 | public function build() |
|
78 | { |
||
79 | 17 | $lines = []; |
|
80 | |||
81 | 17 | $lines[] = sprintf('BEGIN:%s', $this->getType()); |
|
82 | |||
83 | /** @var $property Property */ |
||
84 | 17 | foreach ($this->buildPropertyBag() as $property) { |
|
85 | 17 | foreach ($property->toLines() as $l) { |
|
86 | 17 | $lines[] = $l; |
|
87 | } |
||
88 | } |
||
89 | |||
90 | 17 | $this->buildComponents($lines); |
|
91 | |||
92 | 17 | $lines[] = sprintf('END:%s', $this->getType()); |
|
93 | |||
94 | 17 | $ret = []; |
|
95 | |||
96 | 17 | foreach ($lines as $line) { |
|
97 | 17 | foreach (ComponentUtil::fold($line) as $l) { |
|
98 | 17 | $ret[] = $l; |
|
99 | } |
||
100 | } |
||
101 | |||
102 | 17 | return $ret; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Renders the output. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | 17 | public function render() |
|
114 | |||
115 | /** |
||
116 | * Renders the output when treating the class as a string. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 1 | public function __toString() |
|
124 | |||
125 | /** |
||
126 | * @param $lines |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 17 | private function buildComponents(array &$lines) |
|
161 | |||
162 | /** |
||
163 | * @param array $lines |
||
164 | * @param Component $component |
||
165 | */ |
||
166 | 8 | private function addComponentLines(array &$lines, self $component) |
|
172 | } |
||
173 |