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