|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the SVN-Buddy library. |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
|
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Config; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class ChoiceConfigSetting extends AbstractConfigSetting |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Choices. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $_choices = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Creates choice config setting. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name Name. |
|
28
|
|
|
* @param array $choices Choices. |
|
29
|
|
|
* @param mixed $default Default value. |
|
30
|
|
|
* @param integer $scope_bit Scope. |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \InvalidArgumentException When no choices specified. |
|
33
|
|
|
*/ |
|
34
|
32 |
|
public function __construct($name, array $choices, $default, $scope_bit = null) |
|
35
|
|
|
{ |
|
36
|
32 |
|
if ( empty($choices) ) { |
|
37
|
1 |
|
throw new \InvalidArgumentException('The "$choices" parameter must not be empty.'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
31 |
|
$this->_choices = $choices; |
|
41
|
|
|
|
|
42
|
31 |
|
parent::__construct($name, $default, $scope_bit); |
|
43
|
29 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns choices. |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function getChoices() |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->_choices; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Converts value into scalar for used for storage. |
|
57
|
|
|
* |
|
58
|
|
|
* @param mixed $value Value. |
|
59
|
|
|
* |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
29 |
|
protected function convertToStorageFormat($value) |
|
63
|
|
|
{ |
|
64
|
29 |
|
return $this->getChoiceId($value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Performs value validation. |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $value Value. |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
* @throws \InvalidArgumentException When validation failed. |
|
74
|
|
|
*/ |
|
75
|
11 |
|
protected function validate($value) |
|
76
|
|
|
{ |
|
77
|
11 |
|
$choice_id = $this->getChoiceId($value); |
|
78
|
|
|
|
|
79
|
11 |
|
if ( $choice_id === null ) { |
|
80
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
81
|
1 |
|
'The "%s" config setting value must be one of "%s".', |
|
82
|
1 |
|
$this->getName(), |
|
83
|
1 |
|
implode('", "', array_keys($this->_choices)) |
|
84
|
1 |
|
)); |
|
85
|
|
|
} |
|
86
|
10 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Gets choice id from choice itself. |
|
90
|
|
|
* |
|
91
|
|
|
* @param mixed $choice Choice. |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
29 |
|
protected function getChoiceId($choice) |
|
96
|
|
|
{ |
|
97
|
29 |
|
$choice_id = array_search((string)$choice, $this->_choices); |
|
98
|
|
|
|
|
99
|
29 |
|
if ( $choice_id !== false ) { |
|
100
|
1 |
|
return $choice_id; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
29 |
|
return isset($this->_choices[$choice]) ? $choice : null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|