https://github.com/consistence/consistence
Consistence - consistent approach and additions to PHP's functionality
https://github.com/consistence/consistence
consistence enum php strict-types time-conversion time-formatting
Last synced: 9 months ago
JSON representation
Consistence - consistent approach and additions to PHP's functionality
- Host: GitHub
- URL: https://github.com/consistence/consistence
- Owner: consistence
- License: other
- Created: 2017-01-10T10:06:36.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-06-12T23:57:24.000Z (over 2 years ago)
- Last Synced: 2024-05-17T07:42:20.773Z (over 1 year ago)
- Topics: consistence, enum, php, strict-types, time-conversion, time-formatting
- Language: PHP
- Size: 204 KB
- Stars: 182
- Watchers: 8
- Forks: 15
- Open Issues: 11
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
Consistence
===========
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 [`consistence/consistence`](https://packagist.org/packages/consistence/consistence) with [Composer](https://getcomposer.org/):
```bash
composer require consistence/consistence
```
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](https://github.com/consistence/consistence-doctrine) provides integration to store Consistence value objects in database through entities, there is also [Symfony bundle with this integration](https://github.com/consistence/consistence-doctrine-symfony)
* [JMS Serializer integration](https://github.com/consistence/consistence-jms-serializer) provides integration to (de)serialize Consistence value objects, there is also [Symfony bundle with this integration](https://github.com/consistence/consistence-jms-serializer-symfony)
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](docs/Enum/enums.md)
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.
```php
Learn more about Enums in Consistence](docs/Enum/enums.md)
With [MultiEnums](docs/Enum/multi-enums.md) you can even represent multiple values and operations on them in a single object.
### [Strict Types](docs/Type/strict-types.md)
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.
```php
bar = 'bar';
```
```php
Learn more about Strict Types in Consistence](docs/Type/strict-types.md)
### [Time Format](docs/Time/time-format.md)
Using [value objects](http://martinfowler.com/bliki/ValueObject.html) 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](http://php.net/manual/en/class.datetime.php) 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.
```php
Learn more about Time Formats in Consistence](docs/Time/time-format.md)
### [Arrays](docs/Type/arrays.md)
Arrays in PHP are combination of traditional [lists](https://en.wikipedia.org/wiki/List_(abstract_data_type)) and [dictionaries](https://en.wikipedia.org/wiki/Associative_array) 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:
```php
Learn more about Arrays in Consistence](docs/Type/arrays.md)