| @@ 12-148 (lines=137) @@ | ||
| 9 | use ReflectionClass; |
|
| 10 | use Spatie\SchemaOrg\Exceptions\InvalidProperty; |
|
| 11 | ||
| 12 | abstract class BaseType implements Type, ArrayAccess, JsonSerializable |
|
| 13 | { |
|
| 14 | /** @var array */ |
|
| 15 | protected $properties = []; |
|
| 16 | ||
| 17 | public function getContext(): string |
|
| 18 | { |
|
| 19 | return 'https://schema.org'; |
|
| 20 | } |
|
| 21 | ||
| 22 | public function getType(): string |
|
| 23 | { |
|
| 24 | return (new ReflectionClass($this))->getShortName(); |
|
| 25 | } |
|
| 26 | ||
| 27 | public function setProperty(string $property, $value) |
|
| 28 | { |
|
| 29 | if ($value !== null) { |
|
| 30 | $this->properties[$property] = $value; |
|
| 31 | } |
|
| 32 | ||
| 33 | return $this; |
|
| 34 | } |
|
| 35 | ||
| 36 | public function addProperties(array $properties) |
|
| 37 | { |
|
| 38 | foreach ($properties as $property => $value) { |
|
| 39 | $this->setProperty($property, $value); |
|
| 40 | } |
|
| 41 | ||
| 42 | return $this; |
|
| 43 | } |
|
| 44 | ||
| 45 | public function if($condition, $callback) |
|
| 46 | { |
|
| 47 | if ($condition) { |
|
| 48 | $callback($this); |
|
| 49 | } |
|
| 50 | ||
| 51 | return $this; |
|
| 52 | } |
|
| 53 | ||
| 54 | public function getProperty(string $property, $default = null) |
|
| 55 | { |
|
| 56 | return $this->properties[$property] ?? $default; |
|
| 57 | } |
|
| 58 | ||
| 59 | public function getProperties(): array |
|
| 60 | { |
|
| 61 | return $this->properties; |
|
| 62 | } |
|
| 63 | ||
| 64 | public function offsetExists($offset) |
|
| 65 | { |
|
| 66 | return array_key_exists($offset, $this->properties); |
|
| 67 | } |
|
| 68 | ||
| 69 | public function offsetGet($offset) |
|
| 70 | { |
|
| 71 | return $this->getProperty($offset); |
|
| 72 | } |
|
| 73 | ||
| 74 | public function offsetSet($offset, $value) |
|
| 75 | { |
|
| 76 | $this->setProperty($offset, $value); |
|
| 77 | } |
|
| 78 | ||
| 79 | public function offsetUnset($offset) |
|
| 80 | { |
|
| 81 | unset($this->properties[$offset]); |
|
| 82 | } |
|
| 83 | ||
| 84 | public function toArray(): array |
|
| 85 | { |
|
| 86 | $this->serializeIdentifier(); |
|
| 87 | $properties = $this->serializeProperty($this->getProperties()); |
|
| 88 | ||
| 89 | return [ |
|
| 90 | '@context' => $this->getContext(), |
|
| 91 | '@type' => $this->getType(), |
|
| 92 | ] + $properties; |
|
| 93 | } |
|
| 94 | ||
| 95 | protected function serializeProperty($property) |
|
| 96 | { |
|
| 97 | if (is_array($property)) { |
|
| 98 | return array_map([$this, 'serializeProperty'], $property); |
|
| 99 | } |
|
| 100 | ||
| 101 | if ($property instanceof Type) { |
|
| 102 | $property = $property->toArray(); |
|
| 103 | unset($property['@context']); |
|
| 104 | } |
|
| 105 | ||
| 106 | if ($property instanceof DateTimeInterface) { |
|
| 107 | $property = $property->format(DateTime::ATOM); |
|
| 108 | } |
|
| 109 | ||
| 110 | if (is_object($property) && method_exists($property, '__toString')) { |
|
| 111 | $property = (string) $property; |
|
| 112 | } |
|
| 113 | ||
| 114 | if (is_object($property)) { |
|
| 115 | throw new InvalidProperty(); |
|
| 116 | } |
|
| 117 | ||
| 118 | return $property; |
|
| 119 | } |
|
| 120 | ||
| 121 | protected function serializeIdentifier() |
|
| 122 | { |
|
| 123 | if (isset($this['identifier'])) { |
|
| 124 | $this->setProperty('@id', $this['identifier']); |
|
| 125 | unset($this['identifier']); |
|
| 126 | } |
|
| 127 | } |
|
| 128 | ||
| 129 | public function toScript(): string |
|
| 130 | { |
|
| 131 | return '<script type="application/ld+json">'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>'; |
|
| 132 | } |
|
| 133 | ||
| 134 | public function jsonSerialize() |
|
| 135 | { |
|
| 136 | return $this->toArray(); |
|
| 137 | } |
|
| 138 | ||
| 139 | public function __call(string $method, array $arguments) |
|
| 140 | { |
|
| 141 | return $this->setProperty($method, $arguments[0] ?? ''); |
|
| 142 | } |
|
| 143 | ||
| 144 | public function __toString(): string |
|
| 145 | { |
|
| 146 | return $this->toScript(); |
|
| 147 | } |
|
| 148 | } |
|
| 149 | ||
| @@ 12-148 (lines=137) @@ | ||
| 9 | use ReflectionClass; |
|
| 10 | use Spatie\SchemaOrg\Exceptions\InvalidProperty; |
|
| 11 | ||
| 12 | abstract class BaseType implements Type, ArrayAccess, JsonSerializable |
|
| 13 | { |
|
| 14 | /** @var array */ |
|
| 15 | protected $properties = []; |
|
| 16 | ||
| 17 | public function getContext(): string |
|
| 18 | { |
|
| 19 | return 'https://schema.org'; |
|
| 20 | } |
|
| 21 | ||
| 22 | public function getType(): string |
|
| 23 | { |
|
| 24 | return (new ReflectionClass($this))->getShortName(); |
|
| 25 | } |
|
| 26 | ||
| 27 | public function setProperty(string $property, $value) |
|
| 28 | { |
|
| 29 | if ($value !== null) { |
|
| 30 | $this->properties[$property] = $value; |
|
| 31 | } |
|
| 32 | ||
| 33 | return $this; |
|
| 34 | } |
|
| 35 | ||
| 36 | public function addProperties(array $properties) |
|
| 37 | { |
|
| 38 | foreach ($properties as $property => $value) { |
|
| 39 | $this->setProperty($property, $value); |
|
| 40 | } |
|
| 41 | ||
| 42 | return $this; |
|
| 43 | } |
|
| 44 | ||
| 45 | public function if($condition, $callback) |
|
| 46 | { |
|
| 47 | if ($condition) { |
|
| 48 | $callback($this); |
|
| 49 | } |
|
| 50 | ||
| 51 | return $this; |
|
| 52 | } |
|
| 53 | ||
| 54 | public function getProperty(string $property, $default = null) |
|
| 55 | { |
|
| 56 | return $this->properties[$property] ?? $default; |
|
| 57 | } |
|
| 58 | ||
| 59 | public function getProperties(): array |
|
| 60 | { |
|
| 61 | return $this->properties; |
|
| 62 | } |
|
| 63 | ||
| 64 | public function offsetExists($offset) |
|
| 65 | { |
|
| 66 | return array_key_exists($offset, $this->properties); |
|
| 67 | } |
|
| 68 | ||
| 69 | public function offsetGet($offset) |
|
| 70 | { |
|
| 71 | return $this->getProperty($offset); |
|
| 72 | } |
|
| 73 | ||
| 74 | public function offsetSet($offset, $value) |
|
| 75 | { |
|
| 76 | $this->setProperty($offset, $value); |
|
| 77 | } |
|
| 78 | ||
| 79 | public function offsetUnset($offset) |
|
| 80 | { |
|
| 81 | unset($this->properties[$offset]); |
|
| 82 | } |
|
| 83 | ||
| 84 | public function toArray(): array |
|
| 85 | { |
|
| 86 | $this->serializeIdentifier(); |
|
| 87 | $properties = $this->serializeProperty($this->getProperties()); |
|
| 88 | ||
| 89 | return [ |
|
| 90 | '@context' => $this->getContext(), |
|
| 91 | '@type' => $this->getType(), |
|
| 92 | ] + $properties; |
|
| 93 | } |
|
| 94 | ||
| 95 | protected function serializeProperty($property) |
|
| 96 | { |
|
| 97 | if (is_array($property)) { |
|
| 98 | return array_map([$this, 'serializeProperty'], $property); |
|
| 99 | } |
|
| 100 | ||
| 101 | if ($property instanceof Type) { |
|
| 102 | $property = $property->toArray(); |
|
| 103 | unset($property['@context']); |
|
| 104 | } |
|
| 105 | ||
| 106 | if ($property instanceof DateTimeInterface) { |
|
| 107 | $property = $property->format(DateTime::ATOM); |
|
| 108 | } |
|
| 109 | ||
| 110 | if (is_object($property) && method_exists($property, '__toString')) { |
|
| 111 | $property = (string) $property; |
|
| 112 | } |
|
| 113 | ||
| 114 | if (is_object($property)) { |
|
| 115 | throw new InvalidProperty(); |
|
| 116 | } |
|
| 117 | ||
| 118 | return $property; |
|
| 119 | } |
|
| 120 | ||
| 121 | protected function serializeIdentifier() |
|
| 122 | { |
|
| 123 | if (isset($this['identifier'])) { |
|
| 124 | $this->setProperty('@id', $this['identifier']); |
|
| 125 | unset($this['identifier']); |
|
| 126 | } |
|
| 127 | } |
|
| 128 | ||
| 129 | public function toScript(): string |
|
| 130 | { |
|
| 131 | return '<script type="application/ld+json">'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>'; |
|
| 132 | } |
|
| 133 | ||
| 134 | public function jsonSerialize() |
|
| 135 | { |
|
| 136 | return $this->toArray(); |
|
| 137 | } |
|
| 138 | ||
| 139 | public function __call(string $method, array $arguments) |
|
| 140 | { |
|
| 141 | return $this->setProperty($method, $arguments[0] ?? ''); |
|
| 142 | } |
|
| 143 | ||
| 144 | public function __toString(): string |
|
| 145 | { |
|
| 146 | return $this->toScript(); |
|
| 147 | } |
|
| 148 | } |
|
| 149 | ||