| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvert\Options; |
| 4 |
|
| 5 |
use WebPConvert\Options\Exceptions\InvalidOptionTypeException; |
| 6 |
use WebPConvert\Options\Exceptions\InvalidOptionValueException; |
| 7 |
|
| 8 |
/** |
| 9 |
* (base) option class. |
| 10 |
* |
| 11 |
* @package WebPConvert |
| 12 |
* @author Bjørn Rosell <[email protected]> |
| 13 |
* @since Class available since Release 2.0.0 |
| 14 |
*/ |
| 15 |
class Option |
| 16 |
{ |
| 17 |
/** @var string The id of the option */ |
| 18 |
protected $id; |
| 19 |
|
| 20 |
/** @var mixed The default value of the option */ |
| 21 |
protected $defaultValue; |
| 22 |
|
| 23 |
/** @var mixed The value of the option */ |
| 24 |
protected $value; |
| 25 |
|
| 26 |
/** @var boolean Whether the value has been set (if not, getValue() will return the default value) */ |
| 27 |
protected $isExplicitlySet = false; |
| 28 |
|
| 29 |
/** @var string An option must supply a type id */ |
| 30 |
protected $typeId; |
| 31 |
|
| 32 |
/** @var array Type constraints for the value (JSON schema syntax) */ |
| 33 |
protected $schemaType = []; |
| 34 |
|
| 35 |
/** @var array|null Array of allowed values (JSON schema syntax) */ |
| 36 |
protected $enum = null; //https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values |
| 37 |
|
| 38 |
/** @var boolean Whether the option has been deprecated */ |
| 39 |
protected $deprecated = false; |
| 40 |
|
| 41 |
/** @var string Help text */ |
| 42 |
protected $helpText = ''; |
| 43 |
|
| 44 |
/** @var array UI Def */ |
| 45 |
protected $ui; |
| 46 |
|
| 47 |
/** @var array|null Extra Schema Def (ie holding 'title', 'description' or other)*/ |
| 48 |
protected $extraSchemaDefs; |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Constructor. |
| 53 |
* |
| 54 |
* @param string $id id of the option |
| 55 |
* @param mixed $defaultValue default value for the option |
| 56 |
* @throws InvalidOptionValueException if the default value cannot pass the check |
| 57 |
* @throws InvalidOptionTypeException if the default value is wrong type |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function __construct($id, $defaultValue) |
| 61 |
{ |
| 62 |
$this->id = $id; |
| 63 |
$this->defaultValue = $defaultValue; |
| 64 |
|
| 65 |
// Check that default value is ok |
| 66 |
$this->check(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get Id. |
| 71 |
* |
| 72 |
* @return string The id of the option |
| 73 |
*/ |
| 74 |
public function getId() |
| 75 |
{ |
| 76 |
return $this->id; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get Id. |
| 81 |
* |
| 82 |
* @param string $id The id of the option |
| 83 |
*/ |
| 84 |
public function setId($id) |
| 85 |
{ |
| 86 |
$this->id = $id; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get default value. |
| 91 |
* |
| 92 |
* @return mixed The default value for the option |
| 93 |
*/ |
| 94 |
public function getDefaultValue() |
| 95 |
{ |
| 96 |
return $this->defaultValue; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Get value, or default value if value has not been explicitly set. |
| 102 |
* |
| 103 |
* @return mixed The value/default value |
| 104 |
*/ |
| 105 |
public function getValue() |
| 106 |
{ |
| 107 |
if (!$this->isExplicitlySet) { |
| 108 |
return $this->defaultValue; |
| 109 |
} else { |
| 110 |
return $this->value; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get to know if value has been set. |
| 116 |
* |
| 117 |
* @return boolean Whether or not the value has been set explicitly |
| 118 |
*/ |
| 119 |
public function isValueExplicitlySet() |
| 120 |
{ |
| 121 |
return $this->isExplicitlySet; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Set value |
| 126 |
* |
| 127 |
* @param mixed $value The value |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function setValue($value) |
| 131 |
{ |
| 132 |
$this->isExplicitlySet = true; |
| 133 |
$this->value = $value; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Check if the value is valid. |
| 138 |
* |
| 139 |
* This base class does no checking, but this method is overridden by most other options. |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function check() |
| 143 |
{ |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Helpful function for checking type - used by subclasses. |
| 148 |
* |
| 149 |
* @param string $expectedType The expected type, ie 'string' |
| 150 |
* @throws InvalidOptionTypeException If the type is invalid |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
protected function checkType($expectedType) |
| 154 |
{ |
| 155 |
if (gettype($this->getValue()) != $expectedType) { |
| 156 |
throw new InvalidOptionTypeException( |
| 157 |
'The "' . $this->id . '" option must be a ' . $expectedType . |
| 158 |
' (you provided a ' . gettype($this->getValue()) . ')' |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public function markDeprecated() |
| 164 |
{ |
| 165 |
$this->deprecated = true; |
| 166 |
} |
| 167 |
|
| 168 |
public function isDeprecated() |
| 169 |
{ |
| 170 |
return $this->deprecated; |
| 171 |
} |
| 172 |
|
| 173 |
public function getValueForPrint() |
| 174 |
{ |
| 175 |
return print_r($this->getValue(), true); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Set help text for the option |
| 180 |
* |
| 181 |
* @param string $helpText The help text |
| 182 |
* @return void |
| 183 |
*/ |
| 184 |
public function setHelpText($helpText) |
| 185 |
{ |
| 186 |
$this->helpText = $helpText; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get help text for the option |
| 191 |
* |
| 192 |
* @return string $helpText The help text |
| 193 |
*/ |
| 194 |
public function getHelpText() |
| 195 |
{ |
| 196 |
return $this->helpText; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Set ui definition for the option |
| 201 |
* |
| 202 |
* @param array $ui The UI def |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
public function setUI($ui) |
| 206 |
{ |
| 207 |
$this->ui = $ui; |
| 208 |
} |
| 209 |
|
| 210 |
public function setExtraSchemaDefs($def) |
| 211 |
{ |
| 212 |
$this->extraSchemaDefs = $def; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* Get ui definition for the option |
| 218 |
* |
| 219 |
* @return array $ui The UI def |
| 220 |
*/ |
| 221 |
public function getUI() |
| 222 |
{ |
| 223 |
return $this->ui; |
| 224 |
} |
| 225 |
|
| 226 |
public function getSchema() |
| 227 |
{ |
| 228 |
if (isset($this->extraSchemaDefs)) { |
| 229 |
$schema = $this->extraSchemaDefs; |
| 230 |
} else { |
| 231 |
$schema = []; |
| 232 |
} |
| 233 |
$schema['type'] = $this->schemaType; |
| 234 |
$schema['default'] = $this->defaultValue; |
| 235 |
if (!is_null($this->enum)) { |
| 236 |
$schema['enum'] = $this->enum; |
| 237 |
} |
| 238 |
return $schema; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
public function getDefinition() |
| 243 |
{ |
| 244 |
$obj = [ |
| 245 |
'id' => $this->id, |
| 246 |
'schema' => $this->getSchema(), |
| 247 |
'ui' => $this->ui, |
| 248 |
]; |
| 249 |
if ($this->deprecated) { |
| 250 |
$obj['deprecated'] = true; |
| 251 |
} |
| 252 |
return $obj; |
| 253 |
} |
| 254 |
} |
| 255 |
|