Documentation

Property
in package

Define hydration/dehydration rules for a property.

Table of Contents

__construct()  : mixed
Class constructor, source property required {@see Property::make()}.
allowDuplicates()  : $this
Allow duplicate keys. If false, a duplicate will throw an error.
as()  : $this
Use a different property name when hydrating.
assign()  : bool
Assign a value to this property.
bind()  : $this
Set the class to store this property into, optionally the method for doing so.
block()  : $this
Set the property to blocked. Blocked properties generate an error on hydration.
construct()  : $this
Set a class to be created via constructor.
encodeWith()  : $this
Set the rules for serializing this property to JSON.
factory()  : $this
Set a class to be created via constructor.
getBlocked()  : bool
Query if this property is blocked.
getClass()  : string|null
Get the name of the class this property is bound to. If the class is computed via a closure, null is returned.
getEncode()  : array<string|int, EncoderRule>
Get the current encoding rules.
getErrors()  : array<string|int, mixed>
Get errors generated by the last call to assign().
getHydrateMethod()  : string
Get the name of the hydration method for this property.
getIgnored()  : bool
Query if this property is ignored.
getOptions()  : array<string|int, mixed>
Get the current options.
getReflection()  : ReflectionProperty|null
Return any bound reflection property.
getRequired()  : bool
Query if this property is required.
getter()  : $this
Specify a method in the target class used to get the value of this property.
getValue()  : mixed
Get the value of this property from a source object.
hasReflection()  : bool
Check to see if this property is bound to a reflection property.
ignore()  : $this
Set ignore status on this property. Ignored properties are silently discarded.
key()  : $this
Set the method for computing an array index. Null to disable array mode.
make()  : Property
Fluent constructor.
makeAs()  : Property
Fluent constructor with target mapping.
reflects()  : $this
Set the property's reflection info.
require()  : $this
Set required status on this property. Required properties cause an exception if missing.
set()  : $this
Configure the property via a list of attributes.
setter()  : $this
Specify a method in the target class used to set the value of this property.
source()  : string
Get the name of the property in the source file.
target()  : string
Get the name of the property in the hydrated object. If the property is "synthetic", i.e. accessed via get/set methods, then the target name is prefixed with an asterisk.
toArray()  : $this
Control casting of the value of this property to an associative array before hydration.
unblock()  : $this
Set this property to not blocked.
validate()  : $this
Set a function that will be used to ensure the value of this property is valid before hydration.
with()  : $this
Callback that uses the property value to return a suitable object for hydration.

Methods

__construct()

Class constructor, source property required {@see Property::make()}.

public __construct(string $property[, class-string|null $binding = null ]) : mixed
Parameters
$property : string

The name of this property in the source data.

$binding : class-string|null = null

Optional name of a class to store the property value into.

Return values
mixed

allowDuplicates()

Allow duplicate keys. If false, a duplicate will throw an error.

public allowDuplicates([bool $allow = true ]) : $this
Parameters
$allow : bool = true
Return values
$this

as()

Use a different property name when hydrating.

public as(string $name) : $this
Parameters
$name : string

The property name in the class.

Return values
$this

assign()

Assign a value to this property.

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

The object being hydrated.

$value : mixed

Value of the property.

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

Options (passed to any objects hydrated by this property).

Tags
throws
HydrationException
Return values
bool

bind()

Set the class to store this property into, optionally the method for doing so.

public bind([class-string|object|null $binding = null ][, string $method = 'hydrate' ]) : $this
Parameters
$binding : class-string|object|null = null

A class name or an object of the class to be bound. If null, then the property is just a simple assignment.

$method : string = 'hydrate'

The name of the method to call when hydrating this property.

Return values
$this

block()

Set the property to blocked. Blocked properties generate an error on hydration.

public block([string|null $message = null ]) : $this

unblock()

Parameters
$message : string|null = null

A custom message to be returned as the error.

Return values
$this

construct()

Set a class to be created via constructor.

public construct(class-string $className[, bool $unpack = false ]) : $this
Parameters
$className : class-string

Name of the class to be created.

$unpack : bool = false

If the data to be passed is an array, unpack it to individual arguments.

Tags
deprecated

Use factory()

throws
HydrationException
Return values
$this

encodeWith()

Set the rules for serializing this property to JSON.

public encodeWith(string|array<string|int, mixed>|EncoderRule $rules) : $this
Parameters
$rules : string|array<string|int, mixed>|EncoderRule

If a string is provided then it should be one or more rules delimited by a vertical bar "rule1|rule2|..." (see EncoderRule::define()). If an array is provided then elements can either be individual rules or EncoderRule objects.

Tags
throws
HydrationException
Return values
$this

