1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maiorano\Shortcodes\Manager; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use Maiorano\Shortcodes\Contracts\ContainerAwareInterface; |
9
|
|
|
use Maiorano\Shortcodes\Contracts\ShortcodeInterface; |
10
|
|
|
use Maiorano\Shortcodes\Exceptions\DeregisterException; |
11
|
|
|
use Maiorano\Shortcodes\Exceptions\RegisterException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class BaseManager. |
15
|
|
|
*/ |
16
|
|
|
abstract class BaseManager implements ManagerInterface, ArrayAccess, IteratorAggregate |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $shortcodes = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param ShortcodeInterface $shortcode |
25
|
|
|
* @param string|null $name |
26
|
|
|
* |
27
|
|
|
* @throws RegisterException |
28
|
|
|
* |
29
|
|
|
* @return static |
30
|
|
|
*/ |
31
|
19 |
|
public function register(ShortcodeInterface $shortcode, ?string $name = null): ManagerInterface |
32
|
|
|
{ |
33
|
19 |
|
$name = $name ?: $shortcode->getName(); |
34
|
|
|
|
35
|
19 |
|
if (!$name) { |
36
|
1 |
|
throw RegisterException::blank(); |
37
|
18 |
|
} elseif ($this->isRegistered($name)) { |
38
|
1 |
|
throw RegisterException::duplicate($name); |
39
|
|
|
} |
40
|
|
|
|
41
|
18 |
|
if ($shortcode instanceof ContainerAwareInterface) { |
42
|
1 |
|
$shortcode->bind($this); |
43
|
|
|
} |
44
|
|
|
|
45
|
18 |
|
$this->shortcodes[$name] = $shortcode; |
46
|
|
|
|
47
|
18 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
* |
53
|
|
|
* @throws DeregisterException |
54
|
|
|
* |
55
|
|
|
* @return static |
56
|
|
|
*/ |
57
|
6 |
|
public function deregister(string $name): ManagerInterface |
58
|
|
|
{ |
59
|
6 |
|
if (!$name) { |
60
|
1 |
|
throw DeregisterException::blank(); |
61
|
5 |
|
} elseif (!$this->isRegistered($name)) { |
62
|
1 |
|
throw DeregisterException::missing($name); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
unset($this->shortcodes[$name]); |
66
|
|
|
|
67
|
4 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
21 |
|
public function isRegistered(string $name): bool |
76
|
|
|
{ |
77
|
21 |
|
return isset($this->shortcodes[$name]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
5 |
|
public function getRegistered(): array |
84
|
|
|
{ |
85
|
5 |
|
return array_keys($this->shortcodes); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $content |
90
|
|
|
* @param array $tags |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
abstract public function hasShortcode(string $content, $tags = []): bool; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $content |
98
|
|
|
* @param array $tags |
99
|
|
|
* @param bool $deep |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
abstract public function doShortcode(string $content, $tags = [], bool $deep = false): string; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param mixed $offset |
107
|
|
|
* |
108
|
|
|
* @throws RegisterException |
109
|
|
|
* |
110
|
|
|
* @return ShortcodeInterface |
111
|
|
|
*/ |
112
|
7 |
|
public function offsetGet($offset): ShortcodeInterface |
113
|
|
|
{ |
114
|
7 |
|
if (!$this->isRegistered($offset)) { |
115
|
1 |
|
throw RegisterException::missing($offset); |
116
|
|
|
} |
117
|
|
|
|
118
|
6 |
|
return $this->shortcodes[$offset]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param mixed $offset |
123
|
|
|
* @param mixed $value |
124
|
|
|
* |
125
|
|
|
* @throws RegisterException |
126
|
|
|
*/ |
127
|
15 |
|
public function offsetSet($offset, $value): void |
128
|
|
|
{ |
129
|
15 |
|
$name = is_string($offset) && !is_numeric($offset) ? $offset : null; |
130
|
15 |
|
$this->register($value, $name); |
131
|
15 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param mixed $offset |
135
|
|
|
* |
136
|
|
|
* @throws DeregisterException |
137
|
|
|
*/ |
138
|
1 |
|
public function offsetUnset($offset): void |
139
|
|
|
{ |
140
|
1 |
|
$this->deregister($offset); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param mixed $offset |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
6 |
|
public function offsetExists($offset): bool |
149
|
|
|
{ |
150
|
6 |
|
return $this->isRegistered($offset); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return ArrayIterator |
155
|
|
|
*/ |
156
|
7 |
|
public function getIterator(): ArrayIterator |
157
|
|
|
{ |
158
|
7 |
|
return new ArrayIterator($this->shortcodes); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|