Download the PHP package fasano/phprimitives without Composer
On this page you can find all versions of the php package fasano/phprimitives. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fasano/phprimitives
More information about fasano/phprimitives
Files in fasano/phprimitives
Package phprimitives
Short Description Tools for building rich domain primitives.
License MIT
Informations about the package phprimitives
Tools for building rich domain primitives in PHP.
Installation
What is this?
PHP has no base class for scalars - so there's nowhere to hook validation, enforce invariants, or express domain meaning. PHPrimitives fills that gap with 4 base classes:
- AbstractString
- AbstractInteger
- AbstractFloat
- AbstractBoolean
How to use them?
Simply extend the base classes, and implement the validate method. Throw an InvalidArgumentException if the value is invalid.
Why use them?
Primitives enforce invariants at the edges of your application. Validate at boundaries, trust everywhere else. Primitives convert to/from scalars at your application edges:
Without primitives, validation scatters throughout your codebase:
Key benefits:
- Invariants enforced at construction — Invalid data cannot enter your domain
- Type safety through nominal typing — Can't pass
AgewhereTemperatureis expected, even though both wrap integers - Domain concepts expressed clearly —
Emailis more meaningful thanstring
Serialization
Primitives implement JsonSerializable out of the box:
Ecosystem
The wrapping and unwrapping of values into primitive objects can be automated fairly trivially. You can find very simple libs to do so in Symfony and Doctrine below, along with a full example project that uses no scalars anywhere.
| Package | Description |
|---|---|
| phprimitives-doctrine | Doctrine DBAL type mappings |
| phprimitives-symfony | Symfony Serializer integration |
| phprimitives-example | An in-context example usage of PHPrimitives |