Completed
Push — master ( e53be3...abc481 )
by Lars
03:12
created

AbstractPart::setData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of the Purl package, a project by Jonathan H. Wage.
5
 *
6
 * (c) 2013 Jonathan H. Wage
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Purl;
13
14
/**
15
 * AbstractPart class is implemented by each part of a Url where necessary.
16
 *
17
 * @author      Jonathan H. Wage <[email protected]>
18
 * @implements  ArrayAccess
19
 */
20
abstract class AbstractPart implements \ArrayAccess
21
{
22
  /**
23
   * Flag for whether or not this part has been initialized.
24
   *
25
   * @var boolean
26
   */
27
  protected $initialized = false;
28
29
  /**
30
   * Array of data for this part.
31
   *
32
   * @var array
33
   */
34
  protected $data = [];
35
36
  /**
37
   * Array mapping part names to classes.
38
   *
39
   * @var array
40
   */
41
  protected $partClassMap = [];
42
43
  /**
44
   * Gets the data for this part. This method will initialize the part if it is not already initialized.
45
   *
46
   * @return array
47
   */
48 3
  public function getData(): array
49
  {
50 3
    $this->initialize();
51
52 3
    return $this->data;
53
  }
54
55
  /**
56
   * Sets the data for this part. This method will initialize the part if it is not already initialized.
57
   *
58
   * @param array $data
59
   *
60
   * @return $this
61
   */
62 1
  public function setData(array $data)
63
  {
64 1
    $this->initialize();
65 1
    $this->data = $data;
66
67 1
    foreach ($data as $key => $value) {
68 1
      if (empty($value) && $value !== '0') {
69 1
        unset($this->data[$key]);
70
      }
71
    }
72
73 1
    return $this;
74
  }
75
76
  /**
77
   * Check if this part has been initialized yet.
78
   *
79
   * @return boolean
80
   */
81
  public function isInitialized(): bool
82
  {
83
    return $this->initialized;
84
  }
85
86
  /**
87
   * Check if this part has data by key.
88
   *
89
   * @param string $key
90
   *
91
   * @return boolean
92
   */
93
  public function has($key): bool
94
  {
95
    $this->initialize();
96
97
    return isset($this->data[$key]);
98
  }
99
100
  /**
101
   * Gets data from this part by key.
102
   *
103
   * @param string $key
104
   *
105
   * @return mixed|null
106
   */
107 65
  public function get($key)
108
  {
109 65
    $this->initialize();
110
111 65
    return $this->data[$key] ?? null;
112
  }
113
114
  /**
115
   * Set data for this part by key.
116
   *
117
   * @param string $key
118
   * @param mixed  $value
119
   *
120
   * @return static
121
   */
122 6
  public function set($key, $value)
123
  {
124 6
    $this->initialize();
125 6
    $this->data[$key] = $value;
126
127 6
    return $this;
128
  }
129
130
  /**
131
   * Add data for this part.
132
   *
133
   * @param mixed $value
134
   *
135
   * @return $this
136
   */
137 3
  public function add($value)
138
  {
139 3
    $this->initialize();
140 3
    $this->data[] = $value;
141
142 3
    return $this;
143
  }
144
145
  /**
146
   * Remove data from this part by key.
147
   *
148
   * @param $key
149
   */
150
  public function remove($key)
151
  {
152
    $this->initialize();
153
    unset($this->data[$key]);
154
  }
155
156
  /** Property Overloading */
157
158
  /**
159
   * @param $key
160
   *
161
   * @return bool
162
   */
163
  public function __isset($key)
164
  {
165
    return $this->has($key);
166
  }
167
168
  /**
169
   * @param $key
170
   *
171
   * @return bool
172
   */
173 65
  public function __get($key)
174
  {
175 65
    return $this->get($key);
176
  }
177
178
  /**
179
   * @param $key
180
   * @param $value
181
   *
182
   * @return AbstractPart
183
   */
184 2
  public function __set($key, $value)
185
  {
186 2
    return $this->set($key, $value);
187
  }
188
189
  /**
190
   * @param $key
191
   */
192
  public function __unset($key)
193
  {
194
    return $this->remove($key);
195
  }
196
197
  /** ArrayAccess */
198
199
  /**
200
   * @param mixed $key
201
   *
202
   * @return bool
203
   */
204
  public function offsetExists($key): bool
205
  {
206
    $this->initialize();
207
208
    return isset($this->data[$key]);
209
  }
210
211
  /**
212
   * @param mixed $key
213
   *
214
   * @return bool
215
   */
216
  public function offsetGet($key): bool
217
  {
218
    return $this->get($key);
219
  }
220
221
  /**
222
   * @param mixed $key
223
   * @param mixed $value
224
   *
225
   * @return AbstractPart
226
   */
227 1
  public function offsetSet($key, $value): AbstractPart
228
  {
229 1
    return $this->set($key, $value);
230
  }
231
232
  /**
233
   * @param mixed $key
234
   */
235
  public function offsetUnset($key)
236
  {
237
    return $this->remove($key);
238
  }
239
240 73
  protected function initialize()
241
  {
242 73
    if ($this->initialized === true) {
243 65
      return;
244
    }
245
246 73
    $this->initialized = true;
247
248 73
    $this->doInitialize();
249 73
  }
250
251
  /**
252
   * Prepare a part value.
253
   *
254
   * @param string        $key
255
   * @param string|static $value
256
   *
257
   * @return static
258
   */
259 65
  protected function preparePartValue($key, &$value)
260
  {
261 65
    if (!isset($this->partClassMap[$key])) {
262 59
      return $value;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $value; of type string|Purl\AbstractPart is incompatible with the return type documented by Purl\AbstractPart::preparePartValue of type Purl\AbstractPart as it can also be of type string which is not included in this return type.
Loading history...
263
    }
264
265 65
    $className = $this->partClassMap[$key];
266
267 65
    return !$value instanceof $className ? new $className($value) : $value;
268
  }
269
270
  /**
271
   * Convert the instance back in to string form from the internal parts.
272
   *
273
   * @return string
274
   */
275
  abstract public function __toString();
276
277
  /**
278
   * Each part that extends AbstractPart must implement an doInitialize() method.
279
   *
280
   * @return void
281
   */
282
  abstract protected function doInitialize();
283
}
284