Passed
Branch version-bump (36e4b3)
by Matt
04:39
created

BaseManager::getParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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
    public function register(ShortcodeInterface $shortcode, ?string $name = null): ManagerInterface
32
    {
33
        $name = $name ?: $shortcode->getName();
34
35
        if (!$name) {
36
            throw RegisterException::blank();
37
        } elseif ($this->isRegistered($name)) {
38
            throw RegisterException::duplicate($name);
39
        }
40
41
        if ($shortcode instanceof ContainerAwareInterface) {
42
            $shortcode->bind($this);
43
        }
44
45
        $this->shortcodes[$name] = $shortcode;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $name
52
     *
53
     * @throws DeregisterException
54
     *
55
     * @return static
56
     */
57
    public function deregister(string $name): ManagerInterface
58
    {
59
        if (!$name) {
60
            throw DeregisterException::blank();
61
        } elseif (!$this->isRegistered($name)) {
62
            throw DeregisterException::missing($name);
63
        }
64
65
        unset($this->shortcodes[$name]);
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param string $name
72
     *
73
     * @return bool
74
     */
75
    public function isRegistered(string $name): bool
76
    {
77
        return isset($this->shortcodes[$name]);
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getRegistered(): array
84
    {
85
        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
    public function offsetGet($offset): ShortcodeInterface
113
    {
114
        if (!$this->isRegistered($offset)) {
115
            throw RegisterException::missing($offset);
116
        }
117
118
        return $this->shortcodes[$offset];
119
    }
120
121
    /**
122
     * @param mixed $offset
123
     * @param mixed $value
124
     *
125
     * @throws RegisterException
126
     */
127
    public function offsetSet($offset, $value): void
128
    {
129
        $name = is_string($offset) && !is_numeric($offset) ? $offset : null;
130
        $this->register($value, $name);
131
    }
132
133
    /**
134
     * @param mixed $offset
135
     *
136
     * @throws DeregisterException
137
     */
138
    public function offsetUnset($offset): void
139
    {
140
        $this->deregister($offset);
141
    }
142
143
    /**
144
     * @param mixed $offset
145
     *
146
     * @return bool
147
     */
148
    public function offsetExists($offset): bool
149
    {
150
        return $this->isRegistered($offset);
151
    }
152
153
    /**
154
     * @return ArrayIterator
155
     */
156
    public function getIterator(): ArrayIterator
157
    {
158
        return new ArrayIterator($this->shortcodes);
159
    }
160
}
161