sylarele/object-metadata-mapper
最新稳定版本:v0.3.1
Composer 安装命令:
composer require sylarele/object-metadata-mapper
包简介
README 文档
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 |
统计信息
- 总下载量: 988
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-11