{
if ($count < \count($countable)) {
$message = \sprintf(
static::generateMessage($message ?: 'List should have at most %d elements, but has %d elements.'),
static::stringify($count),
static::stringify(\count($countable))
);
throw static::createException($countable, $message, static::INVALID_MAX_COUNT, $propertyPath, ['count' => $count]);
}
return true;
}
/**
* static call handler to implement:
* - "null or assertion" delegation
* - "all" delegation.
*
* @param string $method
* @param array $args
*
* @return bool|mixed
*/
public static function __callStatic($method, $args)
{
if (0 === \strpos($method, 'nullOr')) {
if (!\array_key_exists(0, $args)) {
throw new BadMethodCallException('Missing the first argument.');
}
if (null === $args[0]) {
return true;
}
$method = \substr($method, 6);
return \call_user_func_array([\get_called_class(), $method], $args);
}
if (0 === \strpos($method, 'all')) {
if (!\array_key_exists(0, $args)) {
throw new BadMethodCallException('Missing the first argument.');
}
static::isTraversable($args[0]);
$method = \substr($method, 3);
$values = \array_shift($args);
$calledClass = \get_called_class();
foreach ($values as $value) {
\call_user_func_array([$calledClass, $method], \array_merge([$value], $args));
}
return true;
}
throw new BadMethodCallException('No assertion Assertion#'.$method.' exists.');
}
/**
* Determines if the values array has every choice as key and that this choice has content.
*
* @param array $values
* @param array $choices
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
{
static::notEmpty($values, $message, $propertyPath);
foreach ($choices as $choice) {
static::notEmptyKey($values, $choice, $message, $propertyPath);
}
return true;
}
/**
* Determines that the named method is defined in the provided object.
*
* @param string $value
* @param mixed $object
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function methodExists($value, $object, $message = null, $propertyPath = null)
{
static::isObject($object, $message, $propertyPath);
if (!\method_exists($object, $value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Expected "%s" does not exist in provided object.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath, ['object' => \get_class($object)]);
}
return true;
}
/**
* Determines that the provided value is an object.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function isObject($value, $message = null, $propertyPath = null)
{
if (!\is_object($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is not a valid object.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
}
return true;
}
/**
* Determines if the value is less than given limit.
*
* @param mixed $value
* @param mixed $limit
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function lessThan($value, $limit, $message = null, $propertyPath = null)
{
if ($value >= $limit) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is not less than "%s".'),
static::stringify($value),
static::stringify($limit)
);
throw static::createException($value, $message, static::INVALID_LESS, $propertyPath, ['limit' => $limit]);
}
return true;
}
/**
* Determines if the value is less or equal than given limit.
*
* @param mixed $value
* @param mixed $limit
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
{
if ($value > $limit) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is not less or equal than "%s".'),
static::stringify($value),
static::stringify($limit)
);
throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath, ['limit' => $limit]);
}
return true;
}
/**
* Determines if the value is greater than given limit.
*
* @param mixed $value
* @param mixed $limit
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
{
if ($value <= $limit) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is not greater than "%s".'),
static::stringify($value),
static::stringify($limit)
);
throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath, ['limit' => $limit]);
}
return true;
}
/**
* Determines if the value is greater or equal than given limit.
*
* @param mixed $value
* @param mixed $limit
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
{
if ($value < $limit) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is not greater or equal than "%s".'),
static::stringify($value),
static::stringify($limit)
);
throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath, ['limit' => $limit]);
}
return true;
}
/**
* Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
* @param string $message
* @param string $propertyPath
*
* @return bool
*/
public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
{
if ($lowerLimit > $value || $value > $upperLimit) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is neither greater than or equal to "%s" nor less than or equal to "%s".'),
static::stringify($value),
static::stringify($lowerLimit),
static::stringify($upperLimit)
);
throw static::createException($value, $message, static::INVALID_BETWEEN, $propertyPath, ['lower' => $lowerLimit, 'upper' => $upperLimit]);
}
return true;
}
/**
* Assert that a value is greater than a lower limit, and less than an upper limit.
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
* @param string $message
* @param string $propertyPath
*
* @return bool
*/
public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
{
if ($lowerLimit >= $value || $value >= $upperLimit) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is neither greater than "%s" nor less than "%s".'),
static::stringify($value),
static::stringify($lowerLimit),
static::stringify($upperLimit)
);
throw static::createException($value, $message, static::INVALID_BETWEEN_EXCLUSIVE, $propertyPath, ['lower' => $lowerLimit, 'upper' => $upperLimit]);
}
return true;
}
/**
* Assert that extension is loaded.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function extensionLoaded($value, $message = null, $propertyPath = null)
{
if (!\extension_loaded($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Extension "%s" is required.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_EXTENSION, $propertyPath);
}
return true;
}
/**
* Assert that date is valid and corresponds to the given format.
*
* @param string $value
* @param string $format supports all of the options date(), except for the following:
* N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*
* @see http://php.net/manual/function.date.php#refsect1-function.date-parameters
*/
public static function date($value, $format, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);
static::string($format, $message, $propertyPath);
$dateTime = \DateTime::createFromFormat('!'.$format, $value);
if (false === $dateTime || $value !== $dateTime->format($format)) {
$message = \sprintf(
static::generateMessage($message ?: 'Date "%s" is invalid or does not match format "%s".'),
static::stringify($value),
static::stringify($format)
);
throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, ['format' => $format]);
}
return true;
}
/**
* Assert that the value is an object, or a class that exists.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function objectOrClass($value, $message = null, $propertyPath = null)
{
if (!\is_object($value)) {
static::classExists($value, $message, $propertyPath);
}
return true;
}
/**
* Assert that the value is an object or class, and that the property exists.
*
* @param mixed $value
* @param string $property
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function propertyExists($value, $property, $message = null, $propertyPath = null)
{
static::objectOrClass($value);
if (!\property_exists($value, $property)) {
$message = \sprintf(
static::generateMessage($message ?: 'Class "%s" does not have property "%s".'),
static::stringify($value),
static::stringify($property)
);
throw static::createException($value, $message, static::INVALID_PROPERTY, $propertyPath, ['property' => $property]);
}
return true;
}
/**
* Assert that the value is an object or class, and that the properties all exist.
*
* @param mixed $value
* @param array $properties
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function propertiesExist($value, array $properties, $message = null, $propertyPath = null)
{
static::objectOrClass($value);
static::allString($properties, $message, $propertyPath);
$invalidProperties = [];
foreach ($properties as $property) {
if (!\property_exists($value, $property)) {
$invalidProperties[] = $property;
}
}
if ($invalidProperties) {
$message = \sprintf(
static::generateMessage($message ?: 'Class "%s" does not have these properties: %s.'),
static::stringify($value),
static::stringify(\implode(', ', $invalidProperties))
);
throw static::createException($value, $message, static::INVALID_PROPERTY, $propertyPath, ['properties' => $properties]);
}
return true;
}
/**
* Assert comparison of two versions.
*
* @param string $version1
* @param string $operator
* @param string $version2
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function version($version1, $operator, $version2, $message = null, $propertyPath = null)
{
static::notEmpty($operator, 'versionCompare operator is required and cannot be empty.');
if (true !== \version_compare($version1, $version2, $operator)) {
$message = \sprintf(
static::generateMessage($message ?: 'Version "%s" is not "%s" version "%s".'),
static::stringify($version1),
static::stringify($operator),
static::stringify($version2)
);
throw static::createException($version1, $message, static::INVALID_VERSION, $propertyPath, ['operator' => $operator, 'version' => $version2]);
}
return true;
}
/**
* Assert on PHP version.
*
* @param string $operator
* @param mixed $version
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function phpVersion($operator, $version, $message = null, $propertyPath = null)
{
static::defined('PHP_VERSION');
return static::version(PHP_VERSION, $operator, $version, $message, $propertyPath);
}
/**
* Assert that extension is loaded and a specific version is installed.
*
* @param string $extension
* @param string $operator
* @param mixed $version
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function extensionVersion($extension, $operator, $version, $message = null, $propertyPath = null)
{
static::extensionLoaded($extension, $message, $propertyPath);
return static::version(\phpversion($extension), $operator, $version, $message, $propertyPath);
}
/**
* Determines that the provided value is callable.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function isCallable($value, $message = null, $propertyPath = null)
{
if (!\is_callable($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is not a callable.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath);
}
return true;
}
/**
* Assert that the provided value is valid according to a callback.
*
* If the callback returns `false` the assertion will fail.
*
* @param mixed $value
* @param callable $callback
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function satisfy($value, $callback, $message = null, $propertyPath = null)
{
static::isCallable($callback);
if (false === \call_user_func($callback, $value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Provided "%s" is invalid according to custom rule.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_SATISFY, $propertyPath);
}
return true;
}
/**
* Assert that value is an IPv4 or IPv6 address
* (using input_filter/FILTER_VALIDATE_IP).
*
* @param string $value
* @param int|null $flag
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*
* @see http://php.net/manual/filter.filters.flags.php
*/
public static function ip($value, $flag = null, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);
if (!\filter_var($value, FILTER_VALIDATE_IP, $flag)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" was expected to be a valid IP address.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_IP, $propertyPath, ['flag' => $flag]);
}
return true;
}
/**
* Assert that value is an IPv4 address
* (using input_filter/FILTER_VALIDATE_IP).
*
* @param string $value
* @param int|null $flag
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*
* @see http://php.net/manual/filter.filters.flags.php
*/
public static function ipv4($value, $flag = null, $message = null, $propertyPath = null)
{
static::ip($value, $flag | FILTER_FLAG_IPV4, static::generateMessage($message ?: 'Value "%s" was expected to be a valid IPv4 address.'), $propertyPath);
return true;
}
/**
* Assert that value is an IPv6 address
* (using input_filter/FILTER_VALIDATE_IP).
*
* @param string $value
* @param int|null $flag
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*
* @see http://php.net/manual/filter.filters.flags.php
*/
public static function ipv6($value, $flag = null, $message = null, $propertyPath = null)
{
static::ip($value, $flag | FILTER_FLAG_IPV6, static::generateMessage($message ?: 'Value "%s" was expected to be a valid IPv6 address.'), $propertyPath);
return true;
}
/**
* Assert that a constant is defined.
*
* @param mixed $constant
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function defined($constant, $message = null, $propertyPath = null)
{
if (!\defined($constant)) {
$message = \sprintf(static::generateMessage($message ?: 'Value "%s" expected to be a defined constant.'), $constant);
throw static::createException($constant, $message, static::INVALID_CONSTANT, $propertyPath);
}
return true;
}
/**
* Assert that a constant is defined.
*
* @param string $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function base64($value, $message = null, $propertyPath = null)
{
if (false === \base64_decode($value, true)) {
$message = \sprintf(static::generateMessage($message ?: 'Value "%s" is not a valid base64 string.'), $value);
throw static::createException($value, $message, static::INVALID_BASE64, $propertyPath);
}
return true;
}
/**
* Helper method that handles building the assertion failure exceptions.
* They are returned from this method so that the stack trace still shows
* the assertions method.
*
* @param mixed $value
* @param string|callable $message
* @param int $code
* @param string|null $propertyPath
* @param array $constraints
*
* @return mixed
*/
protected static function createException($value, $message, $code, $propertyPath = null, array $constraints = [])
{
$exceptionClass = static::$exceptionClass;
return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
}
/**
* Make a string version of a value.
*
* @param mixed $value
*
* @return string
*/
protected static function stringify($value)
{
$result = \gettype($value);
if (\is_bool($value)) {
$result = $value ? '