Test Failed
Push — master ( 0bc352...8c447f )
by Todd
03:54
created

Config::import()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Logikos\Util;
4
5
use Logikos\Util\Config\PathEval;
6
7
/**
8
 * This is largely inspired by \Phalcon\Config - https://docs.phalconphp.com/hr/3.2/api/Phalcon_Config
9
 * NOTICE: \Phalcon\Config will be much faster than this class and you are encouraged to use it
10
 * @see ../docs/config/README.md
11
 */
12
abstract class Config extends Registry {
13
  private $locked   = false;
14
15
  /**
16
   * Config constructor.
17
   * @param array $arrayConfig
18
   * @throws CanNotMutateException
19
   */
20 61
  public function __construct(array $arrayConfig = []) {
21 61
    parent::__construct();
22 61
23 50
    $this->import($this->defaults());
24
    $this->import($arrayConfig);
25 61
    $this->onConstruct();
26 61
  }
27
28
  public function import(array $data) {
29
    foreach ($data as $key=>$value) $this->offsetSet($key, $value);
30
  }
31 51
32 51
  // override this to have a base config
33
  protected function defaults(): array {
34
    return [];
35 17
  }
36 17
37
  // override this if you want to
38
  protected function onConstruct() {}
39 1
40 1
  public function isLocked() {
41
    return $this->locked;
42
  }
43 6
44 6
  public function get($key, $default = null) {
45 6
    return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
46
  }
47 6
48 6
  public function has($key) {
49 6
    return $this->offsetExists($key);
50
  }
51
52
  public function toArray() {
53 6
    return array_map(
54 6
        function ($value) {
55 6
          // $value may be another Config object, if so then we toArray() it also.
56
          return $this->hasToArray($value) ? $value->toArray() : $value;
57
        },
58 5
        $this->rawValues()
59 5
    );
60
  }
61
62
  private function hasToArray($value): bool {
63
    return is_object($value)
64
        && method_exists($value, 'toArray');
65
  }
66
67
  public function path($path, $default = null, $delimiter = '.') {
68
    return (new PathEval($this))->find($path, $default, $delimiter);
69
  }
70 51
71 51
72 51
  # ArrayAccess
73 51
74 51
  /**
75
   * @param $offset
76 51
   * @param $value
77
   * @throws CanNotMutateException
78
   */
79
  public function offsetSet($offset, $value) {
80
    $this->blockIfLocked();
81
    parent::offsetSet($offset, $this->settableValue($value));
82 4
  }
83 4
84 2
  protected function settableValue($value) {
85 2
    if (is_array($value)) return $this->subConfig($value);
86
    return $value;
87
  }
88 1
89 1
  protected function subConfig($arrayConfig = []) {
90
    return new class($arrayConfig) extends Config {};
91
  }
92 12
93 12
  /**
94 12
   * @param $offset
95
   * @throws CanNotMutateException
96 5
   */
97 5
  public function offsetUnset($offset) {
98
    $this->blockIfLocked();
99
    parent::offsetUnset($offset);
100
  }
101
102
103 51
  public static function __set_state(array $data): Config {
104 51
    return new static($data);
105 6
  }
106 51
107
  public function lock() {
108
    $this->locked = true;
109
  }
110
111
  protected function isConfigObject($value) {
112
    return $value instanceof Config;
113
  }
114
115
  /**
116
   * @throws CanNotMutateException
117
   */
118
  private function blockIfLocked() {
119
    if ($this->isLocked())
120
      throw new CanNotMutateException();
121
  }
122
}