Download the PHP package antoninmasek/simple-hydrator without Composer

On this page you can find all versions of the php package antoninmasek/simple-hydrator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package simple-hydrator

Simple Hydrator

Latest Version on Packagist Tests Total Downloads

This package aims to be an extremely easy-to-use object hydrator. You give it an array and it will try to hydrate your data object. I found myself repeating this logic a few times on different projects, so I decided to package it up. I am well aware, that there are far more superior alternatives, but my aim was to do the implementation as simple as possible.

Also, the main objective for me was, to scratch my own itch. So even though the implementation is now limited and possibly contains some issues, for me, it is currently doing the job just fine. If you, however, find yourself using it too and find an issue, feel free to PR it :)

TLDR

This package helps you create objects from arrays like this:

Installation

You can install the package via composer:

Usage

To hydrate your data object you have two options:

Hydrator::hydrate

You can use the Hydrator as follows:

Advantage of this approach is, that your data object does not have to extend anything. Downside is, that without PHP Doc you won't have autocompletion.

YourDataObject::fromArray

This way, you can extend your data object with DataObject abstract class, which will enable you to call fromArray method directly on your data object.

Main advantage is autocompletion as well as better readability. Disadvantage is, that you have to extend your data object. At least the parent. Nested object does not have to extend anything.

Different keys

In a normal circumstances the package maps object property names 1:1 with input array keys. This might not be always optimal. Either the input array might contain invalid characters for PHP properties, or you just want assign your own names. Let's demonstrate this with an example. Imagine we want to get image dimensions and the input array has the following format:

But we want our DTO to look like this:

The solution to this is to use Key attribute and specify the real key value from the source array:

And that's it!

Collections

If you find yourself in a scenario, where you'd have let's say a Car object that has many Key objects:

And you need to cast each of the keys to a Key object, you may add #[Collection(Key::class)] attribute to your data object definition as such:

This ensures correct casting and you will end up with an array of Key objects.

Your root array is list of objects

If your source array is a list of objects and you just want to cast it, then instead of the fromArray method you may use collectionFromArray

DTO Making

For each of your DTO's properties you can use either a camelCase or snake_case approach to set their values which ever suites your preference, in the example below we have the propeties first_name and last_name set on the DTO here.

If you prefer to persist autocompletion you may also use set method, where the first argument is property name and the second one is the value. So to replicate the example above:

Casters

For cases, where the type of property isn't built in PHP, or it needs a special care than just try to fill properties by name it is possible to write a caster.

Caster class

The first way to define a caster is to create a class, that extends AntoninMasek\SimpleHydrator\Casters\Caster. You only need to implement the cast method which is supplied with $value parameter that contains the raw data from the input array, that should be used to hydrate this class.

As an example take a look at simple DateTime caster:

It expects the $value to be a string in valid date format. For example 1969-07-20 and returns a DateTime object with this date.

Anonymous caster

If you don't want to create a caster class you can create anonymous caster by supplying a closure instead of a caster class.

Registering casters

You can register casters in two ways. First is to specify the mapping between all classes and their respective casters:

Or just specify one caster at a time:

To clear all caster you may use:

Overwriting default casters

If any of the default casters in the package does not suit your needs you can easily overwrite it. All you need to do is register your caster for the specific class. Registered casters have higher priority and default casters in the package are used if no mapping for the specific class is supplied.

Notes

Valid characters are the ones, that will pass the following regex: [^a-zA-Z0-9_]

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of simple-hydrator with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
phpunit/phpunit Version ^9.5 || ^10.0 || ^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package antoninmasek/simple-hydrator contains the following files

Loading the files please wait ....