Download the PHP package stratadox/hydration-mapping without Composer

On this page you can find all versions of the php package stratadox/hydration-mapping. 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 hydration-mapping

Hydration Mapping

Build Status codecov Infection Minimum PhpStan Level Scrutinizer Code Quality Maintainability Latest Stable Version License

Implements Latest Stable Version License

Mappings for hydration purposes.

Maps array or array-like data structures to object properties, in order to assemble the objects that model a business domain.

Installation

Install using composer:

composer require stratadox/hydration-mapping

Purpose

These mapping objects define the relationship between an object property and the source of the data.

Typical Usage

Typically, hydration mappings are given to MappedHydrator instances. Together they form a strong team that solves a single purpose: mapping data to an object graph.

For example:

More often, the mapped hydrator is given to a deserializer:

Mapping

Three types of property mappings are available:

Scalar Mapping

Scalar typed properties can be mapped using the *Value classes. The following scalar mappings are available:

Scalar mappings are created through the named constructors:

Basic Validation

When appropriate, these mappings validate the input before producing a value. For instance, the IntegerValue mapping checks that:

This process can be skipped by using the Casted* mappings instead. They provide a minor speed bonus at the cost of decreased integrity. Casted* mappings are available as:

To skip the entire typecasting process, the OriginalValue mapping can be used.

Input to a BooleanValue must either be 0, 1 or already boolean typed. Custom true/false values can be provided as optional parameters:

Nullable- and Mixed values

Each of the above mappings can be made nullable by wrapping the mapping with CanBeNull.

For example, instead of IntegerValue::inProperty('foo'), the foo property can be made nullable with: CanBeNull::or(IntegerValue::inProperty('foo')).

In the same style, mixed value types can be configured. To map a value that could be either an int or a float, as numeric PHP values are often found, CanBeInteger can be used: CanBeInteger::or(FloatValue::inProperty('foo'))). This mapping will first check if the value can safely be transformed into an integer, and fall back to a floating point value. Non-numeric values will result in an exception, denoting where and why the input data could not be mapped.

These mixed mapping can be combined (as is customary for decorators) to produce, for instance, mapping configurations that first attempt to map the value to a boolean, otherwise as an integer, if that cannot be done to cast it to a floating point, and if all else fails, make it a string:

Relationship Mapping

Relationships can be mapped with a monogamous HasOne* or polygamist HasMany* mapping.

Each of these are connected to the input data in one of three ways:

This boils down to the following possibilities:

Relationship mappings are created through the named constructors:

In this context, the term key refers to the key of the associative array from which the object data is mapped, also known as offset, index or position.

Nested vs Embedded

For *Embedded classes, there is no inPropertyWithDifferentKey. Instead of relying on an embedded array in the key, they are given the original input array and compose their attributes from one or more of its values.

Has One

HasOne*-type relationships are each given an object that Deserializes the related instance.

A HasOneNested receives the value that was found in the original input for the given key. This value must be an array, presumably associative.

HasOneEmbedded mappings take a different approach: they produce a new object from the data in the original input array. This approach is useful when mapping, for example, embedded values.

Has Many

A HasMany* relation requires one object that Deserializes the collection, and one that Deserializes the items.

This approach allows for a lot of freedom in the way collections are mapped. The available deserializers can map the collection either as plain array or to a custom collection object.

These deserializers may in turn use mapped hydrator instances. The combination
is able to map entire structures of objects in all kinds and shapes.

Proxies

Proxies are used to allow for lazy loading. Rather than deserializers, they take a factory to create objects that, in turn, load the "real" object in place of the proxy whenever called upon.

Lazy has-one relations can be mapped with the HasOneProxy mapping. Lazy has-many relationships have the option to be normally lazy, or extra lazy. For extra lazy relations, the HasManyProxies mapping is used. When the relation is "regular" lazy, it is mapped as HasOneProxy, where "one" refers to one collection.

The latter only works when the collection is contained in a collection object. In cases where objects that are contained in an array should be lazy-loaded, a HasManyProxies mapping should be used, where each proxy is configured to load the entire array when called upon.

Using this mechanism, both lazy and extra-lazy loading is supported through any type of collection, whether it be an array or a collection object.

Bidirectional

Bidirectional one-to-many and one-to-one relationships can be mapped using the HasBackReference mapping.

This mapping acts as an observer to the hydrator for the owning side, assigning the reference of the "owner" object to the given property.

Advanced validation

Advanced input validation can be applied with a ConstrainedMapping. A ConstrainedMapping will produce the value of the mapping if the specification is satisfied with it, or throw an exception otherwise.

For example, a check on whether a rating is between 1 and 5 might look like this:

The constraints themselves implement the (minimal) interface Satisfiable, which mandates only the method isSatisfiedBy($input).

The recommended way to implement custom constraints is by extending the abstract Specification class:

Or by using the Specifying trait:

Default values

To honour the PHP spirit, a class is available that loads a default value rather than propagating the exception: Defaults::to(-1, IntegerValue::inProperty('foo'))

Extension

The ClosureMapping provides an easy extension point. It takes in an anonymous function as constructor parameter. This function is called with the input data to produce the mapped result.

For additional extension power, custom mapping can be produced by implementing the Mapping interface.


All versions of hydration-mapping with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2
ext-json Version *
stratadox/hydration-mapping-contracts Version ^0.4
stratadox/hydrator-contracts Version ^0.8
stratadox/proxy-contracts Version ^0.3.0
stratadox/specification-interfaces Version ^1.0
stratadox/deserializer-contracts Version ^0.3
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 stratadox/hydration-mapping contains the following files

Loading the files please wait ....