Completed
Branch full-rewrite (4754d3)
by Thibaud
03:13
created

ResourceUri::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Alchemy\Zippy\Resource;
4
5
final class ResourceUri
6
{
7
8
    const DEFAULT_PROTOCOL = 'file';
9
10
    const PROTOCOL_SEPARATOR = '://';
11
12
    /**
13
     * @param $protocol
14
     * @param $resource
15
     * @return self
16
     */
17
    public static function fromProtocolAndResource($protocol, $resource)
18
    {
19
        return new self($protocol . self::PROTOCOL_SEPARATOR . $resource);
20
    }
21
22
    /**
23
     * @param $uri
24
     * @return bool
25
     */
26
    public static function isValidUri($uri)
27
    {
28
        if (strpos($uri, self::PROTOCOL_SEPARATOR) === false) {
29
            return false;
30
        }
31
32
        $parts = explode(self::PROTOCOL_SEPARATOR, $uri, 2);
33
34
        if (count(array_filter($parts, function ($value) { return $value != ''; })) !== 2) {
35
            return false;
36
        }
37
38
        if (strpos($parts[1], self::PROTOCOL_SEPARATOR) !== false) {
39
            return self::isValidUri($parts[1]);
40
        }
41
42
        return true;
43
    }
44
45
    public static function fromString($uri)
46
    {
47
        if (strpos($uri, self::PROTOCOL_SEPARATOR) === false) {
48
            $uri = self::DEFAULT_PROTOCOL . self::PROTOCOL_SEPARATOR . $uri;
49
        }
50
51
        return new self($uri);
52
    }
53
54
    public static function fromStringArray(array $uris)
55
    {
56
        $resourceUris = [];
57
58
        foreach ($uris as $uri) {
59
            $resourceUris[] = self::fromString($uri);
60
        }
61
62
63
        return $resourceUris;
64
    }
65
66
    /**
67
     * @var string
68
     */
69
    private $uri;
70
71
    /**
72
     * @var string
73
     */
74
    private $protocol;
75
76
    /**
77
     * @var string
78
     */
79
    private $resource;
80
81
    /**
82
     * @param string $uri
83
     */
84
    public function __construct($uri)
85
    {
86
        if (! self::isValidUri($uri)) {
87
            throw new \InvalidArgumentException(sprintf(
88
                'Malformed URI: required format is "protocol://resource", got "%s"',
89
                $uri
90
            ));
91
        }
92
93
        $this->uri = $uri;
94
95
        list ($this->protocol, $this->resource) = explode('://', $uri, 2);
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getUri()
102
    {
103
        return $this->uri;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getProtocol()
110
    {
111
        return $this->protocol;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getResource()
118
    {
119
        return $this->resource;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    public function hasChainedResource()
126
    {
127
        return self::isValidUri($this->resource);
128
    }
129
130
    /**
131
     * @return self
132
     */
133
    public function getChainedResource()
134
    {
135
       return new self($this->resource);
136
    }
137
138
    /**
139
     * @param ResourceUri $other
140
     * @return bool
141
     */
142
    public function equals(ResourceUri $other)
143
    {
144
        return $this->getUri() == $other->getUri();
145
    }
146
147
    /**
148
     * @return ResourceUri
149
     */
150
    public function __clone()
151
    {
152
        return new self($this->uri);
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function __toString()
159
    {
160
        return $this->uri;
161
    }
162
}
163