*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RectorPrefix202411\Webmozart\Assert;
use ArrayAccess;
use BadMethodCallException;
use Closure;
use Countable;
use DateTime;
use DateTimeImmutable;
use Exception;
use ResourceBundle;
use SimpleXMLElement;
use Throwable;
use Traversable;
/**
* Efficient assertions to validate the input/output of your methods.
*
* @since 1.0
*
* @author Bernhard Schussek
*/
class Assert
{
use Mixin;
/**
* @psalm-pure
* @psalm-assert string $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function string($value, $message = '')
{
if (!\is_string($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a string. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert non-empty-string $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function stringNotEmpty($value, $message = '')
{
static::string($value, $message);
static::notEq($value, '', $message);
}
/**
* @psalm-pure
* @psalm-assert int $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function integer($value, $message = '')
{
if (!\is_int($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an integer. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert numeric $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function integerish($value, $message = '')
{
if (!\is_numeric($value) || $value != (int) $value) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an integerish value. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert positive-int $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function positiveInteger($value, $message = '')
{
if (!(\is_int($value) && $value > 0)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a positive integer. Got: %s', static::valueToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert float $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function float($value, $message = '')
{
if (!\is_float($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a float. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert numeric $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function numeric($value, $message = '')
{
if (!\is_numeric($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a numeric. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert positive-int|0 $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function natural($value, $message = '')
{
if (!\is_int($value) || $value < 0) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a non-negative integer. Got: %s', static::valueToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert bool $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function boolean($value, $message = '')
{
if (!\is_bool($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a boolean. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert scalar $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function scalar($value, $message = '')
{
if (!\is_scalar($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a scalar. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert object $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function object($value, $message = '')
{
if (!\is_object($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an object. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert resource $value
*
* @param mixed $value
* @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function resource($value, $type = null, $message = '')
{
if (!\is_resource($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a resource. Got: %s', static::typeToString($value)));
}
if ($type && $type !== \get_resource_type($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a resource of type %2$s. Got: %s', static::typeToString($value), $type));
}
}
/**
* @psalm-pure
* @psalm-assert callable $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isCallable($value, $message = '')
{
if (!\is_callable($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a callable. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert array $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isArray($value, $message = '')
{
if (!\is_array($value)) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an array. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert iterable $value
*
* @deprecated use "isIterable" or "isInstanceOf" instead
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isTraversable($value, $message = '')
{
@\trigger_error(\sprintf('The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', __METHOD__), \E_USER_DEPRECATED);
if (!\is_array($value) && !$value instanceof Traversable) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a traversable. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert array|ArrayAccess $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isArrayAccessible($value, $message = '')
{
if (!\is_array($value) && !$value instanceof ArrayAccess) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an array accessible. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert countable $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isCountable($value, $message = '')
{
if (!\is_array($value) && !$value instanceof Countable && !$value instanceof ResourceBundle && !$value instanceof SimpleXMLElement) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected a countable. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-assert iterable $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isIterable($value, $message = '')
{
if (!\is_array($value) && !$value instanceof Traversable) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an iterable. Got: %s', static::typeToString($value)));
}
}
/**
* @psalm-pure
* @psalm-template ExpectedType of object
* @psalm-param class-string $class
* @psalm-assert ExpectedType $value
*
* @param mixed $value
* @param string|object $class
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isInstanceOf($value, $class, $message = '')
{
if (!$value instanceof $class) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an instance of %2$s. Got: %s', static::typeToString($value), $class));
}
}
/**
* @psalm-pure
* @psalm-template ExpectedType of object
* @psalm-param class-string $class
* @psalm-assert !ExpectedType $value
*
* @param mixed $value
* @param string|object $class
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function notInstanceOf($value, $class, $message = '')
{
if ($value instanceof $class) {
static::reportInvalidArgument(\sprintf($message ?: 'Expected an instance other than %2$s. Got: %s', static::typeToString($value), $class));
}
}
/**
* @psalm-pure
* @psalm-param array $classes
*
* @param mixed $value
* @param array