LazyAssertion.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  16. * Chaining builder for lazy assertions.
  17. *
  18. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19. *
  20. * @method LazyAssertion alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric.
  21. * @method LazyAssertion base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
  22. * @method LazyAssertion 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.
  23. * @method LazyAssertion 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.
  24. * @method LazyAssertion betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min,max lengths.
  25. * @method LazyAssertion boolean(string|callable $message = null, string $propertyPath = null) Assert that value is php boolean.
  26. * @method LazyAssertion choice(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices.
  27. * @method LazyAssertion 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.
  28. * @method LazyAssertion classExists(string|callable $message = null, string $propertyPath = null) Assert that the class exists.
  29. * @method LazyAssertion contains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars.
  30. * @method LazyAssertion count(int $count, string $message = null, string $propertyPath = null) Assert that the count of countable is equal to count.
  31. * @method LazyAssertion date(string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format.
  32. * @method LazyAssertion defined(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
  33. * @method LazyAssertion digit(string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit.
  34. * @method LazyAssertion directory(string|callable $message = null, string $propertyPath = null) Assert that a directory exists.
  35. * @method LazyAssertion e164(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number.
  36. * @method LazyAssertion email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
  37. * @method LazyAssertion endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
  38. * @method LazyAssertion eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using == ).
  39. * @method LazyAssertion extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
  40. * @method LazyAssertion extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed.
  41. * @method LazyAssertion false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.
  42. * @method LazyAssertion file(string|callable $message = null, string $propertyPath = null) Assert that a file exists.
  43. * @method LazyAssertion float(string|callable $message = null, string $propertyPath = null) Assert that value is a php float.
  44. * @method LazyAssertion greaterOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit.
  45. * @method LazyAssertion greaterThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit.
  46. * @method LazyAssertion implementsInterface(string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface.
  47. * @method LazyAssertion 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().
  48. * @method LazyAssertion integer(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer.
  49. * @method LazyAssertion integerish(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish.
  50. * @method LazyAssertion interfaceExists(string|callable $message = null, string $propertyPath = null) Assert that the interface exists.
  51. * @method LazyAssertion ip(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address.
  52. * @method LazyAssertion ipv4(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address.
  53. * @method LazyAssertion ipv6(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address.
  54. * @method LazyAssertion isArray(string|callable $message = null, string $propertyPath = null) Assert that value is an array.
  55. * @method LazyAssertion isArrayAccessible(string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object.
  56. * @method LazyAssertion isCallable(string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable.
  57. * @method LazyAssertion isInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name.
  58. * @method LazyAssertion isJsonString(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string.
  59. * @method LazyAssertion isObject(string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object.
  60. * @method LazyAssertion isResource(string|callable $message = null, string $propertyPath = null) Assert that value is a resource.
  61. * @method LazyAssertion isTraversable(string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object.
  62. * @method LazyAssertion keyExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array.
  63. * @method LazyAssertion keyIsset(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset().
  64. * @method LazyAssertion keyNotExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array.
  65. * @method LazyAssertion length(int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length.
  66. * @method LazyAssertion lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit.
  67. * @method LazyAssertion lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit.
  68. * @method LazyAssertion max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit.
  69. * @method LazyAssertion maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars.
  70. * @method LazyAssertion methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object.
  71. * @method LazyAssertion min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit.
  72. * @method LazyAssertion minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long.
  73. * @method LazyAssertion noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty.
  74. * @method LazyAssertion notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank.
  75. * @method LazyAssertion notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty.
  76. * @method LazyAssertion 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.
  77. * @method LazyAssertion notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using == ).
  78. * @method LazyAssertion notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices.
  79. * @method LazyAssertion notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name.
  80. * @method LazyAssertion notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null.
  81. * @method LazyAssertion notSame(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using === ).
  82. * @method LazyAssertion null(string|callable $message = null, string $propertyPath = null) Assert that value is null.
  83. * @method LazyAssertion numeric(string|callable $message = null, string $propertyPath = null) Assert that value is numeric.
  84. * @method LazyAssertion objectOrClass(string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists.
  85. * @method LazyAssertion phpVersion(mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version.
  86. * @method LazyAssertion 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.
  87. * @method LazyAssertion propertyExists(string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists.
  88. * @method LazyAssertion range(mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers.
  89. * @method LazyAssertion readable(string|callable $message = null, string $propertyPath = null) Assert that the value is something readable.
  90. * @method LazyAssertion regex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex.
  91. * @method LazyAssertion same(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===).
  92. * @method LazyAssertion satisfy(callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback.
  93. * @method LazyAssertion scalar(string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar.
  94. * @method LazyAssertion startsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars.
  95. * @method LazyAssertion string(string|callable $message = null, string $propertyPath = null) Assert that value is a string.
  96. * @method LazyAssertion subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name.
  97. * @method LazyAssertion true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True.
  98. * @method LazyAssertion url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL.
  99. * @method LazyAssertion uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID.
  100. * @method LazyAssertion version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions.
  101. * @method LazyAssertion writeable(string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable.
  102. * @method LazyAssertion all() Switch chain into validation mode for an array of values.
  103. * @method LazyAssertion nullOr() Switch chain into mode allowing nulls, ignoring further assertions.
  104. */
  105. class LazyAssertion
  106. {
  107. private $currentChainFailed = false;
  108. private $alwaysTryAll = false;
  109. private $thisChainTryAll = false;
  110. private $currentChain;
  111. private $errors = array();
  112. /** @var string The class to use as AssertionChain factory */
  113. private $assertClass = 'Assert\Assert';
  114. /** @var string|LazyAssertionException The class to use for exceptions */
  115. private $exceptionClass = 'Assert\LazyAssertionException';
  116. public function that($value, $propertyPath, $defaultMessage = null)
  117. {
  118. $this->currentChainFailed = false;
  119. $this->thisChainTryAll = false;
  120. $assertClass = $this->assertClass;
  121. $this->currentChain = $assertClass::that($value, $defaultMessage, $propertyPath);
  122. return $this;
  123. }
  124. public function tryAll()
  125. {
  126. if (!$this->currentChain) {
  127. $this->alwaysTryAll = true;
  128. }
  129. $this->thisChainTryAll = true;
  130. return $this;
  131. }
  132. public function __call($method, $args)
  133. {
  134. if (false === $this->alwaysTryAll
  135. && false === $this->thisChainTryAll
  136. && true === $this->currentChainFailed
  137. ) {
  138. return $this;
  139. }
  140. try {
  141. \call_user_func_array(array($this->currentChain, $method), $args);
  142. } catch (AssertionFailedException $e) {
  143. $this->errors[] = $e;
  144. $this->currentChainFailed = true;
  145. }
  146. return $this;
  147. }
  148. /**
  149. * @throws \Assert\LazyAssertionException
  150. *
  151. * @return bool
  152. */
  153. public function verifyNow()
  154. {
  155. if ($this->errors) {
  156. throw \call_user_func(array($this->exceptionClass, 'fromErrors'), $this->errors);
  157. }
  158. return true;
  159. }
  160. /**
  161. * @param string $className
  162. *
  163. * @return $this
  164. */
  165. public function setAssertClass($className)
  166. {
  167. if (!\is_string($className)) {
  168. throw new LogicException('Assert class name must be passed as a string');
  169. }
  170. if ('Assert\Assert' !== $className && !\is_subclass_of($className, 'Assert\Assert')) {
  171. throw new LogicException($className . ' is not (a subclass of) Assert\Assert');
  172. }
  173. $this->assertClass = $className;
  174. return $this;
  175. }
  176. /**
  177. * @param string $className
  178. *
  179. * @return $this
  180. */
  181. public function setExceptionClass($className)
  182. {
  183. if (!\is_string($className)) {
  184. throw new LogicException('Exception class name must be passed as a string');
  185. }
  186. if ('Assert\LazyAssertionException' !== $className && !\is_subclass_of($className, 'Assert\LazyAssertionException')) {
  187. throw new LogicException($className . ' is not (a subclass of) Assert\LazyAssertionException');
  188. }
  189. $this->exceptionClass = $className;
  190. return $this;
  191. }
  192. }