Download the PHP package rammewerk/hydrator without Composer

On this page you can find all versions of the php package rammewerk/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 hydrator

Rammewerk Hydrator - A strongly typed hydrator

The Rammewerk Hydrator is a fast, reflection-based hydrator for PHP 8.4+, built to effortlessly map arrays onto PHP objects with minimal configuration.

It’s not an ORM - no annotations or heavy frameworks - just a simple yet powerful mapper. It even includes a lazy-loading HydratorCollection for handling large datasets.

Installation

Basic usage

Let's consider a simple entity/dto:

We can populate this from an array:

In return, we got a typed entity.

This might be useful to automatically convert data from database to an entity, or from an API etc.

Features

Benefits

Allowed Property Types

The hydrator ensures each property is set to its declared type by converting raw data as needed (e.g., booleans stored as integers). It supports:

Note: One should try not to use union/intersection types, as they may lead to unexpected results. Use with caution.

Nested hydration

If the hydrator encounters a property of type class, it will hydrate the class if given data is an array.

To handle an array of classes, you can use the built-in mapArray method:

Hydrator Collection

Collections are lazy-loaded, and will only hydrate the entities when you iterate over them. This is useful for large datasets, as it will only hydrate the entities that are actually needed. If needed all at once, you can use the toArray() method.

Constructor parameters

When your class uses constructor parameters, the hydrator automatically resolves them, but with a few rules:

Example:

Read more on this below in the section about object design.

Read only properties

To add readonly properties, they must be initiated through the constructor.

Why Use the Hydrator?

The hydrator helps convert raw data (e.g., from a database) into typed PHP objects without tedious manual checks. It enforces strict types, simplifies property access, and makes your code more maintainable.

Without hydrator

With Hydrator

In the hydrator version, your IDE can autocomplete $user properties because it knows each user is a User object. It also ensures that only defined properties on the User entity are hydrated - no unexpected data creeping in.

There’s no hidden __call() magic or dynamic property illusions - just strictly typed objects, so you always know exactly what data you’re working with.

To achieve this, simply return a HydratorCollection from your repository:

Now you get a collection of actual User objects, each property strictly typed, making your code cleaner and more reliable.

Be Mindful of Object Design

While the hydrator is powerful, it doesn’t replace good object design.

Avoid making your classes reliant on the hydrator by declaring uninitialized or non-optional properties without defaults. If you attempt to access such properties before the hydrator sets them, you’ll encounter exceptions. Instead, use sensible defaults or optional properties, and ensure your constructor aligns with how data is actually provided. See below for an example.

Set reasonable defaults:

The $name property is nullable, but it’s initialized with an empty string. This means the hydrator will never set it to null; instead, it will use '', even when null is provided. To allow null values, initialize the property like this:

Define Required Properties via Constructor

Avoid declaring non-optional properties without initializing them. This is bad practice as it can lead to uninitialized properties.

Best Practice: Define required properties through the constructor. If a property should be read-only, use the readonly keyword in the constructor’s promoted parameters.

Benefits:

Limited Hydration for Non-Promoted Parameters

Non-promoted constructor parameters aren’t automatically hydrated because they often don’t match a specific property or may be used for computed values. The hydrator’s role is to fill properties, not infer custom constructor logic.

If you need non-promoted parameters, give them defaults, make them nullable, or manually instantiate your object before hydration:

Preserving Properties Set by the Constructor

The hydrator won’t overwrite properties already set to values different from their default. This prevents overwriting values explicitly set by your constructor. For example:

If the hydrator is given ['age' => 20], it won’t override age (already set to 18 by the constructor). This behavior ensures constructor-defined values are respected.

Hydrator Callback for More Customizable Hydration

The hydrator can be used as a callback, returning the hydrated object. The first parameter of hydrate() is an array of data to hydrate with, and the second parameter is a callback that customizes hydration.

Note: Data given in the first parameter will override values returned by the callback.

Example Usage

By using this approach, you can define a default set of properties in the first parameter, while the callback provides additional values that are used unless explicitly overridden by the initial array.

Error handling

All exceptions extend Rammewerk\Component\Hydrator\Error\HydratorException. Catch them to manage invalid data.

Contribution

If you have any issues or would like to contribute to the development of this library, feel free to open an issue or pull request.

License

The Rammewerk Hydrator is open-sourced software licensed under the MIT license.


Keywords: DTO, mapper, data-mapper and populator

Converters


All versions of hydrator with dependencies

PHP Build Version
Package Version
Requires php Version >=8.4
ext-ctype Version *
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 rammewerk/hydrator contains the following files

Loading the files please wait ....