Passed
Branch master (7a2f1a)
by Petr
03:10
created

ArrayPath::setString()   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 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace kalanis\kw_paths;
4
5
6
/**
7
 * Class ArrayPath
8
 * @package kalanis\kw_paths
9
 * Path as an array
10
 */
11
class ArrayPath
12
{
13
    /** @var string[] */
14
    protected $path = [];
15
16
    /**
17
     * @throws PathsException
18
     * @return string
19
     */
20 3
    public function __toString()
21
    {
22 3
        return $this->getString();
23
    }
24
25
    /**
26
     * @param string $path
27
     * @throws PathsException
28
     * @return $this
29
     */
30 2
    public function setString(string $path): self
31
    {
32 2
        $this->path = array_filter(array_filter(Stuff::pathToArray($path), [Stuff::class, 'notDots']));
33 2
        return $this;
34
    }
35
36
    /**
37
     * @param string[] $path
38
     * @return $this
39
     */
40 1
    public function setArray(array $path): self
41
    {
42 1
        $this->path = array_filter(array_filter($path, [Stuff::class, 'notDots']));
43 1
        return $this;
44
    }
45
46
    /**
47
     * @throws PathsException
48
     * @return string
49
     */
50 3
    public function getString(): string
51
    {
52 3
        return Stuff::arrayToPath($this->path);
53
    }
54
55
    /**
56
     * @return string[]
57
     */
58 3
    public function getArray(): array
59
    {
60 3
        return array_merge($this->path, []); // remove indexes
61
    }
62
63
    /**
64
     * @throws PathsException
65
     * @return string
66
     */
67 3
    public function getStringDirectory(): string
68
    {
69 3
        $array = $this->getArrayDirectory();
70 3
        return empty($array)
71 2
            ? ''
72 3
            : Stuff::arrayToPath($array)
73
        ;
74
    }
75
76
    /**
77
     * @return string[]
78
     */
79 3
    public function getArrayDirectory(): array
80
    {
81 3
        return (1 < count($this->path))
82 1
            ? array_slice($this->path, 0, -1)
83 3
            : []
84
        ;
85
    }
86
87 3
    public function getFileName(): string
88
    {
89 3
        return (0 < count($this->path))
90 2
            ? end($this->path)
91 3
            : ''
92
        ;
93
    }
94
}
95