|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SeoHelper\MetaData; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
class BaseMetaData |
|
10
|
|
|
{ |
|
11
|
|
|
private array $data = []; |
|
12
|
|
|
|
|
13
|
27 |
|
private array $aliases = []; |
|
14
|
|
|
|
|
15
|
27 |
|
public function __call($name, $arguments) |
|
16
|
15 |
|
{ |
|
17
|
15 |
|
if (str_starts_with($name, 'get')) { |
|
18
|
27 |
|
$propertyName = $this->createPropertyName(str_replace('get', '', $name)); |
|
19
|
15 |
|
return $this->get($propertyName); |
|
20
|
15 |
|
} elseif (str_starts_with($name, 'set')) { |
|
21
|
24 |
|
$propertyName = $this->createPropertyName(str_replace('set', '', $name)); |
|
22
|
18 |
|
return $this->set($propertyName, $arguments[0]); |
|
23
|
18 |
|
} elseif (str_starts_with($name, 'add')) { |
|
24
|
6 |
|
$propertyName = $this->createPropertyName(str_replace('add', '', $name)); |
|
25
|
3 |
|
return $this->add($propertyName, $arguments[0]); |
|
26
|
3 |
|
} elseif (str_starts_with($name, 'reset')) { |
|
27
|
|
|
$propertyName = $this->createPropertyName(str_replace('reset', '', $name)); |
|
28
|
3 |
|
return $this->reset($propertyName, $arguments[0] ?? null); |
|
29
|
|
|
} |
|
30
|
|
|
throw new InvalidArgumentException("Method '$name' not found"); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
final public function set(string $key, string|array $value): static |
|
34
|
|
|
{ |
|
35
|
|
|
if (!is_array($value)) { |
|
|
|
|
|
|
36
|
18 |
|
$value = [$value]; |
|
37
|
|
|
} |
|
38
|
18 |
|
foreach ($this->findAliases($key) as $alias) { |
|
39
|
18 |
|
$this->data[$alias] = $value; |
|
40
|
6 |
|
} |
|
41
|
18 |
|
return $this; |
|
42
|
18 |
|
} |
|
43
|
6 |
|
|
|
44
|
18 |
|
final public function reset(string $key, string|array|null $newValue = null): static |
|
45
|
|
|
{ |
|
46
|
|
|
if ($newValue !== null) { |
|
47
|
|
|
return $this->set($key, $newValue); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
foreach ($this->findAliases($key) as $alias) { |
|
51
|
|
|
unset($this->data[$alias]); |
|
52
|
3 |
|
} |
|
53
|
|
|
return $this; |
|
54
|
3 |
|
} |
|
55
|
3 |
|
|
|
56
|
|
|
final public function resetAll(): void |
|
57
|
|
|
{ |
|
58
|
3 |
|
$this->data = []; |
|
59
|
3 |
|
$this->aliases = []; |
|
60
|
1 |
|
} |
|
61
|
3 |
|
|
|
62
|
|
|
final public function add(string $key, string|array $value): static |
|
63
|
|
|
{ |
|
64
|
|
|
if (!is_array($value)) { |
|
|
|
|
|
|
65
|
|
|
$value = [$value]; |
|
66
|
|
|
} |
|
67
|
|
|
foreach ($this->findAliases($key) as $alias) { |
|
68
|
|
|
if (!isset($this->data[$alias])) { |
|
69
|
18 |
|
$this->data[$alias] = []; |
|
70
|
|
|
} |
|
71
|
18 |
|
$this->data[$alias] = array_merge($this->data[$alias], $value); |
|
72
|
18 |
|
} |
|
73
|
6 |
|
return $this; |
|
74
|
18 |
|
} |
|
75
|
18 |
|
|
|
76
|
18 |
|
/** |
|
77
|
6 |
|
* @return array|null all data if $key is null or data for $key as array or null if $key is not set in data |
|
78
|
18 |
|
*/ |
|
79
|
6 |
|
final public function get(?string $key = null): ?array |
|
80
|
18 |
|
{ |
|
81
|
|
|
if ($key === null) { |
|
82
|
|
|
return $this->data; |
|
83
|
|
|
} |
|
84
|
|
|
return $this->data[$key] ?? null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
27 |
|
/** |
|
88
|
|
|
* alias(es) for type |
|
89
|
27 |
|
* for example title => [og:title, twitter:title], in this case there is no need to set all types, because they will be set automatically with main type |
|
90
|
21 |
|
*/ |
|
91
|
|
|
final public function alias(string $type, string|array $alias): static |
|
92
|
18 |
|
{ |
|
93
|
|
|
if (!isset($this->aliases[$type])) { |
|
94
|
|
|
$this->aliases[$type] = []; |
|
95
|
|
|
} |
|
96
|
|
|
if (!is_array($alias)) { |
|
|
|
|
|
|
97
|
|
|
$this->aliases[$type][] = $alias; |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
$this->aliases[$type] = array_merge($this->aliases[$type], $alias); |
|
102
|
|
|
return $this; |
|
103
|
3 |
|
} |
|
104
|
3 |
|
|
|
105
|
1 |
|
private function createPropertyName(string $camelCase): string |
|
106
|
3 |
|
{ |
|
107
|
3 |
|
$length = strlen($camelCase); |
|
108
|
3 |
|
$lowerCamelCase = lcfirst($camelCase); |
|
109
|
|
|
$propertyName = ''; |
|
110
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
111
|
3 |
|
if ($lowerCamelCase[$i] == strtoupper($lowerCamelCase[$i])) { |
|
112
|
3 |
|
$propertyName .= ':'; |
|
113
|
|
|
} |
|
114
|
|
|
$propertyName .= strtolower($lowerCamelCase[$i]); |
|
115
|
24 |
|
} |
|
116
|
|
|
return $propertyName; |
|
117
|
24 |
|
} |
|
118
|
24 |
|
|
|
119
|
24 |
|
private function findAliases(string $key): array |
|
120
|
24 |
|
{ |
|
121
|
24 |
|
$aliases = $this->aliases[$key] ?? []; |
|
122
|
15 |
|
return array_merge([$key], $aliases); |
|
123
|
5 |
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|