Documentation

EncoderRule
in package

Define a rule for encoding/dehydrating a property.

Table of Contents

applyTransform()  : void
Apply a transformation to a property.
arg()  : mixed|null
Get the value of the argument in the specified slot, or null if the slot is not defined.
command()  : string
define()  : EncoderRule
Define the rule and its arguments.
emit()  : bool
See if the property should be included in the result, optionally transforming it.
make()  : EncoderRule
Fluent factory.

Methods

applyTransform()

Apply a transformation to a property.

public applyTransform(mixed &$value, object|null $source, Property|null $property) : void
Parameters
$value : mixed

The current/transformed property value. Passed by reference.

$source : object|null

The object being encoded.

$property : Property|null

The property being transformed.

Tags
throws
HydrationException
throws
ReflectionException
Return values
void

arg()

Get the value of the argument in the specified slot, or null if the slot is not defined.

public arg(int $slot) : mixed|null
Parameters
$slot : int
Return values
mixed|null

command()

public command() : string
Return values
string

define()

Define the rule and its arguments.

public define(string $command, array<string|int, mixed> ...$args) : EncoderRule
Parameters
$command : string

A simple command verb or a rule definition of the form "command:arg1:arg2:...". The available commands are:

  • array The result is cast as an array.
  • drop:condition Conditions are blank, empty[:method], false, null, true, zero. the property is dropped from the output if its value matches the condition. For the "empty" test, the method defaults to isEmpty.
  • order:<number> Sets the order of appearance. Number is a float.
  • scalar Will cast a single-element array as a stand-alone element. That element can be an object or array, so "scalar" is a bit of a misnomer.
  • transform:method Applies the named method to the value. the method can be a string or a Callable. If the method is a string, it is called on the object being encoded. The transform is called with the arguments (mixed $value, object $source, Property $property). It is expected that the value is passed by reference.
$args : array<string|int, mixed>

If the $command is a command verb, these are the arguments for that command.

Tags
throws
HydrationException

If the command is invalid or poorly structured.

Return values
EncoderRule

emit()

See if the property should be included in the result, optionally transforming it.

public emit(scalar|object|array<string|int, mixed> $value) : bool
Parameters
$value : scalar|object|array<string|int, mixed>

The value of the property.

Return values
bool

Returns true if the value is part of the serialization.

Search results