FtpMutablePath   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 262
rs 10
c 0
b 0
f 0
wmc 29

17 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 3 1
A get() 0 3 1
A raw() 0 12 3
A last() 0 3 1
A first() 0 3 1
A encoded() 0 12 3
A only() 0 3 1
A append() 0 5 1
A set() 0 5 1
A forgetAll() 0 5 1
A putAfter() 0 9 2
A forget() 0 11 3
A putInBetween() 0 13 5
A putBefore() 0 9 2
A prepend() 0 5 1
A getPath() 0 3 1
A has() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Builder\Schemes\Ftp\Bags;
5
6
use Keppler\Url\Exceptions\ComponentNotFoundException;
7
use Keppler\Url\Interfaces\Mutable\MutableBagInterface;
8
use Keppler\Url\Traits\Accessor;
9
use Keppler\Url\Traits\Mutator;
10
11
/**
12
 * Class HttpImmutablePath
13
 * @package Keppler\Url\Schemes\Http\Bags
14
 */
15
class FtpMutablePath implements MutableBagInterface
16
{
17
    use Accessor;
18
    use Mutator;
19
20
    /**
21
     *  path = path-abempty    ; begins with "/" or is empty
22
     *
23
     *
24
     *  / path-absolute   ; begins with "/" but not "//"
25
     *  / path-noscheme   ; begins with a non-colon segment
26
     *  / path-rootless   ; begins with a segment
27
     *  / path-empty      ; zero characters
28
     *
29
     *  path-abempty  = *( "/" segment )
30
     *  path-absolute = "/" [ segment-nz *( "/" segment ) ]
31
     *  path-noscheme = segment-nz-nc *( "/" segment )
32
     *  path-rootless = segment-nz *( "/" segment )
33
     *  path-empty    = 0<pchar>
34
     *  segment       = *pchar
35
     *  segment-nz    = 1*pchar
36
     *  segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
37
     *  ; non-zero-length segment without any colon ":"
38
     *
39
     *  pchar         = unreserved / pct-encoded / sub-delims / ":" / "@
40
     *
41
     * @see https://tools.ietf.org/html/rfc3986#page-22
42
     *
43
     * @var array
44
     */
45
    private $path = [];
46
47
    //////////////////////////
48
    /// GETTER FUNCTIONS  ///
49
    ////////////////////////
50
51
    /**
52
     * @return array
53
     */
54
    public function getPath(): array
55
    {
56
        return $this->path;
57
    }
58
59
    /**
60
     * @return string|null
61
     */
62
    public function first(): ?string
63
    {
64
        return $this->firstInPath($this->path);
65
    }
66
67
    /**
68
     * @return null|string
69
     */
70
    public function last()
71
    {
72
        return $this->lastInPath($this->path);
73
    }
74
75
    /**
76
     * @param string $value
77
     *
78
     * @return FtpMutablePath
79
     */
80
    public function append(string $value): self
81
    {
82
        $this->mutatorAppend($this->path, $value);
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param string $value
89
     *
90
     * @return FtpMutablePath
91
     */
92
    public function prepend(string $value): self
93
    {
94
        $this->mutatorPrepend($this->path, $value);
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param string      $value
101
     * @param string|null $first
102
     * @param string|null $last
103
     *
104
     * @return FtpMutablePath
105
     * @throws ComponentNotFoundException
106
     */
107
    public function putInBetween(string $value, string $first = null, string $last = null): self
108
    {
109
        if (null === $first && null === $last) {
110
            throw new \LogicException('Cannot put value if neither first or last is defined');
111
        }
112
113
        if (!$this->hasValueIn($this->path, $first) && !$this->hasValueIn($this->path, $last)) {
114
            throw new ComponentNotFoundException(sprintf('No component found matching either %s  %s', $first, $last));
115
        }
116
117
        $this->mutatorPutInBetweenKeys($this->path, $value, $first, $last);
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param string $before
124
     * @param string $value
125
     *
126
     * @return FtpMutablePath
127
     * @throws \LogicException
128
     */
129
    public function putBefore(string $before, string $value) : self
130
    {
131
        if (!$this->hasValueIn($this->path, $before)) {
132
            throw new \LogicException(sprintf('Cannot put value %s before %s as %s does not exist', $value, $before, $before));
133
        }
134
135
        $this->path = $this->mutatorPutBeforeValue($this->path, $before, $value);
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param string $after
142
     * @param string $value
143
     *
144
     * @return FtpMutablePath
145
     * @throws \LogicException
146
     */
147
    public function putAfter(string $after, string $value): self
148
    {
149
        if (!$this->hasValueIn($this->path, $after)) {
150
            throw new \LogicException(sprintf('Cannot put value %s after %s as %s does not exist', $value, $after, $after));
151
        }
152
153
        $this->path = $this->mutatorPutAfterValue($this->path, $after, $value);
154
155
        return $this;
156
    }
157
158
    /**
159
     * @param string ...$args
160
     *
161
     * @return FtpMutablePath
162
     */
163
    public function forget(string ...$args): self
164
    {
165
        foreach ($args as $item) {
166
            if (!$this->hasValueIn($this->path, $item)) {
167
                throw new \LogicException(sprintf('Cannot forget %s as it does not exist', $item));
168
            }
169
170
            $this->mutatorForgetKeyOrValue($this->path, $item);
171
        }
172
173
        return $this;
174
    }
175
176
    /**
177
     * @return FtpMutablePath
178
     */
179
    public function forgetAll(): self
180
    {
181
        $this->path = [];
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param string ...$args
188
     * @return array
189
     */
190
    public function only(string ...$args): array
191
    {
192
        return $this->mutatorOnlyPathValues($this->path, $args);
193
    }
194
195
    /////////////////////////////////
196
    /// INTERFACE IMPLEMENTATION ///
197
    ///////////////////////////////
198
199
    /**
200
     * @param $key
201
     * @return mixed
202
     * @throws ComponentNotFoundException
203
     */
204
    public function get($key)
205
    {
206
        return $this->getKeyIn($this->path, $key);
207
    }
208
209
    /**
210
     * @param $key
211
     * @return bool
212
     */
213
    public function has($key): bool
214
    {
215
        return $this->hasKeyIn($this->path, $key);
216
    }
217
218
    /**
219
     * Returns all the components of the query or path
220
     *
221
     * @return array
222
     */
223
    public function all(): array
224
    {
225
        return $this->path;
226
    }
227
228
    /**
229
     * Return the raw unaltered query or path
230
     *
231
     * @return string
232
     */
233
    public function raw(): string
234
    {
235
        if (empty($this->path)) {
236
            return '';
237
        }
238
239
        $path = '/';
240
        foreach ($this->path as $element) {
241
            $path .= $element.'/';
242
        }
243
244
        return $path;
245
    }
246
247
    /**
248
     * Returns the encoded query or path string
249
     *
250
     * @return string
251
     */
252
    public function encoded(): string
253
    {
254
        if (empty($this->path)) {
255
            return '';
256
        }
257
258
        $path = '/';
259
        foreach ($this->path as $element) {
260
            $path .= urlencode($element).'/';
261
        }
262
263
        return $path;
264
    }
265
266
    /**
267
     * @param $key
268
     * @param $value
269
     *
270
     * @return FtpMutablePath
271
     */
272
    public function set($key, $value): self
273
    {
274
        $this->path[$key] = $value;
275
276
        return $this;
277
    }
278
}
279