|
2 | 2 |
|
3 | 3 | namespace Spatie\SchemaOrg;
|
4 | 4 |
|
5 |
| -use DateTimeInterface; |
6 | 5 | use ReflectionClass;
|
7 | 6 | use Spatie\SchemaOrg\Exceptions\InvalidProperty;
|
8 | 7 |
|
@@ -40,43 +39,27 @@ public function getProperties(): array
|
40 | 39 |
|
41 | 40 | public function toArray(): array
|
42 | 41 | {
|
43 |
| - $properties = $this->serialize($this->getProperties()); |
| 42 | + $properties = array_map(function ($property) { |
| 43 | + if ($property instanceof Type) { |
| 44 | + $property = $property->toArray(); |
| 45 | + unset($property['@context']); |
| 46 | + } |
| 47 | + |
| 48 | + if (is_object($property)) { |
| 49 | + throw new InvalidProperty(); |
| 50 | + } |
| 51 | + |
| 52 | + return $property; |
| 53 | + }, $this->getProperties()); |
44 | 54 |
|
45 | 55 | return [
|
46 | 56 | '@context' => $this->getContext(),
|
47 | 57 | '@type' => $this->getType(),
|
48 | 58 | ] + $properties;
|
49 | 59 | }
|
50 | 60 |
|
51 |
| - protected function serialize($property) |
52 |
| - { |
53 |
| - if (is_array($property)) { |
54 |
| - return array_map(function ($property) { |
55 |
| - return $this->serialize($property); |
56 |
| - }, $property); |
57 |
| - } |
58 |
| - |
59 |
| - if ($property instanceof Type) { |
60 |
| - $property = $property->toArray(); |
61 |
| - unset($property['@context']); |
62 |
| - return $property; |
63 |
| - } |
64 |
| - |
65 |
| - if ($property instanceof DateTimeInterface) { |
66 |
| - return $property->format('c'); |
67 |
| - } |
68 |
| - |
69 |
| - if (is_object($property)) { |
70 |
| - throw new InvalidProperty(); |
71 |
| - } |
72 |
| - |
73 |
| - return $property; |
74 |
| - } |
75 |
| - |
76 | 61 | public function toScript(): string
|
77 | 62 | {
|
78 |
| - return '<script type="application/ld+json">'. |
79 |
| - json_encode($this->toArray()). |
80 |
| - '</script>'; |
| 63 | + return '<script type="application/ld+json">'.json_encode($this->toArray()).'</script>'; |
81 | 64 | }
|
82 | 65 | }
|
0 commit comments