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 tools to hydrate and extract data between arrays and objects. Includes hydrator strategies, naming conventions, and integration helpers for forms and domain models, supporting multiple hydrator implementations and extensions.

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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi