| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace Intervention\Image\Commands;
- class Argument
- {
- /**
- * Command with arguments
- *
- * @var AbstractCommand
- */
- public $command;
- /**
- * Key of argument in array
- *
- * @var int
- */
- public $key;
- /**
- * Creates new instance from given command and key
- *
- * @param AbstractCommand $command
- * @param int $key
- */
- public function __construct(AbstractCommand $command, $key = 0)
- {
- $this->command = $command;
- $this->key = $key;
- }
- /**
- * Returns name of current arguments command
- *
- * @return string
- */
- public function getCommandName()
- {
- preg_match("/\\\\([\w]+)Command$/", get_class($this->command), $matches);
- return isset($matches[1]) ? lcfirst($matches[1]).'()' : 'Method';
- }
- /**
- * Returns value of current argument
- *
- * @param mixed $default
- * @return mixed
- */
- public function value($default = null)
- {
- $arguments = $this->command->arguments;
- if (is_array($arguments)) {
- return isset($arguments[$this->key]) ? $arguments[$this->key] : $default;
- }
- return $default;
- }
- /**
- * Defines current argument as required
- *
- * @return \Intervention\Image\Commands\Argument
- */
- public function required()
- {
- if ( ! array_key_exists($this->key, $this->command->arguments)) {
- throw new \Intervention\Image\Exception\InvalidArgumentException(
- sprintf("Missing argument %d for %s", $this->key + 1, $this->getCommandName())
- );
- }
- return $this;
- }
- /**
- * Determines that current argument must be of given type
- *
- * @return \Intervention\Image\Commands\Argument
- */
- public function type($type)
- {
- $fail = false;
- $value = $this->value();
- if (is_null($value)) {
- return $this;
- }
- switch (strtolower($type)) {
- case 'bool':
- case 'boolean':
- $fail = ! is_bool($value);
- $message = sprintf('%s accepts only boolean values as argument %d.', $this->getCommandName(), $this->key + 1);
- break;
- case 'int':
- case 'integer':
- $fail = ! is_integer($value);
- $message = sprintf('%s accepts only integer values as argument %d.', $this->getCommandName(), $this->key + 1);
- break;
- case 'num':
- case 'numeric':
- $fail = ! is_numeric($value);
- $message = sprintf('%s accepts only numeric values as argument %d.', $this->getCommandName(), $this->key + 1);
- break;
- case 'str':
- case 'string':
- $fail = ! is_string($value);
- $message = sprintf('%s accepts only string values as argument %d.', $this->getCommandName(), $this->key + 1);
- break;
- case 'array':
- $fail = ! is_array($value);
- $message = sprintf('%s accepts only array as argument %d.', $this->getCommandName(), $this->key + 1);
- break;
- case 'closure':
- $fail = ! is_a($value, '\Closure');
- $message = sprintf('%s accepts only Closure as argument %d.', $this->getCommandName(), $this->key + 1);
- break;
- case 'digit':
- $fail = ! $this->isDigit($value);
- $message = sprintf('%s accepts only integer values as argument %d.', $this->getCommandName(), $this->key + 1);
- break;
- }
- if ($fail) {
- $message = isset($message) ? $message : sprintf("Missing argument for %d.", $this->key);
- throw new \Intervention\Image\Exception\InvalidArgumentException(
- $message
- );
- }
- return $this;
- }
- /**
- * Determines that current argument value must be numeric between given values
- *
- * @return \Intervention\Image\Commands\Argument
- */
- public function between($x, $y)
- {
- $value = $this->type('numeric')->value();
- if (is_null($value)) {
- return $this;
- }
- $alpha = min($x, $y);
- $omega = max($x, $y);
- if ($value < $alpha || $value > $omega) {
- throw new \Intervention\Image\Exception\InvalidArgumentException(
- sprintf('Argument %d must be between %s and %s.', $this->key, $x, $y)
- );
- }
- return $this;
- }
- /**
- * Determines that current argument must be over a minimum value
- *
- * @return \Intervention\Image\Commands\Argument
- */
- public function min($value)
- {
- $v = $this->type('numeric')->value();
- if (is_null($v)) {
- return $this;
- }
- if ($v < $value) {
- throw new \Intervention\Image\Exception\InvalidArgumentException(
- sprintf('Argument %d must be at least %s.', $this->key, $value)
- );
- }
- return $this;
- }
- /**
- * Determines that current argument must be under a maxiumum value
- *
- * @return \Intervention\Image\Commands\Argument
- */
- public function max($value)
- {
- $v = $this->type('numeric')->value();
- if (is_null($v)) {
- return $this;
- }
- if ($v > $value) {
- throw new \Intervention\Image\Exception\InvalidArgumentException(
- sprintf('Argument %d may not be greater than %s.', $this->key, $value)
- );
- }
- return $this;
- }
- /**
- * Checks if value is "PHP" integer (120 but also 120.0)
- *
- * @param mixed $value
- * @return boolean
- */
- private function isDigit($value)
- {
- return is_numeric($value) ? intval($value) == $value : false;
- }
- }
|