Download the PHP package beberlei/assert without Composer
On this page you can find all versions of the php package beberlei/assert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
All versions of assert with dependencies
Informations about the package assert
A simple php library which contains assertions and guard methods for input validation (not filtering!) in business-model, libraries and application low-level code. The library can be used to implement pre-/post-conditions on input data.
Idea is to reduce the amount of code for implementing assertions in your model and also simplify the code paths to implement assertions. When assertions fail, an exception is thrown, removing the necessity for if-clauses in your code.
The library is not using Symfony or Zend Validators for a reason: The checks have to be low-level, fast, non-object-oriented code to be used everywhere necessary. Using any of the two libraries requires instantiation of several objects, using a locale component, translations, you name it. Its too much bloat.
Real time usage with Azure Blob Storage:
A helper method (
Assertion::nullOr*) is provided to check if a value is null OR holds for the assertion:
Assertion::all* method checks if all provided values hold for the
assertion. It will throw an exception of the assertion does not hold for one of
Using the static API on values is very verbose when checking values against multiple assertions.
Starting with 2.6.7 of Assert the
Assert class provides a much nicer fluent API for assertions, starting
Assert::that($value) and then receiving the assertions you want to call
on the fluent interface. You only have to specify the
There are also two shortcut function
the "nullOr" or "all" helper respectively.
There are many cases in web development, especially when involving forms, you want to collect several errors
instead of aborting directly on the first error. This is what lazy assertions are for. Their API
works exactly like the fluent
Assert::that() API, but instead of throwing an Exception directly,
they collect all errors and only trigger the exception when the method
verifyNow() is called on the
that($value, $propertyPath) requires a property path (name), so that you know how to differentiate
the errors afterwards.
verifyNow() will throw an exception
Assert\\LazyAssertionException with a combined message:
The following 3 assertions failed: 1) foo: Value "10" expected to be string, type integer given. 2) bar: Value "<NULL>" is empty, but non empty value was expected. 3) baz: Value "string" is not an array.
You can also retrieve all the
AssertionFailedExceptions by calling
This can be useful for example to build a failure response for the user.
For those looking to capture multiple errors on a single value when using a lazy assertion chain,
you may follow your call to
tryAll to run all assertions against the value, and
capture all of the resulting failed assertion error messages. Here's an example:
The above shows how to use this functionality to finely tune the behavior of reporting failures, but to make
catching all failures even easier, you may also call
tryAll before making any assertions like below. This
helps to reduce method calls, and has the same behavior as above.
The following functions exist as aliases to
Assert static constructors:
Using the functional or static constructors is entirely personal preference.
Note: The functional constructors will not work with an
However it is trivial to recreate these functions in a way that point to the extended class.
List of assertions
Remember: When a configuration parameter is necessary, it is always passed AFTER the value. The value is always the first parameter.
Exception & Error Handling
If any of the assertions fails a
Assert\AssertionFailedException is thrown.
You can pass an argument called to any assertion to control the
exception message. Every exception contains a default message and unique message code
Assert\AssertionFailedException is just an interface and the default
Assert\InvalidArgumentException which extends the SPL
InvalidArgumentException. You can change the exception being used on a
package based level.
Customised exception messages
You can pass a callback as the message parameter, allowing you to construct your own message only if an assertion fails, rather than every time you run the test.
The callback will be supplied with an array of parameters that are for the assertion.
As some assertions call other assertions, your callback will need to example the array to determine what assertion failed.
The array will contain a key called
::assertion that indicates which assertion
The callback should return the string that will be used as the exception message.
Your own Assertion class
To shield your library from possible bugs, misinterpretations or BC breaks inside Assert you should introduce a library/project based assertion subclass, where you can override the exception thrown as well.
In addition, you can override the
Assert\Assertion::stringify() method to
provide your own interpretations of the types during error handling.
As of V2.9.2, Lazy Assertions now have access to any additional assertions present in your own assertion classes.
Please see CONTRIBUTING for more details.
ext-intl Version *
ext-simplexml Version *
ext-mbstring Version *
ext-ctype Version *
ext-json Version *