| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvert\Options; |
| 4 |
|
| 5 |
use WebPConvert\Options\Option; |
| 6 |
use WebPConvert\Options\Exceptions\InvalidOptionValueException; |
| 7 |
|
| 8 |
/** |
| 9 |
* Abstract 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 IntegerOption extends Option |
| 16 |
{ |
| 17 |
protected $typeId = 'int'; |
| 18 |
protected $schemaType = ['integer']; |
| 19 |
protected $minValue; |
| 20 |
protected $maxValue; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor. |
| 24 |
* |
| 25 |
* @param string $id id of the option |
| 26 |
* @param integer $defaultValue default value for the option |
| 27 |
* @throws InvalidOptionValueException if the default value cannot pass the check |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function __construct($id, $defaultValue, $minValue = null, $maxValue = null) |
| 31 |
{ |
| 32 |
$this->minValue = $minValue; |
| 33 |
$this->maxValue = $maxValue; |
| 34 |
parent::__construct($id, $defaultValue); |
| 35 |
} |
| 36 |
|
| 37 |
protected function checkMin() |
| 38 |
{ |
| 39 |
if (!is_null($this->minValue) && $this->getValue() < $this->minValue) { |
| 40 |
throw new InvalidOptionValueException( |
| 41 |
'"' . $this->id . '" option must be set to minimum ' . $this->minValue . '. ' . |
| 42 |
'It was however set to: ' . $this->getValue() |
| 43 |
); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
protected function checkMax() |
| 48 |
{ |
| 49 |
if (!is_null($this->maxValue) && $this->getValue() > $this->maxValue) { |
| 50 |
throw new InvalidOptionValueException( |
| 51 |
'"' . $this->id . '" option must be set to max ' . $this->maxValue . '. ' . |
| 52 |
'It was however set to: ' . $this->getValue() |
| 53 |
); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
protected function checkMinMax() |
| 58 |
{ |
| 59 |
$this->checkMin(); |
| 60 |
$this->checkMax(); |
| 61 |
} |
| 62 |
|
| 63 |
public function check() |
| 64 |
{ |
| 65 |
$this->checkType('integer'); |
| 66 |
$this->checkMinMax(); |
| 67 |
} |
| 68 |
|
| 69 |
public function getSchema() |
| 70 |
{ |
| 71 |
$obj = parent::getSchema(); |
| 72 |
$obj['minimum'] = $this->minValue; |
| 73 |
$obj['maximum'] = $this->maxValue; |
| 74 |
return $obj; |
| 75 |
} |
| 76 |
} |
| 77 |
|