1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use Exception; |
8
|
|
|
use Spatie\Enum\Enum; |
9
|
|
|
use Spatie\IcalendarGenerator\Components\Component; |
10
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\DateTimePropertyType; |
11
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\PropertyType; |
12
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\TextPropertyType; |
13
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\UriPropertyType; |
14
|
|
|
|
15
|
|
|
final class ComponentPayload |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
private $type; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
private $properties = []; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
private $subComponents = []; |
25
|
|
|
|
26
|
|
|
public static function create(string $type): ComponentPayload |
27
|
|
|
{ |
28
|
|
|
return new self($type); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __construct(string $type) |
32
|
|
|
{ |
33
|
|
|
$this->type = $type; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function property(PropertyType $property, array $parameters = null): ComponentPayload |
37
|
|
|
{ |
38
|
|
|
$property->addParameters($parameters ?? []); |
39
|
|
|
|
40
|
|
|
$this->properties[] = $property; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array|string $names |
47
|
|
|
* @param \DateTimeInterface|null $value |
48
|
|
|
* @param bool $withTime |
49
|
|
|
* @param bool $withTimeZone |
50
|
|
|
* |
51
|
|
|
* @return \Spatie\IcalendarGenerator\ComponentPayload |
52
|
|
|
*/ |
53
|
|
|
public function dateTimeProperty( |
54
|
|
|
$names, |
55
|
|
|
?DateTimeInterface $value, |
56
|
|
|
bool $withTime = false, |
57
|
|
|
bool $withTimeZone = false |
58
|
|
|
): ComponentPayload { |
59
|
|
|
if ($value === null) { |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->property(new DateTimePropertyType($names, $value, $withTime, $withTimeZone)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array|string $names |
68
|
|
|
* @param string|\Spatie\Enum\Enum|null $value |
69
|
|
|
* |
70
|
|
|
* @param bool $disableEscaping |
71
|
|
|
* |
72
|
|
|
* @return \Spatie\IcalendarGenerator\ComponentPayload |
73
|
|
|
*/ |
74
|
|
|
public function textProperty( |
75
|
|
|
$names, |
76
|
|
|
?string $value, |
77
|
|
|
bool $disableEscaping = false |
78
|
|
|
): ComponentPayload { |
79
|
|
|
if ($value === null) { |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($value instanceof Enum) { |
84
|
|
|
$value = (string) $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->property(new TextPropertyType($names, $value, $disableEscaping)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array|string $names |
92
|
|
|
* @param string|null $value |
93
|
|
|
* |
94
|
|
|
* @return \Spatie\IcalendarGenerator\ComponentPayload |
95
|
|
|
*/ |
96
|
|
|
public function uriProperty($names, ?string $value): ComponentPayload |
97
|
|
|
{ |
98
|
|
|
if ($value === null) { |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return filter_var($value, FILTER_VALIDATE_URL) ? $this->property(new UriPropertyType($names, $value)) : $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function subComponent(Component ...$components): ComponentPayload |
106
|
|
|
{ |
107
|
|
|
foreach ($components as $component) { |
108
|
|
|
$this->subComponents[] = $component; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function when(bool $condition, Closure $closure): ComponentPayload |
115
|
|
|
{ |
116
|
|
|
if ($condition) { |
117
|
|
|
$closure($this); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getType(): string |
124
|
|
|
{ |
125
|
|
|
return $this->type; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getProperties(): array |
129
|
|
|
{ |
130
|
|
|
return $this->properties; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getProperty(string $name): PropertyType |
134
|
|
|
{ |
135
|
|
|
$filteredProperties = array_filter( |
136
|
|
|
$this->properties, |
137
|
|
|
function (PropertyType $property) use ($name) { |
138
|
|
|
return in_array($name, $property->getNames()); |
139
|
|
|
} |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$properties = array_values($filteredProperties); |
143
|
|
|
|
144
|
|
|
if (count($properties) === 0) { |
145
|
|
|
throw new Exception("Property `{$name}` does not exist in the payload"); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $properties[0]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getSubComponents(): array |
152
|
|
|
{ |
153
|
|
|
return $this->subComponents; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|