Argument.php 5.8 KB

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