### Install Webmozart Assert with Composer Source: https://github.com/webmozarts/assert/blob/master/README.md This command installs the Webmozart Assert library using Composer, the dependency manager for PHP. Ensure Composer is installed and accessible in your environment. ```bash composer require webmozart/assert ``` -------------------------------- ### File Assertions Source: https://github.com/webmozarts/assert/blob/master/README.md Validates file system paths, checking for existence, readability, and writability. ```php Assert::fileExists($value, $message = '') Assert::file($value, $message = '') Assert::directory($value, $message = '') Assert::readable($value, $message = '') Assert::writable($value, $message = '') ``` -------------------------------- ### Static Analysis Support Source: https://github.com/webmozarts/assert/blob/master/README.md Information on how the assertion functions are annotated to support static analysis tools like Psalm and PHPStan. A dedicated PHPStan plugin is required for full type support. ```php Psalm Assertion syntax: https://psalm.dev/docs/annotating_code/assertion_syntax/ PHPStan Plugin: https://github.com/phpstan/phpstan-webmozart-assert ``` -------------------------------- ### Extending Assert Class Source: https://github.com/webmozarts/assert/blob/master/README.md Details methods that can be overridden or extended to customize the behavior of the Assert class, including handling `nullOr` and `all` prefixes, value string conversion, and error reporting. ```php public static function __callStatic($name, $arguments) protected static function valueToString($value) protected static function typeToString($value) protected static function strlen($value) protected static function reportInvalidArgument($message) ``` -------------------------------- ### Object Assertions Source: https://github.com/webmozarts/assert/blob/master/README.md Checks for the existence and relationships of classes, interfaces, properties, and methods. ```php Assert::classExists($value, $message = '') Assert::subclassOf($value, $class, $message = '') Assert::interfaceExists($value, $message = '') Assert::implementsInterface($value, $class, $message = '') Assert::propertyExists($value, $property, $message = '') Assert::propertyNotExists($value, $property, $message = '') Assert::methodExists($value, $method, $message = '') Assert::methodNotExists($value, $method, $message = '') ``` -------------------------------- ### Basic Assertions in PHP Source: https://github.com/webmozarts/assert/blob/master/README.md Demonstrates how to use Webmozart Assert to validate method arguments. The `Assert` class provides static methods for various data type and value checks. Invalid inputs trigger `InvalidArgumentException` with customizable error messages. ```php use Webmozart\Assert\Assert; class Employee { public function __construct($id) { Assert::integer($id, 'The employee ID must be an integer. Got: %s'); Assert::greaterThan($id, 0, 'The employee ID must be a positive integer. Got: %s'); } } // Example usage: // new Employee('foobar'); // Throws exception: The employee ID must be an integer. Got: string // new Employee(-10); // Throws exception: The employee ID must be a positive integer. Got: -10 ``` -------------------------------- ### Customizable Error Messages with Placeholders Source: https://github.com/webmozarts/assert/blob/master/README.md Illustrates how to provide custom error messages for assertions in Webmozart Assert. The library uses consistent placeholder ordering (`%s`, `%2$s`, etc.) for values like the tested input, minimum/maximum lengths, and allowed values. ```php Assert::string($path, 'The path is expected to be a string. Got: %s'); ``` -------------------------------- ### String Assertions Source: https://github.com/webmozarts/assert/blob/master/README.md Provides methods to validate string properties such as content, format, and length. Requires the value to be a string before use. ```php Assert::string($value, $message = '') // String content assertions Assert::contains($value, $subString, $message = '') Assert::notContains($value, $subString, $message = '') Assert::startsWith($value, $prefix, $message = '') Assert::notStartsWith($value, $prefix, $message = '') Assert::startsWithLetter($value, $message = '') Assert::endsWith($value, $suffix, $message = '') Assert::notEndsWith($value, $suffix, $message = '') // String format assertions Assert::regex($value, $pattern, $message = '') Assert::notRegex($value, $pattern, $message = '') Assert::unicodeLetters($value, $message = '') Assert::alpha($value, $message = '') Assert::digits($value, $message = '') Assert::alnum($value, $message = '') Assert::lower($value, $message = '') Assert::upper($value, $message = '') Assert::uuid($value, $message = '') Assert::ip($value, $message = '') Assert::ipv4($value, $message = '') Assert::ipv6($value, $message = '') Assert::email($value, $message = '') Assert::notWhitespaceOnly($value, $message = '') // String length assertions Assert::length($value, $length, $message = '') Assert::minLength($value, $min, $message = '') Assert::maxLength($value, $max, $message = '') Assert::lengthBetween($value, $min, $max, $message = '') ``` -------------------------------- ### Array Assertions Source: https://github.com/webmozarts/assert/blob/master/README.md Provides methods to assert various conditions related to array keys and counts. These include checking for key existence, validating key types, and verifying the number of elements in an array. ```php Assert::keyExists($array, $key, $message = '') Assert::keyNotExists($array, $key, $message = '') Assert::validArrayKey($key, $message = '') Assert::count($array, $number, $message = '') Assert::minCount($array, $min, $message = '') Assert::maxCount($array, $max, $message = '') Assert::countBetween($array, $min, $max, $message = '') Assert::isList($array, $message = '') Assert::isNonEmptyList($array, $message = '') Assert::isMap($array, $message = '') Assert::isNonEmptyMap($array, $message = '') ``` -------------------------------- ### Comparison Assertions Source: https://github.com/webmozarts/assert/blob/master/README.md Provides methods for comparing values. These include checks for truthiness, falsiness, nullity, emptiness, equality (both loose and strict), greater than, less than, and range checks. It also includes methods to check if a value is present within an array. ```APIDOC true($value, $message = '') Checks that a value is true. false($value, $message = '') Checks that a value is false. notFalse($value, $message = '') Checks that a value is not false. null($value, $message = '') Checks that a value is null. notNull($value, $message = '') Checks that a value is not null. isEmpty($value, $message = '') Checks that a value is empty(). notEmpty($value, $message = '') Checks that a value is not empty(). eq($value, $value2, $message = '') Checks that a value equals another (==). notEq($value, $value2, $message = '') Checks that a value does not equal another (!=). same($value, $value2, $message = '') Checks that a value is identical to another (===). notSame($value, $value2, $message = '') Checks that a value is not identical to another (!==). greaterThan($value, $value2, $message = '') Checks that a value is greater than another. greaterThanEq($value, $value2, $message = '') Checks that a value is greater than or equal to another. lessThan($value, $value2, $message = '') Checks that a value is less than another. lessThanEq($value, $value2, $message = '') Checks that a value is less than or equal to another. range($value, $min, $max, $message = '') Checks that a value is within a range (inclusive). inArray($value, array $values, $message = '') Checks that a value is one of a list of values. oneOf($value, array $values, $message = '') Checks that a value is one of a list of values (alias of inArray). ``` -------------------------------- ### Nullable Assertions (nullOr*) Source: https://github.com/webmozarts/assert/blob/master/README.md Provides a way to conditionally apply assertions only when the value is not null. This is done by prefixing assertion methods with `nullOr*`. ```php Assert::nullOrString($middleName, 'The middle name must be a string or null. Got: %s'); ``` -------------------------------- ### Function Assertions Source: https://github.com/webmozarts/assert/blob/master/README.md Enables testing for exceptions thrown by a given closure. It allows specifying the expected exception class, and subclasses of that exception will also be accepted. ```php Assert::throws($closure, $class, $message = '') ``` -------------------------------- ### Collection Assertions (all*) Source: https://github.com/webmozarts/assert/blob/master/README.md Allows applying assertions to all elements within an array or Traversable object. This is achieved by prefixing standard assertion methods with `all*`. ```php Assert::allIsInstanceOf($employees, 'Acme\Employee'); ``` -------------------------------- ### Type Assertions Source: https://github.com/webmozarts/assert/blob/master/README.md Provides methods to assert the type of a given value. These assertions check for specific data types like strings, integers, floats, booleans, arrays, objects, and callable functions. Some methods also include checks for emptiness, numeric properties, or resource types. ```APIDOC string($value, $message = '') Checks that a value is a string. stringNotEmpty($value, $message = '') Checks that a value is a non-empty string. integer($value, $message = '') Checks that a value is an integer. integerish($value, $message = '') Checks that a value casts to an integer. positiveInteger($value, $message = '') Checks that a value is a positive (non-zero) integer. float($value, $message = '') Checks that a value is a float. numeric($value, $message = '') Checks that a value is numeric. natural($value, $message = '') Checks that a value is a non-negative integer. boolean($value, $message = '') Checks that a value is a boolean. scalar($value, $message = '') Checks that a value is a scalar. object($value, $message = '') Checks that a value is an object. resource($value, $type = null, $message = '') Checks that a value is a resource. Optionally checks for a specific resource type. isCallable($value, $message = '') Checks that a value is a callable. isArray($value, $message = '') Checks that a value is an array. isTraversable($value, $message = '') Checks that a value is an array or a \Traversable (deprecated). isIterable($value, $message = '') Checks that a value is an array or a \Traversable. isCountable($value, $message = '') Checks that a value is an array or a \Countable. isInstanceOf($value, $class, $message = '') Checks that a value is an instanceof a class. isInstanceOfAny($value, array $classes, $message = '') Checks that a value is an instanceof at least one class from the provided array of classes. notInstanceOf($value, $class, $message = '') Checks that a value is not an instanceof a class. isAOf($value, $class, $message = '') Checks that a value is of the class or has one of its parents. isAnyOf($value, array $classes, $message = '') Checks that a value is of at least one of the classes or has one of its parents. isNotA($value, $class, $message = '') Checks that a value is not of the class or has not one of its parents. isArrayAccessible($value, $message = '') Checks that a value can be accessed as an array. uniqueValues($values, $message = '') Checks that the given array contains unique values. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.