Oihana PHP

objects

Table of Contents

Functions

compress()  : object
Compress the passed in object by removing all properties that match given conditions.
ensureObjectPath()  : object
Ensures that a given property of an object is initialized as an object.
set()  : object
Sets a value in an object using a key path.
setObjectValue()  : object
Sets a value in a flat object using a single property name.

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.

ensureObjectPath()

Ensures that a given property of an object is initialized as an object.

& ensureObjectPath(object &$current, string $segment) : object

If the property does not exist or is not an object, it will be replaced with a new stdClass instance. The function returns a reference to the nested object, allowing direct modification.

This is useful when building or navigating nested object structures dynamically.

Parameters
$current : object

The current object in which the property is ensured.

$segment : string

The property name to ensure as an object.

Tags
example
$data = new stdClass();
$ref =& ensureObjectPath($data, 'config');
$ref->enabled = true;
// $data now contains: (object)['config' => (object)['enabled' => true]]
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
object

A reference to the ensured nested object (stdClass).

set()

Sets a value in an object using a key path.

set(object $object, string|null $key, mixed $value[, string $separator = '.' ][, bool $copy = false ][, array<string|int, mixed>|string|callable|null $classFactory = null ]) : object

Supports dot notation for nested properties. Intermediate objects are created if needed.

Parameters
$object : object

The object to modify (or copy).

$key : string|null

The key path to set (e.g. 'user.address.country'). If null, replaces entire object.

$value : mixed

The value to set.

$separator : string = '.'

The separator used in the key path. Default is '.'.

$copy : bool = false

If true, returns a deep copy of the object with the modification.

$classFactory : array<string|int, mixed>|string|callable|null = null

A class name, factory callable, or array path => className to create intermediate objects (default: stdClass).

Tags
example
  1. Basic usage with nested keys and default stdClass
$obj = new \stdClass();
$obj = set($obj, 'user.profile.name', 'Alice');
echo $obj->user->profile->name; // Alice
  1. Replace the entire object when key is null
$original = (object)['foo' => 'bar'];
$new = set($original, null, ['x' => 123]);
echo $new->x; // 123
  1. Use a custom class as intermediate objects
class Node { public string $label = ''; }
$obj = set(new Node(), 'tree.branch.leaf', 'green', '.', false, Node::class);
echo $obj->tree->branch->leaf; // green
  1. Use a factory callable
$factory = fn() => new class { public string $value = ''; };
$obj = set(new \stdClass(), 'x.y.z', 99, '.', false, $factory);
echo $obj->x->y->z; // 99
  1. Use an array of path => class mappings
class Address { public string $city = ''; }
class User    { public string $name = ''; }
$obj = new \stdClass();
$obj = set($obj, 'user.address.city', 'Paris', '.', false, [
'user' => User::class,
'user.address' => Address::class,
]);
echo $obj->user->address->city; // Paris
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
object

The modified (or copied and modified) object.

setObjectValue()

Sets a value in a flat object using a single property name.

setObjectValue(object $document, string $key, mixed $value) : object

This helper function assigns the given value to the specified property of the provided object. It does not support nested paths or separators.

The object is returned with the updated property.

Parameters
$document : object

The object to modify.

$key : string

The property name to set.

$value : mixed

The value to assign to the property.

Tags
example
$obj = (object)['name' => 'Alice'];
$updated = setObjectValue($obj, 'age', 30);
// $updated = (object)['name' => 'Alice', 'age' => 30];
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
object

The modified object with the new or updated property.


        
On this page

Search results