1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator\Builders; |
4
|
|
|
|
5
|
|
|
use Spatie\IcalendarGenerator\ComponentPayload; |
6
|
|
|
|
7
|
|
|
final class ComponentBuilder |
8
|
|
|
{ |
9
|
|
|
/** @var \Spatie\IcalendarGenerator\ComponentPayload */ |
10
|
|
|
private $componentPayload; |
11
|
|
|
|
12
|
|
|
public function __construct(ComponentPayload $componentPayload) |
13
|
|
|
{ |
14
|
|
|
$this->componentPayload = $componentPayload; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function build(): string |
18
|
|
|
{ |
19
|
|
|
$lines = []; |
20
|
|
|
|
21
|
|
|
foreach ($this->buildComponent() as $line) { |
22
|
|
|
$lines = array_merge($lines, $this->chipLine($line)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return implode("\r\n", $lines); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function buildComponent(): array |
29
|
|
|
{ |
30
|
|
|
$lines[] = "BEGIN:V{$this->componentPayload->getType()}"; |
|
|
|
|
31
|
|
|
|
32
|
|
|
$lines = array_merge( |
33
|
|
|
$lines, |
34
|
|
|
$this->buildProperties(), |
35
|
|
|
$this->buildSubComponents() |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$lines[] = "END:V{$this->componentPayload->getType()}"; |
39
|
|
|
|
40
|
|
|
return $lines; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function buildProperties(): array |
44
|
|
|
{ |
45
|
|
|
$lines = []; |
46
|
|
|
|
47
|
|
|
foreach ($this->componentPayload->getProperties() as $property) { |
48
|
|
|
$builder = new PropertyBuilder($property); |
49
|
|
|
|
50
|
|
|
$lines = array_merge( |
51
|
|
|
$lines, |
52
|
|
|
$builder->build() |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $lines; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function buildSubComponents(): array |
60
|
|
|
{ |
61
|
|
|
$lines = []; |
62
|
|
|
|
63
|
|
|
/** @var \Spatie\IcalendarGenerator\Components\Component $component */ |
64
|
|
|
foreach ($this->componentPayload->getSubComponents() as $component) { |
65
|
|
|
$builder = new ComponentBuilder($component->resolvePayload()); |
66
|
|
|
|
67
|
|
|
$lines = array_merge( |
68
|
|
|
$lines, |
69
|
|
|
$builder->buildComponent() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $lines; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function chipLine(string $line): array |
77
|
|
|
{ |
78
|
|
|
$chippedLines = []; |
79
|
|
|
|
80
|
|
|
while (strlen($line) > 0) { |
81
|
|
|
if (strlen($line) > 75) { |
82
|
|
|
$chippedLines[] = mb_strcut($line, 0, 75, 'utf-8'); |
83
|
|
|
$line = ' '.mb_strcut($line, 75, strlen($line), 'utf-8'); |
84
|
|
|
} else { |
85
|
|
|
$chippedLines[] = $line; |
86
|
|
|
|
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $chippedLines; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.