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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package assert

Nexus Assert

PHP Version Latest Release Unit Tests Static Code Analysis

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 value context is always value-exported except when appended by + which means it is type-exported instead.
  • The type context 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|string keys. Calling key-iterating methods whose predicate is unreachable on array keys (e.g. keys()->isFloat(), keys()->isObject(), keys()->hasMethod()) throws a LogicException at 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(), and values() methods can only be invoked on the base Expectation object. 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.


All versions of assert with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package nexusphp/assert contains the following files

Loading the files please wait ...