| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image\Commands; |
| 4 |
|
| 5 |
use Intervention\Image\Exception\InvalidArgumentException; |
| 6 |
|
| 7 |
class Argument |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Command with arguments |
| 11 |
* |
| 12 |
* @var AbstractCommand |
| 13 |
*/ |
| 14 |
public $command; |
| 15 |
|
| 16 |
/** |
| 17 |
* Key of argument in array |
| 18 |
* |
| 19 |
* @var int |
| 20 |
*/ |
| 21 |
public $key; |
| 22 |
|
| 23 |
/** |
| 24 |
* Creates new instance from given command and key |
| 25 |
* |
| 26 |
* @param AbstractCommand $command |
| 27 |
* @param int $key |
| 28 |
*/ |
| 29 |
public function __construct(AbstractCommand $command, $key = 0) |
| 30 |
{ |
| 31 |
$this->command = $command; |
| 32 |
$this->key = $key; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns name of current arguments command |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function getCommandName() |
| 41 |
{ |
| 42 |
preg_match("/\\\\([\w]+)Command$/", get_class($this->command), $matches); |
| 43 |
return isset($matches[1]) ? lcfirst($matches[1]).'()' : 'Method'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns value of current argument |
| 48 |
* |
| 49 |
* @param mixed $default |
| 50 |
* @return mixed |
| 51 |
*/ |
| 52 |
public function value($default = null) |
| 53 |
{ |
| 54 |
$arguments = $this->command->arguments; |
| 55 |
|
| 56 |
if (is_array($arguments)) { |
| 57 |
return isset($arguments[$this->key]) ? $arguments[$this->key] : $default; |
| 58 |
} |
| 59 |
|
| 60 |
return $default; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Defines current argument as required |
| 65 |
* |
| 66 |
* @return \Intervention\Image\Commands\Argument |
| 67 |
*/ |
| 68 |
public function required() |
| 69 |
{ |
| 70 |
if ( ! array_key_exists($this->key, $this->command->arguments)) { |
| 71 |
throw new InvalidArgumentException( |
| 72 |
sprintf("Missing argument %d for %s", $this->key + 1, $this->getCommandName()) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Determines that current argument must be of given type |
| 81 |
* |
| 82 |
* @return \Intervention\Image\Commands\Argument |
| 83 |
*/ |
| 84 |
public function type($type) |
| 85 |
{ |
| 86 |
$valid = true; |
| 87 |
$value = $this->value(); |
| 88 |
|
| 89 |
if ($value === null) { |
| 90 |
return $this; |
| 91 |
} |
| 92 |
|
| 93 |
switch (strtolower($type)) { |
| 94 |
case 'bool': |
| 95 |
case 'boolean': |
| 96 |
$valid = \is_bool($value); |
| 97 |
$message = '%s accepts only boolean values as argument %d.'; |
| 98 |
break; |
| 99 |
case 'int': |
| 100 |
case 'integer': |
| 101 |
$valid = \is_int($value); |
| 102 |
$message = '%s accepts only integer values as argument %d.'; |
| 103 |
break; |
| 104 |
case 'num': |
| 105 |
case 'numeric': |
| 106 |
$valid = is_numeric($value); |
| 107 |
$message = '%s accepts only numeric values as argument %d.'; |
| 108 |
break; |
| 109 |
case 'str': |
| 110 |
case 'string': |
| 111 |
$valid = \is_string($value); |
| 112 |
$message = '%s accepts only string values as argument %d.'; |
| 113 |
break; |
| 114 |
case 'array': |
| 115 |
$valid = \is_array($value); |
| 116 |
$message = '%s accepts only array as argument %d.'; |
| 117 |
break; |
| 118 |
case 'closure': |
| 119 |
$valid = is_a($value, '\Closure'); |
| 120 |
$message = '%s accepts only Closure as argument %d.'; |
| 121 |
break; |
| 122 |
case 'digit': |
| 123 |
$valid = $this->isDigit($value); |
| 124 |
$message = '%s accepts only integer values as argument %d.'; |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
if (! $valid) { |
| 129 |
$commandName = $this->getCommandName(); |
| 130 |
$argument = $this->key + 1; |
| 131 |
|
| 132 |
if (isset($message)) { |
| 133 |
$message = sprintf($message, $commandName, $argument); |
| 134 |
} else { |
| 135 |
$message = sprintf('Missing argument for %d.', $argument); |
| 136 |
} |
| 137 |
|
| 138 |
throw new InvalidArgumentException( |
| 139 |
$message |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
return $this; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Determines that current argument value must be numeric between given values |
| 148 |
* |
| 149 |
* @return \Intervention\Image\Commands\Argument |
| 150 |
*/ |
| 151 |
public function between($x, $y) |
| 152 |
{ |
| 153 |
$value = $this->type('numeric')->value(); |
| 154 |
|
| 155 |
if (is_null($value)) { |
| 156 |
return $this; |
| 157 |
} |
| 158 |
|
| 159 |
$alpha = min($x, $y); |
| 160 |
$omega = max($x, $y); |
| 161 |
|
| 162 |
if ($value < $alpha || $value > $omega) { |
| 163 |
throw new InvalidArgumentException( |
| 164 |
sprintf('Argument %d must be between %s and %s.', $this->key, $x, $y) |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
return $this; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Determines that current argument must be over a minimum value |
| 173 |
* |
| 174 |
* @return \Intervention\Image\Commands\Argument |
| 175 |
*/ |
| 176 |
public function min($value) |
| 177 |
{ |
| 178 |
$v = $this->type('numeric')->value(); |
| 179 |
|
| 180 |
if (is_null($v)) { |
| 181 |
return $this; |
| 182 |
} |
| 183 |
|
| 184 |
if ($v < $value) { |
| 185 |
throw new InvalidArgumentException( |
| 186 |
sprintf('Argument %d must be at least %s.', $this->key, $value) |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
return $this; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Determines that current argument must be under a maxiumum value |
| 195 |
* |
| 196 |
* @return \Intervention\Image\Commands\Argument |
| 197 |
*/ |
| 198 |
public function max($value) |
| 199 |
{ |
| 200 |
$v = $this->type('numeric')->value(); |
| 201 |
|
| 202 |
if (is_null($v)) { |
| 203 |
return $this; |
| 204 |
} |
| 205 |
|
| 206 |
if ($v > $value) { |
| 207 |
throw new InvalidArgumentException( |
| 208 |
sprintf('Argument %d may not be greater than %s.', $this->key, $value) |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
return $this; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Checks if value is "PHP" integer (120 but also 120.0) |
| 217 |
* |
| 218 |
* @param mixed $value |
| 219 |
* @return boolean |
| 220 |
*/ |
| 221 |
private function isDigit($value) |
| 222 |
{ |
| 223 |
return is_numeric($value) ? intval($value) == $value : false; |
| 224 |
} |
| 225 |
} |
| 226 |
|