1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | */ |
||
5 | |||
6 | namespace Mvc5\Config; |
||
7 | |||
8 | trait Config |
||
9 | { |
||
10 | /** |
||
11 | * |
||
12 | */ |
||
13 | use ArrayAccess; |
||
14 | use PropertyAccess; |
||
15 | |||
16 | /** |
||
17 | * @param array|string $name |
||
18 | * @return mixed |
||
19 | */ |
||
20 | 484 | function get($name) |
|
21 | { |
||
22 | 484 | if (is_string($name)) { |
|
23 | 484 | return $this->config[$name] ?? null; |
|
24 | } |
||
25 | |||
26 | 1 | $matched = []; |
|
27 | |||
28 | 1 | foreach($name as $key) { |
|
29 | 1 | $matched[$key] = $this->config[$key] ?? null; |
|
30 | } |
||
31 | |||
32 | 1 | return $matched; |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param array|string $name |
||
37 | * @return bool |
||
38 | */ |
||
39 | 493 | function has($name) : bool |
|
40 | { |
||
41 | 493 | if (is_string($name)) { |
|
42 | 493 | return isset($this->config[$name]); |
|
43 | } |
||
44 | |||
45 | 1 | foreach($name as $key) { |
|
46 | 1 | if (!isset($this->config[$key])) { |
|
47 | 1 | return false; |
|
48 | } |
||
49 | } |
||
50 | |||
51 | 1 | return true; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param array|string $name |
||
56 | */ |
||
57 | 12 | function remove($name) : void |
|
58 | { |
||
59 | 12 | foreach((array) $name as $key) { |
|
60 | 12 | unset($this->config[$key]); |
|
61 | } |
||
62 | 12 | } |
|
63 | |||
64 | /** |
||
65 | * @param array|string $name |
||
66 | * @param mixed $value |
||
67 | * @return mixed |
||
68 | */ |
||
69 | 146 | function set($name, $value = null) |
|
70 | { |
||
71 | 146 | if (is_string($name)) { |
|
72 | 130 | return $this->config[$name] = $value; |
|
73 | } |
||
74 | |||
75 | 53 | foreach($name as $key => $value) { |
|
76 | 53 | $this->config[$key] = $value; |
|
77 | } |
||
78 | |||
79 | 53 | return $name; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param array|string $name |
||
84 | * @param mixed $value |
||
85 | * @return Model|mixed |
||
86 | */ |
||
87 | 104 | function with($name, $value = null) : Model |
|
88 | { |
||
89 | 104 | $new = clone $this; |
|
90 | |||
91 | 104 | $new->config instanceof Immutable |
|
92 | 1 | ? $new->config = $new->config->with($name, $value) |
|
93 | 104 | : $new->set($name, $value); |
|
94 | |||
95 | 104 | return $new; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param array|string $name |
||
100 | * @return Model|mixed |
||
101 | */ |
||
102 | 5 | function without($name) : Model |
|
103 | { |
||
104 | 5 | $new = clone $this; |
|
105 | |||
106 | 5 | $new->config instanceof Immutable |
|
107 | 1 | ? $new->config = $new->config->without($name) |
|
108 | 5 | : $new->remove($name); |
|
109 | |||
110 | 5 | return $new; |
|
0 ignored issues
–
show
|
|||
111 | } |
||
112 | } |
||
113 |