msstc4php / vo-base-bundle
Symfony Validator constraints and a bundle for msstc4php/vo-base.
Package info
github.com/msstc4php/vo-base-bundle
Type:symfony-bundle
pkg:composer/msstc4php/vo-base-bundle
Requires
- php: >=8.4
- msstc4php/vo-base: ^1.4
- symfony/config: ^7.4|^8.0
- symfony/dependency-injection: ^7.4|^8.0
- symfony/http-kernel: ^7.4|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.76
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: >=11.0
- rector/rector: ^2.1
- roave/security-advisories: dev-latest
- symfony/framework-bundle: ^7.4|^8.0
- symfony/property-access: ^7.4|^8.0
- symfony/serializer: ^7.4|^8.0
- symfony/validator: ^7.4|^8.0
Suggests
- msstc4php/vo-base-doctrine: To have the bundle register the Doctrine ORM/ODM types as well.
- msstc4php/vo-documents-doctrine: To have the bundle register the identity-document Doctrine types as well.
- msstc4php/vo-finance-doctrine: To have the bundle register the finance Doctrine types as well.
- msstc4php/vo-gs1-doctrine: To have the bundle register the GS1 Doctrine types as well.
- msstc4php/vo-identifiers-doctrine: To have the bundle register the identifier Doctrine types as well.
- symfony/validator: To validate raw input against value object rules via constraints.
Provides
None
Conflicts
- msstc4php/vo-base-doctrine-bridge: *
- msstc4symfony/vo-base-bridge: *
- symfony/symfony: *
Replaces
None
README
Symfony Validator constraints and a bundle for
msstc4php/vo-base.
One line in bundles.php registers the constraint validator and — if
msstc4php/vo-base-doctrine happens to be installed — the
Doctrine ORM and ODM types as well, together with those of any installed domain Doctrine package
(see Doctrine types).
Install
Neither package is on Packagist yet:
{
"repositories": [
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-base" },
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-base-bundle" }
],
"require": {
"msstc4php/vo-base-bundle": "^1.3"
}
}
Doctrine is not required. If you want the field types, also add
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-base-doctrine" } to repositories and
require msstc4php/vo-base-doctrine:^1.0; the bundle picks it up on its own.
Enable the bundle
// config/bundles.php return [ MSSTC4PHP\ValueObject\Bundle\Msstc4phpVoBaseBundle::class => ['all' => true], ];
That is the whole setup. Each group registers when its dependency is installed (and, for Doctrine, its extension is present), and stays quiet otherwise.
Configuration
# config/packages/msstc4php_vo_base.yaml msstc4php_vo_base: orm: ~ # null auto-detects, false suppresses, true demands odm: ~ domain_types: true # also the types of the installed domain Doctrine packages validator: ~ serializer: ~ # value objects in the Serializer, e.g. behind #[MapRequestPayload] value_resolver: ~ # controller arguments from route attributes; on unless false
true makes a missing dependency an error at container build time instead of
silent absence — useful when a deployment must not start without the types.
Doctrine types
When the bundle registers the DBAL types (orm) or the ODM types (odm), it prepends the types of
msstc4php/vo-base-doctrine into doctrine.dbal.types / doctrine_mongodb.types, and with them the types of each of
these packages that is installed:
| Package | Type names |
|---|---|
msstc4php/vo-gs1-doctrine |
gs1_* |
msstc4php/vo-identifiers-doctrine |
id_* |
msstc4php/vo-documents-doctrine |
vod_* |
msstc4php/vo-finance-doctrine |
fin_* |
The bundle only suggests them: install the ones you need, and nothing else is needed. None of them is on Packagist,
and Composer reads repositories from your root composer.json only, so each vo-X-doctrine needs its own repository
and the one of vo-X; vo-identifiers also needs vo-gs1. For example, for the GS1 and identifier types:
{
"repositories": [
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-gs1" },
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-gs1-doctrine" },
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-identifiers" },
{ "type": "vcs", "url": "https://github.com/msstc4php/vo-identifiers-doctrine" }
],
"require": {
"msstc4php/vo-gs1-doctrine": "^1.0",
"msstc4php/vo-identifiers-doctrine": "^1.0"
}
}
A package that is not installed adds nothing and is no error. The map is prepended, so a type of the same name in your
own doctrine.dbal.types or doctrine_mongodb.types still wins. Between the packages a name is never shared (distinct
prefixes); if it were, the vo-base-doctrine type and then the earlier package in the table would keep it.
Every listed DBAL type is created on the first connection of each PHP process (DoctrineBundle registers the whole map;
ODM stores only class names). The four packages together add about 300 types: measured on PHP 8.4 CLI, that is about
5 ms and 1.6 MB more per process than the 40 vo-base-doctrine types alone (6 ms against 1 ms with opcache). If you
use only a few domain types, domain_types: false keeps only the vo-base-doctrine types and you map those few
yourself.
Validating raw input
use MSSTC4PHP\ValueObject\Bundle\Framework\Validator\AssertEmailAddress; #[AssertEmailAddress] public string $email = '';
The constraint runs the value object's own constructor and turns its exception
into a violation, so the rule lives in one place rather than being restated as
constraint options. ValidValueObject does the same for any value object by
class name.
Serializer
With symfony/serializer installed, a DTO property can be typed with a value object:
final readonly class SignUp { public function __construct(public EmailAddress $email, public ?Port $port = null) {} } #[Route('/sign-up', methods: ['POST'])] public function signUp(#[MapRequestPayload] SignUp $input): Response
{"email": "not an email"} becomes a 422 for the property path email with Symfony's generic text ("This value
should be of type string.") and the value object's message in the violation's hint parameter; no internal class
name reaches the client. Values go out in
their storage form: strings, numbers, and {"min": 1, "max": 5}-style objects for IntRange, GeoCoordinates and
DateRange (with microseconds and offset, unlike jsonSerialize(); input must carry an offset). BoundedString is
never built from input (a BoundedString property is a 422): its payload carries the length limits, so the client
would choose them; type the property as string and validate it with Length instead. Numbers are also accepted as
numeric strings ("8080"), as they arrive from forms, XML and CSV. Your own composite value objects work once their PayloadCodec is a service: it is autoconfigured.
A package's codecs can be registered as services in one block. For vo-documents' composite names:
# config/services.yaml services: _defaults: { autoconfigure: true } MSSTC4PHP\ValueObject\Documents\Name\: resource: '%kernel.project_dir%/vendor/msstc4php/vo-documents/src/Name/**/PersonNameCodec.php'
Bundle 2.1 does the same from one option, codec_providers: ['MSSTC4PHP\ValueObject\Documents\Codecs'].
Route arguments
#[Route('/users/{email}')] public function show(EmailAddress $email): Response
An invalid {email} is a 404, as with Symfony's enum resolver. Only string-, int- and float-backed value objects;
the ISO enums are handled by Symfony itself.
Development
make check # php -l, PHPStan level max, cs-fixer, composer validate/audit, Rector, deptrac
make test # PHPUnit: unit, integration
make fix # cs-fixer + Rector, applied
The Doctrine stack sits only in composer-ci.json, purely for the bundle and
integration tests. A CI job installs composer.json without it and still expects
every class to load — that is what keeps "suggest, not require" honest. Another job drops the domain Doctrine
packages and runs the tests on the lowest dependencies, so the vo-base ^1.1 and vo-base-doctrine ^1.1 floors stay
proven.