Properties

In the simplest case, properties are transparent to the application. For more customization, Hydration offers a fluent interface to define how a property is handled.

To add all public properties to be available for hydration, all that's required is to name the class being hydrated (the host class):

Hydrate::make(self::class);

The second argument to make() allows the selection of properties based on the ReflectionProperty::IS_* constants:

Hydrate::make(
    self::class,
    ReflectionProperty::IS_PUBLIC || ReflectionProperty::IS_PROTECTED
);

The more common case will involve defining individual properties, in which case the class is bound after the property definitions:

Hydrate::make()
    ->addProperty(Property::make(...))
    ->addProperty(Property::make(...))
    ->addProperty(Property::make(...))
    ->bind(self::class, 0);

Passing zero to the second argument of bind() prevents the automatic addition of other properties.

Mapping Property Names

Class properties can have names that make sense in the development environment but are unfriendly to users. For example your application might have a property $clearTextPassword named so developers know to exercise caution about the use of that property, but you would like the configuration to simply use "password". The as() and makeAs()methods handle this:

Property::make('password')
    ->as('clearTextPassword');

Property::makeAs(['password', 'clearTextPassword']);

Required Properties

Hydration can ensure that your configurations contain all the required properties. The require() method causes Hydration to ensure that a value has been provided. A missing property will throw an exception.

require() takes a boolean argument, which defaults to true.

Hydrate::make()
    ->addProperty(
        Property::make('someInternalProperty')
            ->require()
    )
    ->bind(self::class);

Ignoring and Blocking Properties

While it's convenient to bulk add properties via bind(), the application might want to prevent users from setting some properties that get defined with a blanket bind. Hydration provides two methods for handling this. The ingore() method discards any property of that name, while the block() method will cause Hydration to throw an exception if it's provided in the source data.

ignore() takes a boolean argument, which defaults to true.

Hydrate::make()
    ->addProperty(
        Property::make('someInternalProperty')
            ->ignore()
    )
    ->bind(self::class);

block() takes a message string argument to be included in any error message. The unblock() method disables the block status.

Hydrate::make()
    ->addProperty(
        Property::make('someInternalProperty')
            ->block("someInternalProperty cannot be set via configuration.")
    )
    ->bind(self::class);