phrity / util-transformer
Type transformers, normalizers and resolvers.
Installs: 4 828
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^10.0 | ^11.0 | ^12.0
- squizlabs/php_codesniffer: ^3.5
- symfony/property-access: ^6.0 | ^7.0
- symfony/serializer: ^6.0 | ^7.0
- symfony/translation-contracts: ^3.0
- symfony/uid: ^6.0 | ^7.0
README
Introduction
Type transformers, normalizers and resolvers.
Installation
Install with Composer;
composer require phrity/util-transformer
How to use
All transformers exposes canTransform()
and transform()
methods.
This allows us to transform data of a certain type to another type. A specific transformer may not be able to transform all types.
$transformer = new BasicTypeConverter(); if ($transformer->canTransform($subject)) { $transformed = transformer->transform($subject); }
As option, a transformer can take a target type specifier as second argument.
$transformer = new BasicTypeConverter(); if ($transformer->canTransform($subject, Type::ARRAY)) { $transformed = transformer->transform($subject, Type::ARRAY); }
Utility resolvers enable stacking multiple transformers and performing other tasks.
$transformer = new RecursionResolver( new FirstMatchResolver([ new EnumConverter(), new ReadableConverter(), new ThrowableConverter(), new StringableConverter(), new BasicTypeConverter(), ]) ); if ($transformer->canTransform($subject, Type::STRING)) { $transformed = transformer->transform($subject, Type::STRING); }
List of transformers in this library
Encoders & Decoders
- Flatten Decoder - Expands array with flattened keys to nested array
- Json Decoder - Decodes JSON string
Type converters
- Basic Type - Support transforming all PHP types to all other types
- Enum - Transform Enums to string
- Json Serializable - Transform JSON serializable objects
- Readable - Transform booleans and null to readable strings
- Reversed Readable - Transform some strings to boolean and null
- Stringable - Transform stringable objects to string
- Throwable - Transform throwable to object, array or string
Utility resolvers
- First Match - Collection of transformers that will use first compatible transformer for transformation
- Recursion - Will apply transformer recursively
Wrappers
- Symfony Normalizer - Wrap class implementing Symfony
NormalizerInterface
Versions
Version | PHP | |
---|---|---|
1.2 |
^8.1 |
Additional transformers |
1.1 |
^8.1 |
Additional transformers |
1.0 |
^8.1 |
Initial version |