File indexing completed on 2025-01-26 05:29:13
0001 <?php 0002 0003 namespace Intervention\Image\Commands; 0004 0005 class Argument 0006 { 0007 /** 0008 * Command with arguments 0009 * 0010 * @var AbstractCommand 0011 */ 0012 public $command; 0013 0014 /** 0015 * Key of argument in array 0016 * 0017 * @var int 0018 */ 0019 public $key; 0020 0021 /** 0022 * Creates new instance from given command and key 0023 * 0024 * @param AbstractCommand $command 0025 * @param int $key 0026 */ 0027 public function __construct(AbstractCommand $command, $key = 0) 0028 { 0029 $this->command = $command; 0030 $this->key = $key; 0031 } 0032 0033 /** 0034 * Returns name of current arguments command 0035 * 0036 * @return string 0037 */ 0038 public function getCommandName() 0039 { 0040 preg_match("/\\\\([\w]+)Command$/", get_class($this->command), $matches); 0041 return isset($matches[1]) ? lcfirst($matches[1]).'()' : 'Method'; 0042 } 0043 0044 /** 0045 * Returns value of current argument 0046 * 0047 * @param mixed $default 0048 * @return mixed 0049 */ 0050 public function value($default = null) 0051 { 0052 $arguments = $this->command->arguments; 0053 0054 if (is_array($arguments)) { 0055 return isset($arguments[$this->key]) ? $arguments[$this->key] : $default; 0056 } 0057 0058 return $default; 0059 } 0060 0061 /** 0062 * Defines current argument as required 0063 * 0064 * @return \Intervention\Image\Commands\Argument 0065 */ 0066 public function required() 0067 { 0068 if ( ! array_key_exists($this->key, $this->command->arguments)) { 0069 throw new \Intervention\Image\Exception\InvalidArgumentException( 0070 sprintf("Missing argument %d for %s", $this->key + 1, $this->getCommandName()) 0071 ); 0072 } 0073 0074 return $this; 0075 } 0076 0077 /** 0078 * Determines that current argument must be of given type 0079 * 0080 * @return \Intervention\Image\Commands\Argument 0081 */ 0082 public function type($type) 0083 { 0084 $fail = false; 0085 0086 $value = $this->value(); 0087 0088 if (is_null($value)) { 0089 return $this; 0090 } 0091 0092 switch (strtolower($type)) { 0093 0094 case 'bool': 0095 case 'boolean': 0096 $fail = ! is_bool($value); 0097 $message = sprintf('%s accepts only boolean values as argument %d.', $this->getCommandName(), $this->key + 1); 0098 break; 0099 0100 case 'int': 0101 case 'integer': 0102 $fail = ! is_integer($value); 0103 $message = sprintf('%s accepts only integer values as argument %d.', $this->getCommandName(), $this->key + 1); 0104 break; 0105 0106 case 'num': 0107 case 'numeric': 0108 $fail = ! is_numeric($value); 0109 $message = sprintf('%s accepts only numeric values as argument %d.', $this->getCommandName(), $this->key + 1); 0110 break; 0111 0112 case 'str': 0113 case 'string': 0114 $fail = ! is_string($value); 0115 $message = sprintf('%s accepts only string values as argument %d.', $this->getCommandName(), $this->key + 1); 0116 break; 0117 0118 case 'array': 0119 $fail = ! is_array($value); 0120 $message = sprintf('%s accepts only array as argument %d.', $this->getCommandName(), $this->key + 1); 0121 break; 0122 0123 case 'closure': 0124 $fail = ! is_a($value, '\Closure'); 0125 $message = sprintf('%s accepts only Closure as argument %d.', $this->getCommandName(), $this->key + 1); 0126 break; 0127 0128 case 'digit': 0129 $fail = ! $this->isDigit($value); 0130 $message = sprintf('%s accepts only integer values as argument %d.', $this->getCommandName(), $this->key + 1); 0131 break; 0132 } 0133 0134 if ($fail) { 0135 0136 $message = isset($message) ? $message : sprintf("Missing argument for %d.", $this->key); 0137 0138 throw new \Intervention\Image\Exception\InvalidArgumentException( 0139 $message 0140 ); 0141 } 0142 0143 return $this; 0144 } 0145 0146 /** 0147 * Determines that current argument value must be numeric between given values 0148 * 0149 * @return \Intervention\Image\Commands\Argument 0150 */ 0151 public function between($x, $y) 0152 { 0153 $value = $this->type('numeric')->value(); 0154 0155 if (is_null($value)) { 0156 return $this; 0157 } 0158 0159 $alpha = min($x, $y); 0160 $omega = max($x, $y); 0161 0162 if ($value < $alpha || $value > $omega) { 0163 throw new \Intervention\Image\Exception\InvalidArgumentException( 0164 sprintf('Argument %d must be between %s and %s.', $this->key, $x, $y) 0165 ); 0166 } 0167 0168 return $this; 0169 } 0170 0171 /** 0172 * Determines that current argument must be over a minimum value 0173 * 0174 * @return \Intervention\Image\Commands\Argument 0175 */ 0176 public function min($value) 0177 { 0178 $v = $this->type('numeric')->value(); 0179 0180 if (is_null($v)) { 0181 return $this; 0182 } 0183 0184 if ($v < $value) { 0185 throw new \Intervention\Image\Exception\InvalidArgumentException( 0186 sprintf('Argument %d must be at least %s.', $this->key, $value) 0187 ); 0188 } 0189 0190 return $this; 0191 } 0192 0193 /** 0194 * Determines that current argument must be under a maxiumum value 0195 * 0196 * @return \Intervention\Image\Commands\Argument 0197 */ 0198 public function max($value) 0199 { 0200 $v = $this->type('numeric')->value(); 0201 0202 if (is_null($v)) { 0203 return $this; 0204 } 0205 0206 if ($v > $value) { 0207 throw new \Intervention\Image\Exception\InvalidArgumentException( 0208 sprintf('Argument %d may not be greater than %s.', $this->key, $value) 0209 ); 0210 } 0211 0212 return $this; 0213 } 0214 0215 /** 0216 * Checks if value is "PHP" integer (120 but also 120.0) 0217 * 0218 * @param mixed $value 0219 * @return boolean 0220 */ 0221 private function isDigit($value) 0222 { 0223 return is_numeric($value) ? intval($value) == $value : false; 0224 } 0225 }