factory()

Set a class to be created via constructor.

public factory(Closure $fn) : $this
Parameters
$fn : Closure

Closure expected to create and hydrate a target object. Takes the property value and the Property definition as arguments.

Tags
throws
HydrationException
Return values
$this

getBlocked()

Query if this property is blocked.

public getBlocked() : bool
Return values
bool

getClass()

Get the name of the class this property is bound to. If the class is computed via a closure, null is returned.

public getClass() : string|null
Return values
string|null

getErrors()

Get errors generated by the last call to assign().

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

getHydrateMethod()

Get the name of the hydration method for this property.

public getHydrateMethod() : string
Return values
string

getIgnored()

Query if this property is ignored.

public getIgnored() : bool
Return values
bool

getOptions()

Get the current options.

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

getReflection()

Return any bound reflection property.

public getReflection() : ReflectionProperty|null
Return values
ReflectionProperty|null

getRequired()

Query if this property is required.

public getRequired() : bool
Return values
bool

getter()

Specify a method in the target class used to get the value of this property.

public getter(string $method[, bool $passProperty = true ]) : $this
Parameters
$method : string

A function ([Property $property]):mixed that returns the value of the property.

$passProperty : bool = true
Return values
$this

getValue()

Get the value of this property from a source object.

public getValue(object $source) : mixed
Parameters
$source : object
Tags
throws
HydrationException
Return values
mixed

hasReflection()

Check to see if this property is bound to a reflection property.

public hasReflection() : bool
Return values
bool

ignore()

Set ignore status on this property. Ignored properties are silently discarded.

public ignore([bool $ignore = true ]) : $this
Parameters
$ignore : bool = true
Return values
$this

key()

Set the method for computing an array index. Null to disable array mode.

public key([string|bool|Closure|null $key = true ]) : $this
Parameters
$key : string|bool|Closure|null = true
Tags
throws
HydrationException
Return values
$this

make()

Fluent constructor.

public static make(string $property[, class-string|null $binding = null ]) : Property
Parameters
$property : string

Name of the property.

$binding : class-string|null = null

Name of the class to create when hydrating the property.

Return values
Property

makeAs()

Fluent constructor with target mapping.

public static makeAs(string|array<string|int, string> $property) : Property
Parameters
$property : string|array<string|int, string>

If the property is a string, this method behaves like make(). If it is an array then the first element is the source property name, and the second is the property name in the object.

Tags
throws
HydrationException
Return values
Property

reflects()

Set the property's reflection info.

public reflects(ReflectionProperty|class-string|object $reflectProperty) : $this
Parameters
$reflectProperty : ReflectionProperty|class-string|object

The target class, an object of that class, or a ReflectionProperty.

Tags
throws
HydrationException
Return values
$this

require()

Set required status on this property. Required properties cause an exception if missing.

public require([bool $required = true ]) : $this
Parameters
$required : bool = true
Return values
$this

set()

Configure the property via a list of attributes.

public set(array<string|int, mixed> $options) : $this
Parameters
$options : array<string|int, mixed>

The index of each array element is a method of Property, the array value will be passed to the method as an argument. If the value is an array, then it will be unpacked and passed as a series of arguments.

Return values
$this

setter()

Specify a method in the target class used to set the value of this property.

public setter(string $method[, bool $passProperty = true ]) : $this
Parameters
$method : string

The method(mixed $value[, Property $property]):bool takes the proposed property value and Property as arguments, returns true on success.

$passProperty : bool = true
Return values
$this

source()

Get the name of the property in the source file.

public source() : string
Return values
string

target()

Get the name of the property in the hydrated object. If the property is "synthetic", i.e. accessed via get/set methods, then the target name is prefixed with an asterisk.

public target() : string
Return values
string

toArray()

Control casting of the value of this property to an associative array before hydration.

public toArray([bool $castToArray = true ]) : $this
Parameters
$castToArray : bool = true
Return values
$this

unblock()

Set this property to not blocked.

public unblock() : $this
Return values
$this

validate()

Set a function that will be used to ensure the value of this property is valid before hydration.

public validate(Closure $fn) : $this
Parameters
$fn : Closure

The validation function. Arguments are ($value, $this), the value to be validated and the current Property. If the function's return value evaluates to true if the value is considered to be valid.

Return values
$this

with()

Callback that uses the property value to return a suitable object for hydration.

public with(Closure $callback[, string $method = 'hydrate' ]) : $this
Parameters
$callback : Closure

A function (mixed $value, Property $property):string that takes the property value and Property as arguments and returns the appropriate class name.

$method : string = 'hydrate'

Unused, deprecated.

Return values
$this

Search results