Completed
Push — master ( ea07b3...ae46c6 )
by Todd
01:50
created

Config::isConfigObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Logikos\Util;
4
5
/**
6
 * This is largely inspired by \Phalcon\Config - https://docs.phalconphp.com/hr/3.2/api/Phalcon_Config
7
 * NOTICE: \Phalcon\Config will be much faster than this class and you are encouraged to use it
8
 * @see ../docs/config/README.md
9
 */
10
abstract class Config implements \ArrayAccess, \Countable, \Iterator {
11
  private $locked   = false;
12
  private $values   = [];
13
14 49
  public function __construct(array $arrayConfig = []) {
15 49
    foreach($arrayConfig as $key => $value)
16 44
      $this->offsetSet($key, $value);
17
18 49
    $this->onConstruct();
19 49
  }
20
21
  // override this if you want to
22
  protected function onConstruct() {}
23
24 44
  public function isLocked() {
25 44
    return $this->locked;
26
  }
27
28 7
  public function get($key, $default = null) {
29 7
    return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
30
  }
31
32 1
  public function has($key) {
33 1
    return $this->offsetExists($key);
34
  }
35
36 6
  public function toArray() {
37 6
    $array = [];
38
39 6
    foreach ($this->values as $key => $value)
40 6
      $array[$key] = $this->extractValue($value);
41
42 6
    return $array;
43
  }
44
45
  /**
46
   * @param $value Config|mixed
47
   * @return mixed
48
   */
49 6
  private function extractValue($value) {
50 6
    return $this->hasToArray($value)
51 3
        ? $value->toArray()
52 6
        : $value;
53
  }
54
55 6
  private function hasToArray($value): bool {
56 6
    return is_object($value) && method_exists($value, 'toArray');
57
  }
58
59 4
  public function path($path, $default = null, $delimiter = '.') {
60 4
    $tokens = explode($delimiter, $path);
61 4
    $token  = array_shift($tokens);
62 4
    if ($this->get($token) instanceof self)
63 2
      return $this[$token]->path(implode('.', $tokens), $default);
64 4
    return $this->get($token, $default);
65
  }
66
67
68
  # Countable
69 3
  public function count() {
70 3
    return count($this->values);
71
  }
72
73
74
  # ArrayAccess
75 30
  public function offsetExists($offset) {
76 30
    return array_key_exists($offset, $this->values);
77
  }
78
79 18
  public function offsetGet($offset) {
80 18
    if (!$this->offsetExists($offset))
81 2
      throw new \OutOfBoundsException("offset '{$offset}' does not exist");
82 16
    return $this->values[$offset];
83
  }
84
85 44
  public function offsetSet($offset, $value) {
86 44
    $this->blockIfLocked();
87 44
    $this->values[strval($offset)] = is_array($value)
88 10
        ? new static($value)
89 44
        : $value;
90 44
  }
91
92 4
  public function offsetUnset($offset) {
93 4
    $this->blockIfLocked();
94 2
    unset($this->values[strval($offset)]);
95 2
  }
96
97
98
  # Iterator
99
  public function rewind()  { return reset($this->values);        }
100
  public function key()     { return key($this->values);          }
101
  public function current() { return current($this->values);      }
102
  public function next()    { return next($this->values);         }
103
  public function valid()   { return key($this->values) !== null; }
104
105
106
  # Magic Property Access
107
  public function __set($offset, $value) { $this->offsetSet($offset, $value);   }
108
  public function __unset($offset)       { $this->offsetUnset($offset);         }
109
  public function __get($offset)         { return $this->offsetGet($offset);    }
110
  public function __isset($offset)       { return $this->offsetExists($offset); }
111
112
  /**
113
   * @param array $data
114
   * @return static
115
   */
116 1
  public static function __set_state(array $data) {
117 1
    return new static($data);
118
  }
119
120 12
  public function lock() {
121 12
    $this->locked = true;
122 12
  }
123
124 5
  protected function rawValues() {
125 5
    return $this->values;
126
  }
127
128 5
  protected function isConfigObject($value) {
129 5
    return $value instanceof Config;
130
  }
131
132 44
  private function blockIfLocked() {
133 44
    if ($this->isLocked())
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->isLocked() targeting Logikos\Util\Config::isLocked() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
134 6
      throw new CanNotMutateException();
135
  }
136
}