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

DateTimeImmutableFormatter

Available since version 3.1.0

DateTimeImmutableFormatterStrategy provides bidirectional conversion between strings and DateTimeImmutable instances.

The strategy uses DateTimeFormatterStrategy for conversion where the input and output formats can be set.

Basic Usage

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

Create and configure strategy

Create the strategy and set the input and output formats via the DateTimeFormatterStrategy.

$strategy = new Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy(
    new Laminas\Hydrator\Strategy\DateTimeFormatterStrategy('Y-m-d')
);

Hydrate data

$hydrated = $strategy->hydrate('2020-07-01');

var_dump($hydrated instanceof DateTimeImmutable); // true

Extract data

$extracted = $strategy->extract(
    DateTimeImmutable::createFromFormat('Y-m-d', '2020-07-01')
);

echo $extracted // '2020-07-01'

Example

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

An example class which represents a music album with a release date:

class Album
{
    private ?DateTimeImmutable $releaseDate;

    public function __construct(?DateTimeImmutable $releaseDate = null)
    {
        $this->releaseDate = $releaseDate;
    }

    public function getReleaseDate() : ?DateTimeImmutable
    {
        return $this->releaseDate;
    }
}

Create hydrator and add strategy

Create a hydrator and add DateTimeImmutableFormatterStrategy as strategy:

$hydrator = new Laminas\Hydrator\ReflectionHydrator();
$hydrator->addStrategy(
    'releaseDate',
    new Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy(
        new Laminas\Hydrator\Strategy\DateTimeFormatterStrategy('Y-m-d')
    )
);

Hydrate data

Create an instance of the example class and hydrate data:

$album = new Album();
$hydrator->hydrate(['releaseDate' => '2020-07-01'], $album);

var_dump($album->getReleaseDate() instanceof DateTimeImmutable); // true

Extract data

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

echo $extracted; // '2020-07-01'
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