-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathBaseType.php
More file actions
180 lines (144 loc) · 3.97 KB
/
Copy pathBaseType.php
File metadata and controls
180 lines (144 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Spatie\SchemaOrg;
use ArrayAccess;
use DateTime;
use DateTimeInterface;
use JsonSerializable;
use ReflectionClass;
use Spatie\SchemaOrg\Exceptions\InvalidProperty;
abstract class BaseType implements Type, ArrayAccess, JsonSerializable
{
/** @var array */
protected $properties = [];
/** @var string */
protected $nonce = '';
public function getContext(): string
{
return 'https://schema.org';
}
public function getType(): string
{
return (new ReflectionClass($this))->getShortName();
}
public function setProperty(string $property, $value)
{
if ($value !== null && $value !== '') {
$this->properties[$property] = $value;
}
return $this;
}
public function addProperties(array $properties)
{
foreach ($properties as $property => $value) {
$this->setProperty($property, $value);
}
return $this;
}
public function if($condition, $callback)
{
if ($condition) {
$callback($this);
}
return $this;
}
public function setNonce(string $nonce)
{
$this->nonce = $nonce;
return $this;
}
public function getProperty(string $property, $default = null)
{
return $this->properties[$property] ?? $default;
}
public function getProperties(): array
{
return $this->properties;
}
/**
* @return ReferencedType|static
*/
public function referenced()
{
return new ReferencedType($this);
}
public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->properties);
}
public function offsetGet($offset): mixed
{
return $this->getProperty($offset);
}
public function offsetSet($offset, $value): void
{
$this->setProperty($offset, $value);
}
public function offsetUnset($offset): void
{
unset($this->properties[$offset]);
}
public function toArray(): array
{
$this->serializeIdentifier();
$properties = $this->serializeProperty($this->getProperties());
return [
'@context' => $this->getContext(),
'@type' => $this->getType(),
] + $properties;
}
protected function serializeProperty($property)
{
if (is_array($property)) {
return array_map([$this, 'serializeProperty'], $property);
}
if ($property instanceof Type) {
$property = $property->toArray();
unset($property['@context']);
}
if ($property instanceof DateTimeInterface) {
$property = $property->format(DateTime::ATOM);
}
if (is_object($property) && method_exists($property, '__toString')) {
$property = (string) $property;
}
if (is_object($property)) {
throw new InvalidProperty();
}
return $property;
}
protected function serializeIdentifier()
{
if (
isset($this['identifier'])
&& ! $this['identifier'] instanceof Type
) {
$this->setProperty('@id', $this['identifier']);
unset($this['identifier']);
}
}
public function nonceAttr(): string
{
if ($this->nonce) {
$attr = ' nonce="'.$this->nonce.'"';
} else {
$attr = '';
}
return $attr;
}
public function toScript(): string
{
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG).'</script>';
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
public function __call(string $method, array $arguments)
{
return $this->setProperty($method, $arguments[0] ?? '');
}
public function __toString(): string
{
return $this->toScript();
}
}