Passed
Push — master ( 1a17c3...faad8c )
by
unknown
01:09
created

DataContainerDecoratorTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 183
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0
wmc 14

14 Methods

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