Download the PHP package slevomat/consistence without Composer
On this page you can find all versions of the php package slevomat/consistence. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package consistence
Consistence
This package is fork of consistence/consistence
that adds support PHP 8.
PHP offers a lot of handy functionality, but due to its organic growth this is not always easily usable. The aim of this library is to provide consistent approach to PHP's functionality. This means:
- clear and consistent naming patterns
- consistent arguments order
- errors reported as Exceptions, never as return values
- added functionality and interfaces, which are missing
- value objects representing common elements
Installation
Install package slevomat/consistence
with Composer:
There are no further steps needed, you can start using Consistence whenever suitable in your codebase, see features below.
Integrations
If you are using one of the following libraries/frameworks, check out these integrations:
- Doctrine ORM integration provides integration to store Consistence value objects in database through entities, there is also Symfony bundle with this integration
- JMS Serializer integration provides integration to (de)serialize Consistence value objects, there is also Symfony bundle with this integration
Documentation & features
In following sections, there are excerpts of Consistence functionality helping in key areas along with links to dedicated documentation pages.
Enums and MultiEnums
Enums represent predefined set of values. The available values are defined statically by each enum class. Each value is represented by an instance of this class in a flyweight manner. This ensures that the same values are always the same instance.
The main advantages of using enums for representing set of values are:
- you can be sure, that the value is unchanged (not only validated once)
- you can use type hints to check that only the defined set of values is acceptable
- you can define behavior on top of the represented values
> Learn more about Enums in Consistence
With MultiEnums you can even represent multiple values and operations on them in a single object.
Strict Types
PHP contains by default a lot of magic behavior and checks almost nothing. When particular type is expected, PHP tries to convert given type to the expected one, which can yield unexpected results. Consistence promotes using strict types to achieve more predictable, maintainable and readable code.
> Learn more about Strict Types in Consistence
Time Format
Using value objects is great, because you get clear type hint and you can be sure, that the value you get is in consistent state and that it is valid (if the representation follows the principles).
DateTime is one of the few value objects which are natively in PHP. Unfortunately, PHP again prefers using magic behavior over clear errors by default and there is also quite a lot of commonly required functionality missing, so Consistence tries to help with these shortcomings.
> Learn more about Time Formats in Consistence
Arrays
Arrays in PHP are combination of traditional lists and dictionaries and therefore have a wide range of applications. Because they are so common, there are a lot of repeating operations for manipulation of these data structures. These operations should be abstracted away into function, so that they can be reused and the code contains only essential business logic.
Simple finding of a value by a custom rule ("find values shorter than five characters") can be cumbersome:
In the previous example, there is logic of going through the array mixed with business logic. This can be improved by using a dedicated method for iteration. Then only the business logic remains in the callback:
PHP has a lot of array-manipulation functions, but using them is difficult, because they do not have unified API and never throw any Exceptions, errors are reported only through return values. These functions also use non-strict comparisons while searching by default. Consistence tries to solve those issues.
> Learn more about Arrays in Consistence