Passed
Pull Request — master (#9)
by Ashoka
07:03
created

DataContainerDecoratorTrait::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\DataContainer;
8
9
use Traversable;
10
11
/**
12
 * Implements DataContainerInterface
13
 */
14
trait DataContainerDecoratorTrait
15
{
16
    /** @var DataContainerInterface */
17
    private $storage;
18
19
    /**
20
     * Set data on a new storage.
21
     *
22
     * @param array $data
23
     *
24
     * @return void
25
     */
26 1
    protected function setData(array $data)
27
    {
28 1
        $this->storage = new DataContainer($data);
29 1
    }
30
31
    /**
32
     * Set the storage.
33
     *
34
     * @param DataContainerInterface $storage
35
     *
36
     * @return void
37
     */
38 1
    protected function setStorage(DataContainerInterface $storage)
39
    {
40 1
        $this->storage = $storage;
41 1
    }
42
43
    /**
44
     * Get the storage.
45
     *
46
     * @return DataContainerInterface
47
     */
48 2
    protected function getStorage(): DataContainerInterface
49
    {
50 2
        return $this->storage;
51
    }
52
53
    /**
54
     * Check whether a path exists.
55
     *
56
     * @param string $path
57
     *
58
     * @return bool
59
     */
60 1
    public function has(string $path): bool
61
    {
62 1
        return $this->getStorage()
63 1
            ->has($path);
64
    }
65
66
    /**
67
     * Get a value of a path.
68
     *
69
     * @param string $path
70
     * @param mixed  $default
71
     *
72
     * @return mixed
73
     */
74 1
    public function get(string $path, $default = null)
75
    {
76 1
        return $this->getStorage()
77 1
            ->get($path, $default);
78
    }
79
80
    /**
81
     * Get the contained array.
82
     *
83
     * @return array
84
     */
85 1
    public function all(): array
86
    {
87 1
        return $this->getStorage()
88 1
            ->all();
89
    }
90
91
    /**
92
     * Set a value on a path.
93
     *
94
     * @param string $path
95
     * @param mixed  $value
96
     *
97
     * @return void
98
     */
99 1
    public function set(string $path, $value = null)
100
    {
101 1
        $this->getStorage()
102 1
            ->set($path, $value);
103 1
    }
104
105
    /**
106
     * Remove a path if it exists.
107
     *
108
     * @param string $pattern
109
     *
110
     * @return void
111
     */
112 1
    public function remove(string $pattern)
113
    {
114 1
        $this->getStorage()
115 1
            ->remove($pattern);
116 1
    }
117
118
    /**
119
     * Find paths that match a pattern.
120
     *
121
     * @param string $pattern
122
     *
123
     * @return string[]
124
     */
125 1
    public function glob(string $pattern): array
126
    {
127 1
        return $this->getStorage()
128 1
            ->glob($pattern);
129
    }
130
131
    /**
132
     * Find paths that match a pattern an their replacements.
133
     *
134
     * @param string $pattern
135
     * @param string $replacement
136
     *
137
     * @return string[]
138
     */
139 1
    public function expand(string $pattern, string $replacement): array
140
    {
141 1
        return $this->getStorage()
142 1
            ->expand($pattern, $replacement);
143
    }
144
145
    /**
146
     * Branch into a list of data containers.
147
     *
148
     * @param string $pattern
149
     *
150
     * @return DataContainerInterface[]
151
     */
152 1
    public function branch(string $pattern): array
153
    {
154 1
        return $this->getStorage()
155 1
            ->branch($pattern);
156
    }
157
158
    /**
159
     * Get a node from the container.
160
     *
161
     * @param string $path
162
     *
163
     * @return DataContainerInterface
164
     */
165 1
    public function node(string $path): DataContainerInterface
166
    {
167 1
        return $this->getStorage()
168 1
            ->node($path);
169
    }
170
171
    /**
172
     * Copy paths matching a pattern to another path.
173
     *
174
     * @param string $pattern
175
     * @param string $replacement
176
     *
177
     * @return void
178
     */
179 1
    public function copy(string $pattern, string $replacement)
180
    {
181 1
        $this->getStorage()
182 1
            ->copy($pattern, $replacement);
183 1
    }
184
185
    /**
186
     * Move paths matching a pattern to another path.
187
     *
188
     * @param string $pattern
189
     * @param string $replacement
190
     *
191
     * @return void
192
     */
193 1
    public function move(string $pattern, string $replacement)
194
    {
195 1
        $this->getStorage()
196 1
            ->move($pattern, $replacement);
197 1
    }
198
199
    /**
200
     * Retrieve an external iterator.
201
     *
202
     * @return Traversable
203
     */
204 1
    public function getIterator(): Traversable
205
    {
206 1
        return $this->getStorage()
207 1
            ->getIterator();
208
    }
209
210
    /**
211
     * Prepare the object for cloning.
212
     *
213
     * @return void
214
     */
215 1
    public function __clone()
216
    {
217 1
        $this->storage = clone $this->storage;
218 1
    }
219
}
220