Documentation

Hydrator
in package

Copy information from an object created from a JSON/YAML/etc. configuration file into a new object of the host class, performing validation and transformation operations while doing so.

Table of Contents

ALL_CALLABLE_METHODS  = ReflectionMethod::IS_PRIVATE | ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PUBLIC
Convenience constant for all methods that can be accessed.
ALL_NONSTATIC_PROPERTIES  = ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC
Convenience constant for all properties that can be accessed.
addProperties()  : Hydrator
Add a list of properties to be hydrated.
addProperty()  : Hydrator
Add a property to be hydrated.
bind()  : $this
Bind an object instance or class name.
checkBindings()  : void
Ensure that all properties are members of the same class.
decode()  : mixed
encode()  : mixed
Prepare an object for encoding.
fetchReflection()  : array<string|int, mixed>
Load and cache reflection information for the named class.
getErrors()  : array<string|int, string>
Get the error log.
getOptions()  : array<string|int, mixed>
Get the current option settings.
getSource()  : Property
Retrieve a Property by source name.
getSources()  : array<string|int, Property>
Retrieve the Property list indexed by source name.
getTarget()  : Property
Retrieve a Property by target name.
getTargets()  : array<string|int, Property>
Retrieve the Property list indexed by source name.
hasSource()  : bool
Check for a source property.
hasTarget()  : bool
Check for a target property.
hydrate()  : bool
Load configuration data into an object structure.
isHydratable()  : bool
See if a class implements Hydratable.
make()  : Hydrator
Create a Hydrator instance, optionally binding it to a subject class.
reflectionType()  : string|class-string|null
Get the type of the passed property.

Constants

ALL_CALLABLE_METHODS

Convenience constant for all methods that can be accessed.

public mixed ALL_CALLABLE_METHODS = ReflectionMethod::IS_PRIVATE | ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PUBLIC

ALL_NONSTATIC_PROPERTIES

Convenience constant for all properties that can be accessed.

public mixed ALL_NONSTATIC_PROPERTIES = ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC

Methods

addProperties()

Add a list of properties to be hydrated.

public addProperties(array<string|int, mixed> $properties[, array<string|int, mixed> $options = [] ]) : Hydrator
Parameters
$properties : array<string|int, mixed>

Elements are any of 'propertyName', ['sourceName', 'targetName'] or a Property object.

$options : array<string|int, mixed> = []

Common attributes to apply to the new properties. Options are any public method of the Property class, except __construct, as, assign, make, and reflects. Use an array to pass multiple arguments.

Tags
throws
HydrationException
Return values
Hydrator

bind()

Bind an object instance or class name.

public bind(class-string|object $subject[, int $filter = ReflectionProperty::IS_PUBLIC ]) : $this
Parameters
$subject : class-string|object

Name or instance of the class to bind the hydrator to.

$filter : int = ReflectionProperty::IS_PUBLIC

Filter for adding implicit properties. This is any combination of the ReflectionProperty IS_* flags that apply to non-static properties.

Tags
throws
ReflectionException
throws
HydrationException
Return values
$this

checkBindings()

Ensure that all properties are members of the same class.

public static checkBindings(array<string|int, Property$properties, class-string $toClass) : void
Parameters
$properties : array<string|int, Property>

A list of properties to check.

$toClass : class-string

The name of the class the properties should belong to.

Tags
throws
HydrationException
throws
ReflectionException
Return values
void

decode()

public decode(string|object $config, array<string|int, mixed> $options) : mixed
Parameters
$config : string|object
$options : array<string|int, mixed>
Tags
throws
HydrationException
Return values
mixed

encode()

Prepare an object for encoding.

public encode(object $source[, EncoderRule|array<string|int, mixed>|null $rules = [] ]) : mixed
Parameters
$source : object

An object to be prepared for encoding.

$rules : EncoderRule|array<string|int, mixed>|null = []

Encoding rules to be applied to the object.

Tags
throws
HydrationException
throws
ReflectionException
noinspection

PhpReturnDocTypeMismatchInspection

Return values
mixed

fetchReflection()

Load and cache reflection information for the named class.

public static fetchReflection(class-string $subjectClass) : array<string|int, mixed>
Parameters
$subjectClass : class-string

the name of the class we're loading.

Tags
throws
ReflectionException
Return values
array<string|int, mixed>

All relevant properties and methods of the class.

getErrors()

Get the error log.

public getErrors() : array<string|int, string>
Return values
array<string|int, string>

getOptions()

Get the current option settings.

public getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

getSource()

Retrieve a Property by source name.

public getSource(string $name) : Property
Parameters
$name : string

The name of the property in the source data.

Tags
throws
HydrationException

If the property is not defined.

Return values
Property

getSources()

Retrieve the Property list indexed by source name.

public getSources() : array<string|int, Property>
Return values
array<string|int, Property>

getTarget()

Retrieve a Property by target name.

public getTarget(string $name) : Property
Parameters
$name : string

The name of the property in the target object.

Tags
throws
HydrationException

If the property is not defined.

Return values
Property

getTargets()

Retrieve the Property list indexed by source name.

public getTargets() : array<string|int, Property>
Return values
array<string|int, Property>

hasSource()

Check for a source property.

public hasSource(string $name) : bool
Parameters
$name : string

The name of the property in the source data.

Return values
bool

hasTarget()

Check for a target property.

public hasTarget(string $name) : bool
Parameters
$name : string

The name of the property in the target object.

Return values
bool

hydrate()

Load configuration data into an object structure.

public hydrate(object $target, string|object|array<string|int, mixed> $config[, array<string|int, mixed> $options = [] ]) : bool
Parameters
$target : object

The object to be hydrated.

$config : string|object|array<string|int, mixed>

Configuration data either as a string or the result of decoding a configuration file.

$options : array<string|int, mixed> = []

Options. Hydratable::hydrate().

Tags
throws
HydrationException
Return values
bool

True if all fields passed validation; if in strict mode true when all fields are defined class properties.

isHydratable()

See if a class implements Hydratable.

public static isHydratable(class-string|null $forClass) : bool
Parameters
$forClass : class-string|null
Return values
bool

make()

Create a Hydrator instance, optionally binding it to a subject class.

public static make([object|class-string|null $subject = null ][, int $filter = ReflectionProperty::IS_PUBLIC ]) : Hydrator
Parameters
$subject : object|class-string|null = null

an instance or class name to bind to.

$filter : int = ReflectionProperty::IS_PUBLIC

Filter for property scopes to auto-bind, based on the ReflectionProperty IS_* constants. Defaults to ReflectionProperty::IS_PUBLIC.

Tags
throws
HydrationException
throws
ReflectionException
Return values
Hydrator

reflectionType()

Get the type of the passed property.

public static reflectionType(ReflectionProperty $reflectProperty) : string|class-string|null
Parameters
$reflectProperty : ReflectionProperty
Return values
string|class-string|null

Search results