marvin255 / value-object-bundle
Set of value objects for Dotrine in Symfony.
Package info
github.com/marvin255/value-object-bundle
Type:symfony-bundle
pkg:composer/marvin255/value-object-bundle
Requires
- php: >=8.4
- doctrine/doctrine-bundle: ^2.0|^3.0
- doctrine/orm: ^3.0
- marvin255/value-object: ^0.3
- symfony/framework-bundle: ^6.0|^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- infection/infection: ^0.32
- phpunit/phpunit: ^12.0|^13.0
- vimeo/psalm: ^6.0
README
Symfony bundle that provides Doctrine types for common value objects such as Email, Uri, String, Integer, and FileInfo.
Installation
Install the package via Composer:
composer require marvin255/value-object-bundle
Configuration
Add the bundle to your config/bundles.php (if not done automatically):
return [ // ... Marvin255\ValueObjectBundle\Marvin255ValueObjectBundle::class => ['all' => true], ];
Usage
The bundle provides Doctrine DBAL types that automatically convert database values to value objects and vice versa.
Supported Value Object Types
marvin255_string- StringValueObjectmarvin255_non_empty_string- StringNonEmptyValueObjectmarvin255_email- EmailValueObjectmarvin255_uri- UriValueObjectmarvin255_file_info- FileInfoValueObjectmarvin255_integer- IntValueObjectmarvin255_positive_integer- IntPositiveValueObjectmarvin255_negative_integer- IntNegativeValueObjectmarvin255_non_negative_integer- IntNonNegativeValueObjectmarvin255_non_positive_integer- IntNonPositiveValueObjectmarvin255_float- FloatValueObjectmarvin255_percentage- PercentageValueObjectmarvin255_bc_math_number- BcMathNumberValueObject (requires bcmath PHP extension)
Optional Types
Some types are only available when their corresponding PHP extensions are installed:
BcMath Type (marvin255_bc_math_number)
The BcMathNumberValueObject type provides arbitrary-precision decimal arithmetic using PHP's bcmath extension. It's automatically registered only if the bcmath extension is enabled.
To enable bcmath on your PHP installation, ensure it's installed and uncommented in php.ini:
extension=bcmath
If the extension is not available, the type will not be registered but the rest of the bundle will continue to function normally.
Define Entity Properties
Use the Doctrine types in your entity mapping:
namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Marvin255\ValueObject\EmailValueObject; use Marvin255\ValueObject\StringValueObject; #[ORM\Entity] class User { #[ORM\Column(type: 'marvin255_string')] private ?StringValueObject $name = null; #[ORM\Column(type: 'marvin255_email')] private EmailValueObject $email; public function setEmail(EmailValueObject $email): void { $this->email = $email; } public function getEmail(): EmailValueObject { return $this->email; } }
Working with Value Objects
Create and work with value objects naturally in your code:
use Marvin255\ValueObject\EmailValueObject; use Marvin255\ValueObject\StringValueObject; $email = new EmailValueObject('user@example.com'); $name = new StringValueObject('John Doe'); $user->setEmail($email); $user->setName($name); $repository->save($user);
The bundle automatically handles conversion between value objects and their database representations.