|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Bluz\Common; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Options Trait |
|
15
|
|
|
* |
|
16
|
|
|
* Example of usage |
|
17
|
|
|
* class Foo |
|
18
|
|
|
* { |
|
19
|
|
|
* use \Bluz\Common\Options; |
|
20
|
|
|
* |
|
21
|
|
|
* protected $bar = ''; |
|
22
|
|
|
* protected $baz = ''; |
|
23
|
|
|
* |
|
24
|
|
|
* public function setBar($value) |
|
25
|
|
|
* { |
|
26
|
|
|
* $this->bar = $value; |
|
27
|
|
|
* } |
|
28
|
|
|
* |
|
29
|
|
|
* public function setBaz($value) |
|
30
|
|
|
* { |
|
31
|
|
|
* $this->baz = $value; |
|
32
|
|
|
* } |
|
33
|
|
|
* } |
|
34
|
|
|
* |
|
35
|
|
|
* $Foo = new Foo(); |
|
36
|
|
|
* $Foo->setOptions(['bar'=>123, 'baz'=>456]); |
|
37
|
|
|
* |
|
38
|
|
|
* @package Bluz\Common |
|
39
|
|
|
* @author Anton Shevchuk |
|
40
|
|
|
* @link https://github.com/bluzphp/framework/wiki/Trait-Options |
|
41
|
|
|
*/ |
|
42
|
|
|
trait Options |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var array options store |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $options; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get option by key |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $key |
|
53
|
|
|
* @param array $keys |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
9 |
|
public function getOption($key, ...$keys) |
|
57
|
|
|
{ |
|
58
|
9 |
|
$method = 'get' . Line::toCamelCase($key); |
|
59
|
9 |
|
if (method_exists($this, $method)) { |
|
60
|
1 |
|
return $this->$method($key, ...$keys); |
|
61
|
|
|
} |
|
62
|
9 |
|
return Collection::get($this->options, $key, ...$keys); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Set option by key over setter |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param string $value |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
736 |
|
public function setOption($key, $value) |
|
73
|
|
|
{ |
|
74
|
736 |
|
$method = 'set' . Line::toCamelCase($key); |
|
75
|
736 |
|
if (method_exists($this, $method)) { |
|
76
|
734 |
|
$this->$method($value); |
|
77
|
|
|
} else { |
|
78
|
715 |
|
$this->options[$key] = $value; |
|
79
|
|
|
} |
|
80
|
736 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get all options |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getOptions() : array |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->options; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Setup, check and init options |
|
94
|
|
|
* |
|
95
|
|
|
* Requirements |
|
96
|
|
|
* - options must be a array |
|
97
|
|
|
* - options can be null |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $options |
|
100
|
|
|
* @return self |
|
101
|
|
|
*/ |
|
102
|
734 |
|
public function setOptions(array $options = null) |
|
103
|
|
|
{ |
|
104
|
|
|
// store options by default |
|
105
|
734 |
|
$this->options = (array) $options; |
|
106
|
|
|
|
|
107
|
|
|
// apply options |
|
108
|
734 |
|
foreach ($this->options as $key => $value) { |
|
109
|
734 |
|
$this->setOption($key, $value); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// check and initialize options |
|
113
|
734 |
|
$this->initOptions(); |
|
114
|
|
|
|
|
115
|
734 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Check and initialize options in package |
|
120
|
|
|
* |
|
121
|
|
|
* @throws \Bluz\Config\ConfigException |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
734 |
|
protected function initOptions() |
|
125
|
|
|
{ |
|
126
|
734 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|