These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\SchemaOrg; |
||
4 | |||
5 | use DateTime; |
||
6 | use ReflectionClass; |
||
7 | use DateTimeInterface; |
||
8 | use Spatie\SchemaOrg\Exceptions\InvalidProperty; |
||
9 | |||
10 | View Code Duplication | abstract class BaseType implements Type, \ArrayAccess, \JsonSerializable |
|
11 | { |
||
12 | /** @var array */ |
||
13 | protected $properties = []; |
||
14 | |||
15 | public function getContext(): string |
||
16 | { |
||
17 | return 'http://schema.org'; |
||
18 | } |
||
19 | |||
20 | public function getType(): string |
||
21 | { |
||
22 | return (new ReflectionClass($this))->getShortName(); |
||
23 | } |
||
24 | |||
25 | public function setProperty(string $property, $value) |
||
26 | { |
||
27 | $this->properties[$property] = $value; |
||
28 | |||
29 | return $this; |
||
30 | } |
||
31 | |||
32 | public function addProperties(array $properties) |
||
33 | { |
||
34 | foreach ($properties as $property => $value) { |
||
35 | $this->setProperty($property, $value); |
||
36 | } |
||
37 | |||
38 | return $this; |
||
39 | } |
||
40 | |||
41 | public function if($condition, $callback) |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
42 | { |
||
43 | if ($condition) { |
||
44 | $callback($this); |
||
45 | } |
||
46 | |||
47 | return $this; |
||
0 ignored issues
–
show
The visibility should be declared for property
$this .
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using class A {
var $property;
}
the property is implicitly global. To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.
Loading history...
|
|||
48 | } |
||
49 | |||
50 | public function getProperty(string $property, $default = null) |
||
51 | { |
||
52 | return $this->properties[$property] ?? $default; |
||
53 | } |
||
54 | |||
55 | public function getProperties(): array |
||
56 | { |
||
57 | return $this->properties; |
||
58 | } |
||
59 | |||
60 | public function offsetExists($offset) |
||
61 | { |
||
62 | return array_key_exists($offset, $this->properties); |
||
63 | } |
||
64 | |||
65 | public function offsetGet($offset) |
||
66 | { |
||
67 | return $this->getProperty($offset); |
||
68 | } |
||
69 | |||
70 | public function offsetSet($offset, $value) |
||
71 | { |
||
72 | $this->setProperty($offset, $value); |
||
73 | } |
||
74 | |||
75 | public function offsetUnset($offset) |
||
76 | { |
||
77 | unset($this->properties[$offset]); |
||
78 | } |
||
79 | |||
80 | public function toArray(): array |
||
81 | { |
||
82 | $properties = $this->serializeProperty($this->getProperties()); |
||
83 | |||
84 | return [ |
||
85 | '@context' => $this->getContext(), |
||
86 | '@type' => $this->getType(), |
||
87 | ] + $properties; |
||
88 | } |
||
89 | |||
90 | protected function serializeProperty($property) |
||
91 | { |
||
92 | if (is_array($property)) { |
||
93 | return array_map([$this, 'serializeProperty'], $property); |
||
94 | } |
||
95 | |||
96 | if ($property instanceof Type) { |
||
97 | $property = $property->toArray(); |
||
98 | unset($property['@context']); |
||
99 | } |
||
100 | |||
101 | if ($property instanceof DateTimeInterface) { |
||
102 | $property = $property->format(DateTime::ATOM); |
||
103 | } |
||
104 | |||
105 | if (is_object($property)) { |
||
106 | throw new InvalidProperty(); |
||
107 | } |
||
108 | |||
109 | return $property; |
||
110 | } |
||
111 | |||
112 | public function toScript(): string |
||
113 | { |
||
114 | return '<script type="application/ld+json">'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>'; |
||
115 | } |
||
116 | |||
117 | public function jsonSerialize() |
||
118 | { |
||
119 | return $this->toArray(); |
||
120 | } |
||
121 | |||
122 | public function __call(string $method, array $arguments) |
||
123 | { |
||
124 | return $this->setProperty($method, $arguments[0] ?? ''); |
||
125 | } |
||
126 | |||
127 | public function __toString(): string |
||
128 | { |
||
129 | return $this->toScript(); |
||
130 | } |
||
131 | } |
||
132 |