Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Util Transformer Laravel Package

phrity/util-transformer

Lightweight PHP utility for transforming values between types. Provides transformers with canTransform()/transform(), plus resolvers to chain and recurse converters. Includes JSON/flatten decoders and converters for basic types, DateTime, enums, Stringable, Throwable and more.

View on GitHub
Deep Wiki
Context7

Build Status Coverage Status

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

Type converters

  • Basic Type - Support transforming all PHP types to all other types
  • DateTime - Transform DateTime classes to string
  • 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

  • Chained - Collection of transformers that will send output from one into next one
  • First Match - Collection of transformers that will use first compatible transformer for transformation
  • Recursion - Will apply transformer recursively
  • String - Fold anything to a string

Wrappers

Versions

Version PHP
1.3 ^8.1 Transformer: DateTime, Resolver: Chained, String
1.2 ^8.1 Additional transformers
1.1 ^8.1 Additional transformers
1.0 ^8.1 Initial version
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cadot.eu/make
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky