1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SchemaOrg; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @mixin Schema |
10
|
|
|
*/ |
11
|
|
View Code Duplication |
class Graph extends BaseType |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
protected $hidden = []; |
14
|
|
|
|
15
|
|
|
public function __call(string $method, array $arguments) |
16
|
|
|
{ |
17
|
|
|
if (is_callable([Schema::class, $method])) { |
18
|
|
|
return $this->getOrNew((new ReflectionClass(Schema::class))->getMethod($method)->getReturnType()); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function add(Type $schema) |
23
|
|
|
{ |
24
|
|
|
$type = get_class($schema); |
25
|
|
|
if ($this->has($type)) { |
26
|
|
|
throw new InvalidArgumentException(sprintf('The graph already has an item of type "%s".', $type)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this->set($schema); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function has(string $type): bool |
33
|
|
|
{ |
34
|
|
|
return $this->offsetExists($type); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function set(Type $schema) |
38
|
|
|
{ |
39
|
|
|
return $this->setProperty(get_class($schema), $schema); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function get(string $type): Type |
43
|
|
|
{ |
44
|
|
|
if (! $this->has($type)) { |
45
|
|
|
throw new InvalidArgumentException(sprintf('The graph does not have an item of type "%s".', $type)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->getProperty($type); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getOrNew(string $type): Type |
52
|
|
|
{ |
53
|
|
|
if ($this->has($type)) { |
54
|
|
|
return $this->get($type); |
55
|
|
|
} elseif (is_subclass_of($type, Type::class)) { |
56
|
|
|
return $this->properties[$type] = new $type(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw new InvalidArgumentException(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function hide(string $type) |
63
|
|
|
{ |
64
|
|
|
$this->hidden[$type] = true; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function show(string $type) |
70
|
|
|
{ |
71
|
|
|
$this->hidden[$type] = false; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function toArray(): array |
77
|
|
|
{ |
78
|
|
|
$properties = $this->getProperties(); |
79
|
|
|
foreach ($this->hidden as $type => $hide) { |
80
|
|
|
if ($hide) { |
81
|
|
|
unset($properties[$type]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return [ |
86
|
|
|
'@context' => $this->getContext(), |
87
|
|
|
'@graph' => $this->serializeProperty(array_values($properties)), |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.