Oihana PHP

compress.php

Table of Contents

Functions

compress()  : object
Compress the passed in object by removing all properties that match given conditions.

Functions

compress()

Compress the passed in object by removing all properties that match given conditions.

compress(object $object[, array{conditions: callable|callable[], depth: null|int, excludes: string[], recursive: bool, throwable: bool} $options = [] ][, int $currentDepth = 0 ]) : object
Parameters
$object : object

The object to compress.

$options : array{conditions: callable|callable[], depth: null|int, excludes: string[], recursive: bool, throwable: bool} = []

Optional configuration:

  • 'conditions' (callable|array) : One or more functions used to determine whether a value should be removed.
  • 'depth' (array) : List of property names to exclude from filtering.
  • 'excludes' (array) : List of property names to exclude from filtering.
  • 'recursive' (bool) : If true (default), recursively compress nested objects.
  • 'throwable' (bool) : If true (default), throws InvalidArgumentException for invalid callbacks.
$currentDepth : int = 0

Used internally to track recursion depth.

Tags
throws
InvalidArgumentException

If invalid condition callbacks are provided and 'throwable' is true.

example
use function oihana\core\objects\compress;

$obj = new stdClass();
$obj->id = 1;
$obj->created = null;
$obj->name = "hello world";
$obj->description = null;

$compressed = compress($obj, [
    'conditions' => [fn($v) => $v === null],
    'excludes' => ['id'],
    'recursive' => true,
]);
echo json_encode($compressed);
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
object

The compressed object, with properties removed according to the provided conditions.


        
On this page

Search results