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