sylarele / object-metadata-mapper
Package info
github.com/sylarele/object-metadata-mapper
pkg:composer/sylarele/object-metadata-mapper
Requires
- php: ^8.3|^8.4|^8.5
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.5|^13.0
- rector/rector: ^2.3
- shipmonk/composer-dependency-analyser: ^1.8
- structuraphp/structura: ^0.7.1
- sylarele/php-cs-fixer-config: ^2.0
- dev-master
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.0
- v0.0.1
- dev-feat/projet-version
- dev-dependabot/composer/master/rector/rector-2.0.15
- dev-dependabot/composer/master/phpunit/phpunit-12.1.5
- dev-dependabot/composer/master/phpstan/phpstan-2.1.14
- dev-dependabot/composer/master/friendsofphp/php-cs-fixer-3.75.0
- dev-develop
- dev-dependabot/composer/master/phpstan/phpstan-phpunit-2.0.6
- dev-fix/t-enum-in-metadata-service
This package is auto-updated.
Last update: 2026-04-02 13:09:53 UTC
README
A PHP library for describing metadata and fake data on classes via PHP attributes, then querying them through a generic service.
Installation
composer require sylarele/object-metadata-mapper
How it works
Annotate a class with Mapper attributes to describe its structure and fake values.
Implement MetadataService to resolve which class corresponds to a key (a BackedEnum).
Call findByKey() to get a MetadataDto containing the descriptions and fake data.
Annotating a class
Each attribute takes a key (field name) and an optional description.
use Sylarele\ObjectMetadataMapper\Attributes\ObjectMapper; use Sylarele\ObjectMetadataMapper\Attributes\FullNameMapper; use Sylarele\ObjectMetadataMapper\Attributes\EmailMapper; use Sylarele\ObjectMetadataMapper\Attributes\PhoneMapper; use Sylarele\ObjectMetadataMapper\Attributes\ObjectEachMapper; use Sylarele\ObjectMetadataMapper\Attributes\AddressMapper; #[ObjectMapper( 'user', new FullNameMapper('fullname'), new PhoneMapper('phone'), new EmailMapper('email'), new ObjectEachMapper( 'addresses', 2, new AddressMapper('address'), ) )] class UserObject { }
Implementing MetadataService
Create a BackedEnum as a key, then extend MetadataService by implementing getClassName() to map each enum value to an annotated class.
// The key enum enum UserType: string { case Profile = 'profile'; }
use Sylarele\ObjectMetadataMapper\MetadataService; /** * @extends MetadataService<UserType> */ class UserMetadataService extends MetadataService { protected function getClassName(BackedEnum $keyTemplate): string { return match ($keyTemplate) { UserType::Profile => UserObject::class, }; } }
Using the service
$service = new UserMetadataService(); $dto = $service->findByKey(UserType::Profile); // Structured fake data $dto->fake; // Field descriptions $dto->description;
MetadataDto structure
| Property | Type | Description |
|---|---|---|
$template |
BackedEnum |
The key used |
$description |
array<string, string> |
Mapping key => description |
$fake |
array<string, mixed> |
Mapping key => fake value |
Available attributes
| Attribute | Description |
|---|---|
StringMapper |
Short string (max 60 characters) |
TextMapper |
Long text |
IntegerMapper |
Integer |
BooleanMapper |
Boolean |
DateMapper |
Date (Y-m-d) |
DateTimeMapper |
Date and time |
TimeMapper |
Time |
EmailMapper |
Email address |
PhoneMapper |
Phone number |
UrlMapper |
URL |
ImageMapper |
Image URL |
FirstNameMapper |
First name |
LastNameMapper |
Last name |
FullNameMapper |
Full name |
AddressMapper |
Postal address |
AmountMapper |
Numeric amount |
EnumValueMapper |
Enum value |
ReferenceMapper |
Reference (UUID / ID) |
ArrayMapper |
Array of values |
StrictArrayMapper |
Strictly typed array |
ArrayEachMapper |
Array with N occurrences of a mapper |
StrictArrayEachMapper |
Strict array with N occurrences |
ObjectMapper |
Object composed of sub-mappers |
ObjectEachMapper |
Array of N composed objects |