1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SchemaOrg; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use Spatie\SchemaOrg\Exceptions\InvalidType; |
8
|
|
|
use Spatie\SchemaOrg\Exceptions\TypeNotInGraph; |
9
|
|
|
use Spatie\SchemaOrg\Exceptions\TypeAlreadyInGraph; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @mixin \Spatie\SchemaOrg\Schema |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
class Graph extends BaseType |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
protected $hidden = []; |
18
|
|
|
|
19
|
|
|
public function __call(string $method, array $arguments) |
20
|
|
|
{ |
21
|
|
|
if (is_callable([Schema::class, $method])) { |
22
|
|
|
$type = (new ReflectionClass(Schema::class))->getMethod($method)->getReturnType(); |
23
|
|
|
|
24
|
|
|
$schema = $this->getOrCreate($type); |
25
|
|
|
|
26
|
|
|
if (isset($arguments[0]) && is_callable($arguments[0])) { |
27
|
|
|
call_user_func($arguments[0], $schema, $this); |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $schema; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
throw new BadMethodCallException(sprintf('The method "%" does not exist on class "%s".', $method, get_class($this))); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function add(Type $schema): self |
39
|
|
|
{ |
40
|
|
|
$type = get_class($schema); |
41
|
|
|
|
42
|
|
|
if ($this->has($type)) { |
43
|
|
|
throw new TypeAlreadyInGraph(sprintf('The graph already has an item of type "%s".', $type)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->set($schema); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function has(string $type): bool |
50
|
|
|
{ |
51
|
|
|
return $this->offsetExists($type); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function set(Type $schema) |
55
|
|
|
{ |
56
|
|
|
return $this->setProperty(get_class($schema), $schema); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function get(string $type): Type |
60
|
|
|
{ |
61
|
|
|
if (! $this->has($type)) { |
62
|
|
|
throw new TypeNotInGraph(sprintf('The graph does not have an item of type "%s".', $type)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->getProperty($type); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getOrCreate(string $type): Type |
69
|
|
|
{ |
70
|
|
|
if (! is_subclass_of($type, Type::class)) { |
71
|
|
|
throw new InvalidType(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (! $this->has($type)) { |
75
|
|
|
$this->set(new $type()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->get($type); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function hide(string $type): self |
82
|
|
|
{ |
83
|
|
|
$this->hidden[$type] = true; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function show(string $type): self |
89
|
|
|
{ |
90
|
|
|
$this->hidden[$type] = false; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function toArray(): array |
96
|
|
|
{ |
97
|
|
|
$properties = $this->getProperties(); |
98
|
|
|
|
99
|
|
|
foreach ($this->hidden as $type => $hide) { |
100
|
|
|
if ($hide) { |
101
|
|
|
unset($properties[$type]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return [ |
106
|
|
|
'@context' => $this->getContext(), |
107
|
|
|
'@graph' => $this->serializeProperty(array_values($properties)), |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.