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

Laminas Hydrator Laravel Package

laminas/laminas-hydrator

Laminas Hydrator provides flexible strategies to hydrate and extract data between objects and arrays. Supports reflection, class methods, naming strategies, and custom hydrators, making it easy to map entities, DTOs, and forms cleanly across your application.

View on GitHub
Deep Wiki
Context7

BackedEnum

INFO: New Feature Available since version 4.8.0

MISSING: Installation Requirements Enumerations require PHP version 8.1 or higher.

The BackedEnumStrategy provides bidirectional conversion between strings or integers and Backed Enums.

The code examples below will use the following backed enum, representing a genre of music:

enum Genre: string
{
    case Pop   = 'pop';
    case Blues = 'blues';
    case Jazz  = 'jazz';
}

Basic Usage

The following code example shows standalone usage without adding the strategy to a hydrator.

Create and Configure Strategy

Create the strategy passing the class name of the enum it will hydrate and extract:

$strategy = new Laminas\Hydrator\Strategy\BackedEnumStrategy(Genre::class);

Hydrate Data

$hydrated = $strategy->hydrate('blues', null);
var_dump($hydrated); // enum Genre::Blues : string("blues");

Extract Data

$extracted = $strategy->extract(Genre::Pop);
var_dump($extracted); // string(3) "pop"

Example

The following example demonstrates hydration for a class with a property.

An example class which represents an album with a music genre:

class Album
{
    private ?Genre $genre;

    public function __construct(?Genre $genre = null)
    {
        $this->genre = $genre;
    }

    public function getGenre() : Genre
    {
        return $this->genre;
    }
}

Create Hydrator and Add Atrategy

Create a hydrator and add the BackedEnumStrategy as a strategy:

$hydrator = new Laminas\Hydrator\ReflectionHydrator();
$hydrator->addStrategy(
    'genre',
    new Laminas\Hydrator\Strategy\BackedEnumStrategy(Genre::class)
);

Hydrate Data

Create an instance of the example class and hydrate data:

$album = new Album();
$hydrator->hydrate(['genre' => 'jazz'], $album);

echo $album->getGenre()->value; // "jazz"

Extract Data

$extracted = $hydrator->extract($album);

echo $extracted['genre']; // "jazz"
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle