Cheetah
Argument.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Argument
6 {
12  public $command;
13 
19  public $key;
20 
27  public function __construct(AbstractCommand $command, $key = 0)
28  {
29  $this->command = $command;
30  $this->key = $key;
31  }
32 
38  public function getCommandName()
39  {
40  preg_match("/\\\\([\w]+)Command$/", get_class($this->command), $matches);
41  return isset($matches[1]) ? lcfirst($matches[1]).'()' : 'Method';
42  }
43 
50  public function value($default = null)
51  {
52  $arguments = $this->command->arguments;
53 
54  if (is_array($arguments)) {
55  return isset($arguments[$this->key]) ? $arguments[$this->key] : $default;
56  }
57 
58  return $default;
59  }
60 
66  public function required()
67  {
68  if ( ! array_key_exists($this->key, $this->command->arguments)) {
69  throw new \Intervention\Image\Exception\InvalidArgumentException(
70  sprintf("Missing argument %d for %s", $this->key + 1, $this->getCommandName())
71  );
72  }
73 
74  return $this;
75  }
76 
82  public function type($type)
83  {
84  $fail = false;
85 
86  $value = $this->value();
87 
88  if (is_null($value)) {
89  return $this;
90  }
91 
92  switch (strtolower($type)) {
93 
94  case 'bool':
95  case 'boolean':
96  $fail = ! is_bool($value);
97  $message = sprintf('%s accepts only boolean values as argument %d.', $this->getCommandName(), $this->key + 1);
98  break;
99 
100  case 'int':
101  case 'integer':
102  $fail = ! is_integer($value);
103  $message = sprintf('%s accepts only integer values as argument %d.', $this->getCommandName(), $this->key + 1);
104  break;
105 
106  case 'num':
107  case 'numeric':
108  $fail = ! is_numeric($value);
109  $message = sprintf('%s accepts only numeric values as argument %d.', $this->getCommandName(), $this->key + 1);
110  break;
111 
112  case 'str':
113  case 'string':
114  $fail = ! is_string($value);
115  $message = sprintf('%s accepts only string values as argument %d.', $this->getCommandName(), $this->key + 1);
116  break;
117 
118  case 'array':
119  $fail = ! is_array($value);
120  $message = sprintf('%s accepts only array as argument %d.', $this->getCommandName(), $this->key + 1);
121  break;
122 
123  case 'closure':
124  $fail = ! is_a($value, '\Closure');
125  $message = sprintf('%s accepts only Closure as argument %d.', $this->getCommandName(), $this->key + 1);
126  break;
127 
128  case 'digit':
129  $fail = ! $this->isDigit($value);
130  $message = sprintf('%s accepts only integer values as argument %d.', $this->getCommandName(), $this->key + 1);
131  break;
132  }
133 
134  if ($fail) {
135 
136  $message = isset($message) ? $message : sprintf("Missing argument for %d.", $this->key);
137 
138  throw new \Intervention\Image\Exception\InvalidArgumentException(
139  $message
140  );
141  }
142 
143  return $this;
144  }
145 
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 \Intervention\Image\Exception\InvalidArgumentException(
164  sprintf('Argument %d must be between %s and %s.', $this->key, $x, $y)
165  );
166  }
167 
168  return $this;
169  }
170 
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 \Intervention\Image\Exception\InvalidArgumentException(
186  sprintf('Argument %d must be at least %s.', $this->key, $value)
187  );
188  }
189 
190  return $this;
191  }
192 
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 \Intervention\Image\Exception\InvalidArgumentException(
208  sprintf('Argument %d may not be greater than %s.', $this->key, $value)
209  );
210  }
211 
212  return $this;
213  }
214 
221  private function isDigit($value)
222  {
223  return is_numeric($value) ? intval($value) == $value : false;
224  }
225 }
Intervention\Image\Commands\Argument\$command
$command
Definition: Argument.php:12
Intervention\Image\Commands\Argument
Definition: Argument.php:6
Intervention\Image\Commands\Argument\required
required()
Definition: Argument.php:66
Intervention\Image\Commands\Argument\max
max($value)
Definition: Argument.php:198
Intervention\Image\Commands\Argument\$key
$key
Definition: Argument.php:19
Intervention\Image\Commands
Definition: AbstractCommand.php:3
php
Intervention\Image\Commands\Argument\min
min($value)
Definition: Argument.php:176
Intervention\Image\Commands\Argument\value
value($default=null)
Definition: Argument.php:50
Intervention\Image\Commands\Argument\__construct
__construct(AbstractCommand $command, $key=0)
Definition: Argument.php:27
Intervention\Image\Commands\Argument\type
type($type)
Definition: Argument.php:82
Intervention\Image\Commands\AbstractCommand
Definition: AbstractCommand.php:6
Intervention\Image\Commands\Argument\between
between($x, $y)
Definition: Argument.php:151
Intervention\Image\Commands\Argument\getCommandName
getCommandName()
Definition: Argument.php:38