Xref::hasData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ConfigToken\TreeCompiler;
4
5
use ConfigToken\TreeCompiler\XrefResolver\XrefResolverFactory;
6
7
8
class Xref
9
{
10
    /** @var string */
11
    protected $type;
12
13
    /** @var string */
14
    protected $location;
15
16
    /** @var string */
17
    protected $contentType;
18
19
    /** @var array */
20
    protected $data;
21
22
    /** @var boolean */
23
    protected $resolved = false;
24
25
26 7
    public function __construct($type, $location)
27
    {
28 7
        $this->setType($type);
29 7
        $this->setLocation($location);
30 7
    }
31
32
    /**
33
     * Get the type and location from the definition string.
34
     *
35
     * @param string $typeAndLocation
36
     * @param string $delimiter
37
     * @return array Type and Location.
38
     * @throws \Exception
39
     */
40 1
    public static function parseDefinitionString($typeAndLocation, $delimiter)
41
    {
42 1
        $k = strpos($typeAndLocation, $delimiter);
43 1
        if ($k === false) {
44
            throw new \Exception(sprintf('Missing Xref type in "%s".', $typeAndLocation));
45
        }
46 1
        return array(substr($typeAndLocation, 0, $k), substr($typeAndLocation, $k + 1));
47
    }
48
49
    /**
50
     * Get the id from the definition string.
51
     *
52
     * @param string $typeAndLocation
53
     * @param string $delimiter
54
     * @return string
55
     * @throws \Exception
56
     */
57
    public static function getIdFromDefinitionString($typeAndLocation, $delimiter)
58
    {
59
        list($type, $location) = static::parseDefinitionString($typeAndLocation, $delimiter);
60
        return static::computeId($type, $location);
61
    }
62
63
    /**
64
     * Create Xref instance based on the given definition string.
65
     *
66
     * @param string $typeAndLocation
67
     * @param string $delimiter
68
     * @return Xref
69
     * @throws \Exception
70
     */
71
    public static function makeFromDefinitionString($typeAndLocation, $delimiter)
72
    {
73
        list($type, $location) = static::parseDefinitionString($typeAndLocation, $delimiter);
74
        $xref = new static($type, $location);
75
        return $xref;
76
    }
77
78 7
    public function isResolved()
79
    {
80 7
        return $this->resolved;
81
    }
82
83 7
    public function setResolved($value)
84
    {
85 7
        $this->resolved = $value;
86
87 7
        return $this;
88
    }
89
90 7
    public function getResolver()
91
    {
92 7
        return XrefResolverFactory::getByType($this->type);
93
    }
94
95 7
    public function resolve($force = false)
96
    {
97 7
        if ($this->isResolved() && (!$force)) {
98 7
            return;
99
        }
100
        if (!$this->hasType()) {
101
            throw new \Exception('Unable to resolve Xref without type.');
102
        }
103
        $resolver = $this->getResolver();
104
        $resolver::resolve($this, $force);
105
    }
106
107
    public function hasType()
108
    {
109
        return isset($this->type);
110
    }
111
112
    public function getType()
113
    {
114
        return $this->type;
115
    }
116
117 7
    public function setType($value)
118
    {
119 7
        $this->type = $value;
120
121 7
        return $this;
122
    }
123
124
    public function hasLocation()
125
    {
126
        return isset($this->location);
127
    }
128
129
    public function getLocation()
130
    {
131
        return $this->location;
132
    }
133
134 7
    public static function computeAbsoluteLocation($xrefType, $xrefLocation, $xrefPath)
135
    {
136 7
        $resolver = XrefResolverFactory::getByType($xrefType);
137 7
        return $resolver::getAbsoluteLocation($xrefLocation, $xrefPath);
138
    }
139
140 7
    public static function computeId($type, $location)
141
    {
142 7
        return md5($type . $location);
143
    }
144
145 7
    public function getId()
146
    {
147 7
        return static::computeId($this->type, $this->location);
148
    }
149
150 7
    public function setLocation($value)
151
    {
152 7
        if (isset($value)) {
153 7
            $resolver = $this->getResolver();
154 7
            if (isset($resolver)) {
155 7
                $value = $resolver->getPlatformSpecificLocation($value);
156 7
            }
157 7
        }
158 7
        $this->location = $value;
159
160 7
        return $this;
161
    }
162
163
    public function hasContentType()
164
    {
165
        return isset($this->contentType);
166
    }
167
168
    public function getContentType()
169
    {
170
        return $this->contentType;
171
    }
172
173
    public function setContentType($value)
174
    {
175
        $this->contentType = $value;
176
        return $this;
177
    }
178
179
    public function hasData()
180
    {
181
        return isset($this->data);
182
    }
183
184 7
    public function getData()
185
    {
186 7
        return $this->data;
187
    }
188
189 7
    public function setData($value)
190
    {
191 7
        $this->data = $value;
192
193 7
        return $this;
194
    }
195
196
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
197
    {
198
        return $this->getId() . ':' . $this->getType() . ':' . $this->getLocation();
199
    }
200
201
202
}