Download the PHP package nexusphp/assert without Composer
On this page you can find all versions of the php package nexusphp/assert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nexusphp/assert
More information about nexusphp/assert
Files in nexusphp/assert
Package assert
Short Description Chainable type-safety assertions library.
License MIT
Informations about the package assert
Nexus Assert
This library provides efficient type assertions for input validation in a chainable, fluent, natural language way. This also provides static analysis support ensuring PHPStan can understand the asserted type.
Installation
Installing the PHPStan extension
You're all set if you are using phpstan/extension-installer.
Manual installation
If you don't want to use `phpstan/extension-installer`, include extension.neon in your project's PHPStan config:Usage
Use the static that() method of Nexus\Assert\Assert to start chaining expectations.
When an expectation fails, the method call will throw a Nexus\Assert\ExpectationFailedException object
with the message formatted depending on the available context. By default, there are two available context:
| Name | Description |
|---|---|
value |
The exported value of the argument passed to that() |
type |
The exported type of the argument passed to that() |
List of Expectations
| Methods | Available Context |
|---|---|
contains(string $needle, ?string $message = null): self |
value, needle |
endsWith(string $needle, ?string $message = null): self |
value, needle |
hasCount(int $count, ?string $message = null): self |
value, count |
hasLength(int $length, ?string $message = null): self |
value, length, actual |
hasMaxCount(int $max, ?string $message = null): self |
value, max |
hasMaxLength(int $max, ?string $message = null): self |
value, max |
hasMethod(string $method, ?string $message = null): self |
value+, method= |
hasMinCount(int $min, ?string $message = null): self |
value, min |
hasMinLength(int $min, ?string $message = null): self |
value, min |
hasOffset(int\|string $key, ?string $message = null): self |
value, key= |
hasProperty(string $property, ?string $message = null): self |
value+, property= |
implementsInterface(string $interface, ?string $message = null): self |
value, interface, type |
isArray(?string $message = null): self |
value, type |
isArrayAccessible(?string $message = null): self |
value, type |
isArrayKey(?string $message = null): self |
value, type |
isBetween(float\|int $min, float\|int $max, bool $inclusive = true, ?string $message = null): self |
value, min, max |
isBool(?string $message = null): self |
value, type |
isCallable(?string $message = null): self |
value, type |
isClassString(?string $message = null): self |
value, type |
isCountable(?string $message = null): self |
value, type |
isFalse(?string $message = null): self |
value, type |
isFloat(?string $message = null): self |
value, type |
isGreaterThan(float\|int $limit, ?string $message = null): self |
value, limit |
isGreaterThanOrEqual(float\|int $limit, ?string $message = null): self |
value, limit |
isIdentical(mixed $other, ?string $message = null): self |
value, other, type |
isInstanceOf(object\|string $class, ?string $message = null): self |
value, class, type |
isInstanceOfAny(array $classes, ?string $message = null): self |
value, classes, type |
isInt(?string $message = null): self |
value, type |
isIntOrNonEmptyString(?string $message = null): self |
value, type |
isIterable(?string $message = null): self |
value, type |
isLessThan(float\|int $limit, ?string $message = null): self |
value, limit |
isLessThanOrEqual(float\|int $limit, ?string $message = null): self |
value, limit |
isList(?string $message = null): self |
value, type |
isLowercaseString(?string $message = null): self |
value, type |
isMap(?string $message = null): self |
value, type |
isNaturalInt(?string $message = null): self |
value, type |
isNegativeInt(?string $message = null): self |
value, type |
isNonEmptyList(?string $message = null): self |
value, type |
isNonEmptyMap(?string $message = null): self |
value, type |
isNonEmptyString(?string $message = null): self |
value, type |
isNull(?string $message = null): self |
value, type |
isNumeric(?string $message = null): self |
value, type |
isNumericString(?string $message = null): self |
value, type |
isObject(?string $message = null): self |
value, type |
isOneOf(array $choices, ?string $message = null): self |
value, choices |
isPositiveInt(?string $message = null): self |
value, type |
isResource(?string $message = null): self |
value, type |
isSameOrSubclassOf(object\|string $class, ?string $message = null): self |
value, class, type |
isScalar(?string $message = null): self |
value, type |
isString(?string $message = null): self |
value, type |
isSubclassOf(object\|string $class, ?string $message = null): self |
value, class, type |
isTrue(?string $message = null): self |
value, type |
isUppercaseString(?string $message = null): self |
value, type |
isUrl(?string $message = null): self |
value |
matchesRegularExpression(string $pattern, ?string $message = null): self |
value, pattern= |
startsWith(string $needle, ?string $message = null): self |
value, needle |
[!NOTE]
- The
valuecontext is always value-exported except when appended by+which means it is type-exported instead.- The
typecontext is always type-exported. In negated expectations, this context is omitted.- Other context values are value-exported except when appended by
=which means it is integrated as-is.
Available Expectation Classes
Nexus\Assert\Assert::that() returns an instance of Nexus\Assert\Expectation which is the base implementation
of the Nexus\Assert\Expectable interface.
If you want to have a negated expectation, you can invoke not() on the expectation to return an instance of
Nexus\Assert\NegatedExpectation.
If you want to have a nullable expectation, that is, proceed with the expectation only if the input is not
null, then you can invoke nullOr() on the base expectation.
If you want to assert against each key or each value of an iterable input, you can invoke keys() or
values() on the base expectation. These return a Nexus\Assert\KeysIteratingExpectation or
Nexus\Assert\ValuesIteratingExpectation respectively, on which any expectation method is applied to
every element.
[!NOTE] PHP arrays only allow
int|stringkeys. Calling key-iterating methods whose predicate is unreachable on array keys (e.g.keys()->isFloat(),keys()->isObject(),keys()->hasMethod()) throws aLogicExceptionat runtime when the wrapped value is an array. They remain valid on non-array iterables (Iterator,Generator,IteratorAggregate), whose keys are not so constrained.[!NOTE] Currently, the
not(),nullOr(),keys(), andvalues()methods can only be invoked on the baseExpectationobject. They are not yet available on the variant expectations. This can be considered in future versions.
Customising the Exporter
Nexus\Assert\Assert utilises the default implementation of Nexus\Assert\ExporterInterface - Exporter -
to nicely export the value and type of the asserted input. If you need to do some customisations for your
use case, you can implement your own exporter and let Assert use that.
Formatting the Exception Message
ExpectationFailedException accepts a templated message and the context when it is instantiated. You can
modify the message by passing a template message as last argument to any expectation method. The context
values are wrapped in curly braces with no in-between spaces.
When creating your custom messages, you are not compelled to use all available context. However, adding an unknown context for an expectation method will be useless as that won't be interpolated.
Resources
License
This library is licensed under MIT.