AssertionChain.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Assert
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to kontakt@beberlei.de so I can send you a copy immediately.
  12. */
  13. namespace Assert;
  14. use LogicException;
  15. use ReflectionClass;
  16. /**
  17. * Chaining builder for assertions.
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. *
  21. * @method AssertionChain alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric.
  22. * @method AssertionChain base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
  23. * @method AssertionChain between(mixed $lowerLimit, mixed $upperLimit, string $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
  24. * @method AssertionChain betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit.
  25. * @method AssertionChain betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min,max lengths.
  26. * @method AssertionChain boolean(string|callable $message = null, string $propertyPath = null) Assert that value is php boolean.
  27. * @method AssertionChain choice(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices.
  28. * @method AssertionChain choicesNotEmpty(array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content.
  29. * @method AssertionChain classExists(string|callable $message = null, string $propertyPath = null) Assert that the class exists.
  30. * @method AssertionChain contains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars.
  31. * @method AssertionChain count(int $count, string $message = null, string $propertyPath = null) Assert that the count of countable is equal to count.
  32. * @method AssertionChain date(string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format.
  33. * @method AssertionChain defined(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
  34. * @method AssertionChain digit(string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit.
  35. * @method AssertionChain directory(string|callable $message = null, string $propertyPath = null) Assert that a directory exists.
  36. * @method AssertionChain e164(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number.
  37. * @method AssertionChain email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
  38. * @method AssertionChain endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
  39. * @method AssertionChain eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using == ).
  40. * @method AssertionChain extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
  41. * @method AssertionChain extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed.
  42. * @method AssertionChain false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.
  43. * @method AssertionChain file(string|callable $message = null, string $propertyPath = null) Assert that a file exists.
  44. * @method AssertionChain float(string|callable $message = null, string $propertyPath = null) Assert that value is a php float.
  45. * @method AssertionChain greaterOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit.
  46. * @method AssertionChain greaterThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit.
  47. * @method AssertionChain implementsInterface(string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface.
  48. * @method AssertionChain inArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice().
  49. * @method AssertionChain integer(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer.
  50. * @method AssertionChain integerish(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish.
  51. * @method AssertionChain interfaceExists(string|callable $message = null, string $propertyPath = null) Assert that the interface exists.
  52. * @method AssertionChain ip(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address.
  53. * @method AssertionChain ipv4(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address.
  54. * @method AssertionChain ipv6(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address.
  55. * @method AssertionChain isArray(string|callable $message = null, string $propertyPath = null) Assert that value is an array.
  56. * @method AssertionChain isArrayAccessible(string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object.
  57. * @method AssertionChain isCallable(string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable.
  58. * @method AssertionChain isInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name.
  59. * @method AssertionChain isJsonString(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string.
  60. * @method AssertionChain isObject(string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object.
  61. * @method AssertionChain isResource(string|callable $message = null, string $propertyPath = null) Assert that value is a resource.
  62. * @method AssertionChain isTraversable(string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object.
  63. * @method AssertionChain keyExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array.
  64. * @method AssertionChain keyIsset(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset().
  65. * @method AssertionChain keyNotExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array.
  66. * @method AssertionChain length(int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length.
  67. * @method AssertionChain lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit.
  68. * @method AssertionChain lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit.
  69. * @method AssertionChain max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit.
  70. * @method AssertionChain maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars.
  71. * @method AssertionChain methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object.
  72. * @method AssertionChain min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit.
  73. * @method AssertionChain minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long.
  74. * @method AssertionChain noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty.
  75. * @method AssertionChain notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank.
  76. * @method AssertionChain notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty.
  77. * @method AssertionChain notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty.
  78. * @method AssertionChain notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using == ).
  79. * @method AssertionChain notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices.
  80. * @method AssertionChain notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name.
  81. * @method AssertionChain notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null.
  82. * @method AssertionChain notSame(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using === ).
  83. * @method AssertionChain null(string|callable $message = null, string $propertyPath = null) Assert that value is null.
  84. * @method AssertionChain numeric(string|callable $message = null, string $propertyPath = null) Assert that value is numeric.
  85. * @method AssertionChain objectOrClass(string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists.
  86. * @method AssertionChain phpVersion(mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version.
  87. * @method AssertionChain propertiesExist(array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist.
  88. * @method AssertionChain propertyExists(string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists.
  89. * @method AssertionChain range(mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers.
  90. * @method AssertionChain readable(string|callable $message = null, string $propertyPath = null) Assert that the value is something readable.
  91. * @method AssertionChain regex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex.
  92. * @method AssertionChain same(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===).
  93. * @method AssertionChain satisfy(callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback.
  94. * @method AssertionChain scalar(string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar.
  95. * @method AssertionChain startsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars.
  96. * @method AssertionChain string(string|callable $message = null, string $propertyPath = null) Assert that value is a string.
  97. * @method AssertionChain subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name.
  98. * @method AssertionChain true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True.
  99. * @method AssertionChain url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL.
  100. * @method AssertionChain uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID.
  101. * @method AssertionChain version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions.
  102. * @method AssertionChain writeable(string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable.
  103. */
  104. class AssertionChain
  105. {
  106. private $value;
  107. private $defaultMessage;
  108. private $defaultPropertyPath;
  109. /**
  110. * Return each assertion as always valid.
  111. *
  112. * @var bool
  113. */
  114. private $alwaysValid = false;
  115. /**
  116. * Perform assertion on every element of array or traversable.
  117. *
  118. * @var bool
  119. */
  120. private $all = false;
  121. /** @var string|Assertion Class to use for assertion calls */
  122. private $assertionClassName = 'Assert\Assertion';
  123. public function __construct($value, $defaultMessage = null, $defaultPropertyPath = null)
  124. {
  125. $this->value = $value;
  126. $this->defaultMessage = $defaultMessage;
  127. $this->defaultPropertyPath = $defaultPropertyPath;
  128. }
  129. /**
  130. * Call assertion on the current value in the chain.
  131. *
  132. * @param string $methodName
  133. * @param array $args
  134. *
  135. * @return \Assert\AssertionChain
  136. */
  137. public function __call($methodName, $args)
  138. {
  139. if (true === $this->alwaysValid) {
  140. return $this;
  141. }
  142. if (!\method_exists($this->assertionClassName, $methodName)) {
  143. throw new \RuntimeException("Assertion '" . $methodName . "' does not exist.");
  144. }
  145. $reflClass = new ReflectionClass($this->assertionClassName);
  146. $method = $reflClass->getMethod($methodName);
  147. \array_unshift($args, $this->value);
  148. $params = $method->getParameters();
  149. foreach ($params as $idx => $param) {
  150. if (isset($args[$idx])) {
  151. continue;
  152. }
  153. if ('message' == $param->getName()) {
  154. $args[$idx] = $this->defaultMessage;
  155. }
  156. if ('propertyPath' == $param->getName()) {
  157. $args[$idx] = $this->defaultPropertyPath;
  158. }
  159. }
  160. if ($this->all) {
  161. $methodName = 'all' . $methodName;
  162. }
  163. \call_user_func_array(array($this->assertionClassName, $methodName), $args);
  164. return $this;
  165. }
  166. /**
  167. * Switch chain into validation mode for an array of values.
  168. *
  169. * @return \Assert\AssertionChain
  170. */
  171. public function all()
  172. {
  173. $this->all = true;
  174. return $this;
  175. }
  176. /**
  177. * Switch chain into mode allowing nulls, ignoring further assertions.
  178. *
  179. * @return \Assert\AssertionChain
  180. */
  181. public function nullOr()
  182. {
  183. if (null === $this->value) {
  184. $this->alwaysValid = true;
  185. }
  186. return $this;
  187. }
  188. /**
  189. * @param string $className
  190. *
  191. * @return $this
  192. */
  193. public function setAssertionClassName($className)
  194. {
  195. if (!\is_string($className)) {
  196. throw new LogicException('Exception class name must be passed as a string');
  197. }
  198. if ('Assert\Assertion' !== $className && !\is_subclass_of($className, 'Assert\Assertion')) {
  199. throw new LogicException($className . ' is not (a subclass of) Assert\Assertion');
  200. }
  201. $this->assertionClassName = $className;
  202. return $this;
  203. }
  204. }