1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator\Components; |
4
|
|
|
|
5
|
|
|
use Spatie\IcalendarGenerator\Builders\ComponentBuilder; |
6
|
|
|
use Spatie\IcalendarGenerator\ComponentPayload; |
7
|
|
|
use Spatie\IcalendarGenerator\Exceptions\InvalidComponent; |
8
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\PropertyType; |
9
|
|
|
|
10
|
|
|
abstract class Component |
11
|
|
|
{ |
12
|
|
|
/** @var \Spatie\IcalendarGenerator\PropertyTypes\PropertyType[] */ |
13
|
|
|
private $appendedProperties = []; |
14
|
|
|
|
15
|
|
|
/** @var \Spatie\IcalendarGenerator\Components\Component[] */ |
16
|
|
|
private $appendedSubComponents = []; |
17
|
|
|
|
18
|
|
|
abstract public function getComponentType(): string; |
19
|
|
|
|
20
|
|
|
abstract public function getRequiredProperties(): array; |
21
|
|
|
|
22
|
|
|
abstract protected function payload(): ComponentPayload; |
23
|
|
|
|
24
|
|
|
public function resolvePayload(): ComponentPayload |
25
|
|
|
{ |
26
|
|
|
$payload = $this->payload(); |
27
|
|
|
|
28
|
|
|
foreach ($this->appendedProperties as $appendedProperty) { |
29
|
|
|
$payload->property($appendedProperty); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$payload->subComponent(...$this->appendedSubComponents); |
33
|
|
|
|
34
|
|
|
return $payload; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function toString(): string |
38
|
|
|
{ |
39
|
|
|
$payload = $this->resolvePayload(); |
40
|
|
|
|
41
|
|
|
$this->ensureRequiredPropertiesAreSet($payload); |
42
|
|
|
|
43
|
|
|
$builder = new ComponentBuilder($payload); |
44
|
|
|
|
45
|
|
|
return $builder->build(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function appendProperty(PropertyType $property): Component |
49
|
|
|
{ |
50
|
|
|
$this->appendedProperties[] = $property; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function appendSubComponent(Component $component): Component |
56
|
|
|
{ |
57
|
|
|
$this->appendedSubComponents[] = $component; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function ensureRequiredPropertiesAreSet(ComponentPayload $componentPayload) |
63
|
|
|
{ |
64
|
|
|
$providedProperties = []; |
65
|
|
|
|
66
|
|
|
/** @var \Spatie\IcalendarGenerator\PropertyTypes\PropertyType $property */ |
67
|
|
|
foreach ($componentPayload->getProperties() as $property) { |
68
|
|
|
$providedProperties = array_merge( |
69
|
|
|
$providedProperties, |
70
|
|
|
$property->getNames() |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$requiredProperties = $this->getRequiredProperties(); |
75
|
|
|
|
76
|
|
|
$intersection = array_intersect($requiredProperties, $providedProperties); |
77
|
|
|
|
78
|
|
|
if (count($intersection) !== count($requiredProperties)) { |
79
|
|
|
$missingProperties = array_diff($requiredProperties, $intersection); |
80
|
|
|
|
81
|
|
|
throw InvalidComponent::requiredPropertyMissing($missingProperties, $this); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|