ArrayOption
Defines constants and normalization constants for array options.
These options are typically used in functions like prepare() to standardize array transformations before serialization or output.
Available options:
- BEFORE : Associative array of key/value pairs to inject before serialized properties.
- AFTER : Associative array of key/value pairs to append after serialized properties.
- DEFAULTS : Default values for missing or null properties.
- INCLUDE : Whitelist of property names to include.
- EXCLUDE : Blacklist of property names to exclude.
- FIRST_KEYS : Keys that must appear first in the output array (before sorting).
- SORT : Whether to sort remaining keys alphabetically (default: true).
- REDUCE : Controls value reduction using
compress()semantics (false/true/array).
Tags
Table of Contents
Constants
- AFTER : string = 'after'
- Keys/values to append **after** serialized properties.
- BEFORE : string = 'before'
- Keys/values to inject **before** serialized properties.
- DEFAULTS : string = 'defaults'
- Default values to apply for keys that are missing or null in the serialized output.
- EXCLUDE : string = 'exclude'
- Blacklist of property names to exclude from serialization.
- FIRST_KEYS : string = 'firstKeys'
- List of keys that must appear first in the resulting JSON object, in the given order, before alphabetical sorting is applied.
- INCLUDE : string = 'include'
- Whitelist of property names to include in serialization.
- REDUCE : string = 'reduce'
- Controls value reduction using `compress()` semantics.
- SORT : string = 'sort'
- Whether remaining keys should be sorted alphabetically (ksort).
Methods
- normalize() : array<string, mixed>
- Normalize the options.
Constants
AFTER
Keys/values to append **after** serialized properties.
public
string
AFTER
= 'after'
Typically used for metadata that should appear at the end of JSON output.
Type: array<string,mixed>
BEFORE
Keys/values to inject **before** serialized properties.
public
string
BEFORE
= 'before'
Typically used for metadatas. Type: array<string,mixed>
DEFAULTS
Default values to apply for keys that are missing or null in the serialized output.
public
string
DEFAULTS
= 'defaults'
Type: array<string,mixed>
Usage:
- Each key represents a property name.
- If the property is missing or its value is
null, the corresponding default value will be used.
Example:
$options =
[
ArrayOption::DEFAULTS =>
[
'stock' => 0,
'desc' => 'No description'
]
];
$data = $helper->toArray( $options ) ;
// 'stock' and 'desc' will get default values if they are null or missing
EXCLUDE
Blacklist of property names to exclude from serialization.
public
string
EXCLUDE
= 'exclude'
Type: string[]|null
FIRST_KEYS
List of keys that must appear first in the resulting JSON object, in the given order, before alphabetical sorting is applied.
public
string
FIRST_KEYS
= 'firstKeys'
Type: string[]
INCLUDE
Whitelist of property names to include in serialization.
public
string
INCLUDE
= 'include'
If set, only these properties are serialized.
Type: string[]|null
REDUCE
Controls value reduction using `compress()` semantics.
public
string
REDUCE
= 'reduce'
Can be:
- false : no reduction (default)
- true : use reduction
- array : options forwarded reduce with the compress() function.
Type: bool|array
SORT
Whether remaining keys should be sorted alphabetically (ksort).
public
string
SORT
= 'sort'
Type: bool
Methods
normalize()
Normalize the options.
public
static normalize([array<string, mixed>|null $options = [] ]) : array<string, mixed>
Fills in defaults for missing keys and ensures consistent option names.
Parameters
- $options : array<string, mixed>|null = []
-
User-provided options
Tags
Return values
array<string, mixed> —The options with default values.