1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Spiral Framework package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Storage\Config\DTO\Traits; |
13
|
|
|
|
14
|
|
|
use Spiral\Storage\Exception\ConfigException; |
15
|
|
|
use Spiral\Storage\Config\DTO\FileSystemInfo\OptionsBasedInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait based on usage options array |
19
|
|
|
*/ |
20
|
|
|
trait OptionsTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $options = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check if option was defined |
29
|
|
|
* |
30
|
|
|
* @param string $key |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public function hasOption(string $key): bool |
35
|
|
|
{ |
36
|
|
|
return array_key_exists($key, $this->options); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get option value |
41
|
|
|
* |
42
|
|
|
* @param string $key |
43
|
|
|
* |
44
|
|
|
* @return mixed|null |
45
|
|
|
*/ |
46
|
|
|
public function getOption(string $key) |
47
|
|
|
{ |
48
|
|
|
return $this->hasOption($key) ? $this->options[$key] : null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check if option was defined with required type |
53
|
|
|
* |
54
|
|
|
* @param string $optionLabel |
55
|
|
|
* @param $optionVal |
56
|
|
|
* @param string $type |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
* |
60
|
|
|
* @throws ConfigException |
61
|
|
|
*/ |
62
|
|
|
protected function isOptionHasRequiredType(string $optionLabel, $optionVal, string $type): bool |
63
|
|
|
{ |
64
|
|
|
switch ($type) { |
65
|
|
|
case OptionsBasedInterface::INT_TYPE: |
66
|
|
|
case OptionsBasedInterface::FLOAT_TYPE: |
67
|
|
|
return is_numeric($optionVal); |
68
|
|
|
case OptionsBasedInterface::STRING_TYPE: |
69
|
|
|
return is_string($optionVal); |
70
|
|
|
case OptionsBasedInterface::BOOL_TYPE: |
71
|
|
|
return is_bool($optionVal) || in_array($optionVal, [0, 1, '0', '1'], true); |
72
|
|
|
case OptionsBasedInterface::ARRAY_TYPE: |
73
|
|
|
return is_array($optionVal); |
74
|
|
|
case OptionsBasedInterface::MIXED_TYPE: |
75
|
|
|
return true; |
76
|
|
|
default: |
77
|
|
|
throw new ConfigException( |
78
|
|
|
\sprintf( |
79
|
|
|
'Unknown option type detected for option `%s`: %s', |
80
|
|
|
$optionLabel, |
81
|
|
|
$type |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Process option value to required type |
89
|
|
|
* |
90
|
|
|
* @param $optionVal |
91
|
|
|
* @param string $type |
92
|
|
|
* |
93
|
|
|
* @return bool|float|int|string |
94
|
|
|
*/ |
95
|
|
|
protected function processOptionByType($optionVal, string $type) |
96
|
|
|
{ |
97
|
|
|
switch ($type) { |
98
|
|
|
case OptionsBasedInterface::INT_TYPE: |
99
|
|
|
return (int)$optionVal; |
100
|
|
|
case OptionsBasedInterface::FLOAT_TYPE: |
101
|
|
|
return (float)$optionVal; |
102
|
|
|
case OptionsBasedInterface::STRING_TYPE: |
103
|
|
|
return (string)$optionVal; |
104
|
|
|
case OptionsBasedInterface::BOOL_TYPE: |
105
|
|
|
return (bool)$optionVal; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $optionVal; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Validate required options defined |
113
|
|
|
* |
114
|
|
|
* @param array $requiredOptions |
115
|
|
|
* @param array $options |
116
|
|
|
* @param string $msgPostfix |
117
|
|
|
* |
118
|
|
|
* @throws ConfigException |
119
|
|
|
*/ |
120
|
|
|
protected function validateRequiredOptions(array $requiredOptions, array $options, string $msgPostfix = ''): void |
121
|
|
|
{ |
122
|
|
|
foreach ($requiredOptions as $requiredOption) { |
123
|
|
|
if (!array_key_exists($requiredOption, $options)) { |
124
|
|
|
throw new ConfigException( |
125
|
|
|
\sprintf('Option `%s` not detected%s', $requiredOption, $msgPostfix) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|