Value   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 52.78%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 19
cts 36
cp 0.5278
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A __toString() 0 4 1
A jsonSerialize() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A count() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A offsetExists() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Ariadne Component Library.
4
 *
5
 * (c) Muze <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace arc\path;
12
13
/**
14
 * This class is a value object for a collapsed path. It behaves as close as possible like a string.
15
 * But you can't change it. Serializing it to json or php will return a string. You can access characters
16
 * by position, like a string. You can count() it.
17
 * \arc\path::collapse() will return an instance of this class. If the collapsed path is identical to
18
 * another path Value, \arc\path::collapse will return that value. This means that you can use a ===
19
 * to compare path Value objects, as long as you only use \arc\path::collapse() to create them.
20
 */
21
final class Value implements \JsonSerializable, \Serializable, \ArrayAccess, \Countable {
22
23
    private $path = '';
24
25 14
	public function __construct($path)
26
	{
27 14
        $path = str_replace('\\', '/', (string) $path);
28 14
        $this->path = \arc\path::reduce(
29 14
            $path,
30 14
            function ($result, $entry) {
31 14
                if ($entry == '..' ) {
32 2
                    $result = dirname( $result );
33 2
                    if (isset($result[1])) { // fast check to see if there is a dirname
34 2
                        $result .= '/';
35
                    } else {
36 2
                        $result = '/';
37
                    }
38 14
                } else if ($entry !== '.') {
39 14
                    $result .= $entry .'/';
40
                }
41 14
                return $result;
42 14
            },
43 14
            '/' // initial value, always start paths with a '/'
44
        );
45 14
	}
46
47
48 16
	public function __toString()
49
	{
50 16
		return $this->path;
51
	}
52
53
	public function jsonSerialize()
54
	{
55
		return $this->path;
56
	}
57
58
	public function serialize()
59
	{
60
		return serialize($this->path);
61
	}
62
63
	public function unserialize($data)
64
	{
65
		$this->path = unserialize($data);
66
	}
67
68
	public function count()
69
	{
70
		return count($this->path);
71
	}
72
73
	public function offsetGet($offset)
74
	{
75
		return $this->path[$offset];
76
	}
77
78
	public function offsetSet($offset, $char)
79
	{
80
		throw new \LogicException('\arc\path\Value is immutable, cast it to a string to change it');
81
	}
82
83
	public function offsetUnset($offset)
84
	{
85
		throw new \LogicException('\arc\path\Value is immutable, cast it to a string to change it');
86
	}
87
88
	public function offsetExists($offset)
89
	{
90
		return isset($this->path[$offset]);
91
	}
92
}