Download the PHP package xthiago/id-value-object without Composer
On this page you can find all versions of the php package xthiago/id-value-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xthiago/id-value-object
More information about xthiago/id-value-object
Files in xthiago/id-value-object
Package id-value-object
Short Description A Value Object (VO) for Object Identity (ID). Includes a Doctrine DBAL type for persistence.
License MIT
Informations about the package id-value-object
Id Value Object
PHP library to make working with object identity (ID) easier and fun!
In short: You shouldn't rely on database mechanisms to generate IDs. Also, you shouldn't manipulate scalar values (usual int) over all the application. Wherever you need to generate an ID, use this ID value object.
Features
UUID v4
The library automatically generates UUID v4 as ID. You will not rely on persistence mechanism and flush operations to generate identity.
Integration with Doctrine
The library comes with a Doctrine DBAL Type which allows you to map the Id value object as an Doctrine Entity attribute.
Requirements
This library requires PHP >= 7.2.
Installation
Use composer to install the library.
Doctrine
To make use of Doctrine integration for persistence you have to configure the Doctrine DBAL Type given by this package.
Standalone Doctrine
You must register the DBAL Type in your application boostrap like following:
Symfony Framework
If you are using Symfony, you just need to edit the following the Doctrine configuration to add the following:
Usage
Basic examples:
`
Mapping Id in your entity class (e.g. Product):
Creating custom ID classes
Instead of rely on a generic Id
class for all entities, you can create specific value objects for each entity.
For example, give you have a Product
entity, you can create a ProductId
value object in the following way:
Then you will need to map this new type. You can just extend DoctrineDbalType
in the following way:
Then you configure this new type (e.g. Type::addType(ProductIdDbalType::NAME, ProductIdDbalType::class);
) and start
using it in your model (this time I will show the mapping using PHP Attributes):
Symfony Serializer
This library includes a class IdInterfaceNormalizer
able to
normalize and denormalize instances of Id
value object.
To use that normalizer, you need to pass it as an argument of Serializer
class constructor:
FreezesUuidTrait
utility to help with tests
When creating tests, we may want to know the value of the generated IDs beforehand. This lets us write more simpler
assertions. This library provides a trait FreezesUuidTrait
that could be used
to freeze the uuid v4 generation or to set a known sequence.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
MIT