|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Yiisoft\Yii\Cycle\Config\Exception\PropertyNotFoundException; |
|
6
|
|
|
|
|
7
|
|
|
class BaseConfig |
|
8
|
|
|
{ |
|
9
|
|
|
public function __get($name) |
|
10
|
|
|
{ |
|
11
|
|
|
$getter = 'get' . ucfirst($name); |
|
12
|
|
|
if (method_exists($this, $getter)) { |
|
13
|
|
|
return $this->$getter(); |
|
14
|
|
|
} |
|
15
|
|
|
if (property_exists($this, $name)) { |
|
16
|
|
|
return $this->$name; |
|
17
|
|
|
} |
|
18
|
|
|
throw new PropertyNotFoundException("The `$name` property not found in a " . static::class . " object"); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function __call($name, $arguments) |
|
22
|
|
|
{ |
|
23
|
|
|
$prefix = substr($name, 0, 3); |
|
24
|
|
|
if ($prefix === 'get') { |
|
25
|
|
|
$prop = lcfirst(substr($name, 3)); |
|
26
|
|
|
if (!property_exists($this, $prop)) { |
|
27
|
|
|
throw new PropertyNotFoundException("The `$name` property was not found when the `$name` method was called"); |
|
28
|
|
|
} |
|
29
|
|
|
return $this->$prop; |
|
30
|
|
|
} |
|
31
|
|
|
throw new \BadMethodCallException(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function configure(array $params): void |
|
35
|
|
|
{ |
|
36
|
|
|
foreach ($params as $name => $value) { |
|
37
|
|
|
$setter = 'set' . ucfirst($name); |
|
38
|
|
|
if (method_exists($this, $setter)) { |
|
39
|
|
|
$this->$setter($value); |
|
40
|
|
|
} elseif (property_exists($this, $name)) { |
|
41
|
|
|
$this->$name = $value; |
|
42
|
|
|
} else { |
|
43
|
|
|
throw new PropertyNotFoundException("The `$name` property not found when configure " . static::class . " object"); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Return all public and protected properties as array. |
|
50
|
|
|
* Property values will be obtained using getter methods. |
|
51
|
|
|
* Note: all private properties will be ignored! |
|
52
|
|
|
* @return array |
|
53
|
|
|
* @throws PropertyNotFoundException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function toArray(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$result = []; |
|
58
|
|
|
$arrayed = (array)$this; |
|
59
|
|
|
$keys = array_keys($arrayed); |
|
60
|
|
|
foreach ($keys as $key) { |
|
61
|
|
|
if ($key[0] !== chr(0)) { |
|
62
|
|
|
// public property |
|
63
|
|
|
|
|
64
|
|
|
$result[$key] = $this->__get($key); |
|
65
|
|
|
} elseif ($key[1] === '*') { |
|
66
|
|
|
// protected property |
|
67
|
|
|
|
|
68
|
|
|
$key = substr($key, 3); |
|
69
|
|
|
$result[$key] = $this->__get($key); |
|
70
|
|
|
} else { |
|
71
|
|
|
// private property |
|
72
|
|
|
|
|
73
|
|
|
// ignore |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|