Oihana PHP

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
author

Marc Alcaraz

since
1.0.8

Table of Contents

Constants

AFTER  = 'after'
Keys/values to append **after** serialized properties.
BEFORE  = 'before'
Keys/values to inject **before** serialized properties.
DEFAULTS  = 'defaults'
Default values to apply for keys that are missing or null in the serialized output.
EXCLUDE  = 'exclude'
Blacklist of property names to exclude from serialization.
FIRST_KEYS  = 'firstKeys'
List of keys that must appear first in the resulting JSON object, in the given order, before alphabetical sorting is applied.
INCLUDE  = 'include'
Whitelist of property names to include in serialization.
REDUCE  = 'reduce'
Controls value reduction using `compress()` semantics.
SORT  = 'sort'
Whether remaining keys should be sorted alphabetically (ksort).

Methods

normalize()  : array<string|int, mixed>
Normalize the options.

Constants

AFTER

Keys/values to append **after** serialized properties.

public mixed 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 mixed 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 mixed 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 mixed 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 mixed FIRST_KEYS = 'firstKeys'

Type: string[]

INCLUDE

Whitelist of property names to include in serialization.

public mixed INCLUDE = 'include'

If set, only these properties are serialized.

Type: string[]|null

REDUCE

Controls value reduction using `compress()` semantics.

public mixed 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 mixed SORT = 'sort'

Type: bool

Methods

normalize()

Normalize the options.

public static normalize([array<string|int, mixed>|null $options = [] ]) : array<string|int, mixed>

Fills in defaults for missing keys and ensures consistent option names.

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

User-provided options

Tags
example
$opts = ArrayOption::normalize
([
     ArrayOption::BEFORE => [ '_type' => 'Thing' ],
     ArrayOption::REDUCE => true
]);

// Result:
// [
//   'after'     => [],
//   'before'    => ['_type' => 'Thing'],
//   'exclude'   => null,
//   'firstKeys' => [],
//   'sort'      => true,
//   'reduce'    => true,
//   'include'   => null
// ]
Return values
array<string|int, mixed>

The options with default values.


        
On this page

Search results