ArrayPath::getString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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