|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert\Options; |
|
4
|
|
|
|
|
5
|
|
|
use WebPConvert\Options\Exceptions\InvalidOptionValueException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* (base) option class. |
|
9
|
|
|
* |
|
10
|
|
|
* @package WebPConvert |
|
11
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
12
|
|
|
* @since Class available since Release 2.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Option |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string The id of the option */ |
|
17
|
|
|
protected $id; |
|
18
|
|
|
|
|
19
|
|
|
/** @var mixed The default value of the option */ |
|
20
|
|
|
protected $defaultValue; |
|
21
|
|
|
|
|
22
|
|
|
/** @var mixed The value of the option */ |
|
23
|
|
|
protected $value; |
|
24
|
|
|
|
|
25
|
|
|
/** @var boolean Whether the value has been explicitly set */ |
|
26
|
|
|
protected $isExplicitlySet = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $id id of the option |
|
32
|
|
|
* @param string $defaultValue default value for the option |
|
33
|
|
|
* @throws InvalidOptionValueException if the default value cannot pass the check |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
public function __construct($id, $defaultValue) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->id = $id; |
|
40
|
|
|
$this->defaultValue = $defaultValue; |
|
41
|
|
|
|
|
42
|
|
|
// Check that default value is ok |
|
43
|
|
|
$this->check(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get Id. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string The id of the option |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getId() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->id; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get default value. |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed The default value for the option |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getDefaultValue() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->defaultValue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get value, or default value if value has not been explicitly set. |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed The value/default value |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getValue() |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$this->isExplicitlySet) { |
|
75
|
|
|
return $this->defaultValue; |
|
76
|
|
|
} else { |
|
77
|
|
|
return $this->value; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set value |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed $value The value |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function setValue($value) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->isExplicitlySet = true; |
|
90
|
|
|
$this->value = $value; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Check if the value is valid. |
|
95
|
|
|
* |
|
96
|
|
|
* This base class does no checking, but this method is overridden by most other options. |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
public function check() |
|
100
|
|
|
{ |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Helpful function for checking type - used by subclasses. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $expectedType The expected type, ie 'string' |
|
107
|
|
|
* @throws InvalidOptionValueException If the type is invalid |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function checkType($expectedType) |
|
111
|
|
|
{ |
|
112
|
|
|
if (gettype($this->getValue()) != $expectedType) { |
|
113
|
|
|
throw new InvalidOptionValueException( |
|
114
|
|
|
'The "' . $this->id . '" option must be a ' . $expectedType . |
|
115
|
|
|
' (you provided a ' . gettype($this->getValue()) . ')' |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|