Oihana PHP

CompressOption

Defines constants for the options used by the `compress` functions for arrays and objects.

Using these constants helps avoid typos when passing options to oihana\core\arrays\compress or oihana\core\objects\compress.

Each constant corresponds to a specific configurable option:

  • CLONE : Whether to clone the input before compression.
  • CONDITIONS : One or more callable conditions to remove values.
  • EXCLUDES : Keys to skip during compression.
  • DEPTH : Maximum recursion depth for nested arrays/objects.
  • RECURSIVE : Whether to compress nested arrays/objects recursively.
  • REMOVE_KEYS : Keys to forcibly remove regardless of conditions.
  • THROWABLE : Whether invalid conditions throw exceptions.
Tags
author

Marc Alcaraz

since
1.0.0
example

Using constants with compress

use oihana\core\options\CompressOption;
use function oihana\core\arrays\compress;

$data =
[
  'id'    => 1,
  'name'  => null,
  'debug' => 'keep me'
];

$clean = compress( $data ,
[
   CompressOption::EXCLUDES    => ['debug'] , // don't remove 'debug'
   CompressOption::REMOVE_KEYS => ['id'] ,    // forcibly remove 'id'
   CompressOption::RECURSIVE   => true,
   CompressOption::THROWABLE   => true
]);

Table of Contents

Constants

CLONE  = 'clone'
Option key to clone the array/object before compressing.
CONDITIONS  = 'conditions'
One or more callbacks: fn(mixed $value): bool.
DEPTH  = 'depth'
Option key to limit recursion depth when compressing nested arrays/objects.
EXCLUDES  = 'excludes'
Option key for property/array keys to exclude from compression.
RECURSIVE  = 'recursive'
Option key to enable recursive compression of nested arrays/objects.
REMOVE_KEYS  = 'removeKeys'
Option key for property/array keys to forcibly remove.
THROWABLE  = 'throwable'
Option key to control whether invalid conditions throw exceptions.

Methods

normalize()  : array<string|int, mixed>
Normalize an options array for compress functions.

Constants

CLONE

Option key to clone the array/object before compressing.

public mixed CLONE = 'clone'

If true, the original is not modified.

Type: bool

CONDITIONS

One or more callbacks: fn(mixed $value): bool.

public mixed CONDITIONS = 'conditions'

If any returns true, the entry is removed.

Type: callable|callable[]

DEPTH

Option key to limit recursion depth when compressing nested arrays/objects.

public mixed DEPTH = 'depth'

Null means no limit.

Type: int|null

EXCLUDES

Option key for property/array keys to exclude from compression.

public mixed EXCLUDES = 'excludes'

Keys listed here will not be removed even if conditions match.

Type: string[]

RECURSIVE

Option key to enable recursive compression of nested arrays/objects.

public mixed RECURSIVE = 'recursive'

Type: bool

REMOVE_KEYS

Option key for property/array keys to forcibly remove.

public mixed REMOVE_KEYS = 'removeKeys'

Keys listed here are removed regardless of conditions.

Type: string[]

THROWABLE

Option key to control whether invalid conditions throw exceptions.

public mixed THROWABLE = 'throwable'

Type: bool

Methods

normalize()

Normalize an options array for compress functions.

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 = CompressOption::normalize([
    CompressOption::EXCLUDES => ['id'],
    CompressOption::RECURSIVE => true
]);

// Result:
// [
//   'clone' => false,
//   'excludes' => ['id'],
//   'depth' => null,
//   'recursive' => true,
//   'removeKeys' => null,
//   'throwable' => true
// ]
Return values
array<string|int, mixed>

Normalized options with default values


        
On this page

Search results