objects
Table of Contents
Functions
- compress() : object
- Compress the given object by removing properties that match certain conditions.
- ensureObjectPath() : object
- Ensures that a given property of an object is initialized as an object.
- hasAllProperties() : bool
- Check if all of the given properties exist in the object.
- hasAnyProperty() : bool
- Check if at least one of the given properties exists in the 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.
- toAssociativeArray() : array<string|int, mixed>
- Recursively converts an object (or array) into a full associative array.
Functions
compress()
Compress the given object by removing properties that match certain conditions.
compress(object $object[, array{conditions?: callable|callable[], depth?: null|int, excludes?: string[], recursive?: bool, removeKeys?: string[], throwable?: bool} $options = [] ][, int $currentDepth = 0 ]) : object
This function traverses the object and removes properties according to the provided options. It can operate recursively on nested objects and arrays.
Parameters
- $object : object
-
The object to compress.
- $options : array{conditions?: callable|callable[], depth?: null|int, excludes?: string[], recursive?: bool, removeKeys?: string[], throwable?: bool} = []
-
Optional configuration.
- $currentDepth : int = 0
-
Internal counter used to track recursion depth.
Tags
Return values
object —The compressed object, with properties removed according to the rules.
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
Return values
object —A reference to the ensured nested object (stdClass).
hasAllProperties()
Check if all of the given properties exist in the object.
hasAllProperties(object $object, array<string|int, mixed> $properties[, bool $notNull = false ]) : bool
If $notNull is set to true, each property must also be non-null.
Parameters
- $object : object
-
The object to inspect
- $properties : array<string|int, mixed>
-
List of property names to check
- $notNull : bool = false
-
Whether to check for non-null values (default: false)
Tags
Return values
bool —True if all properties exist (and are not null if $notNull = true)
hasAnyProperty()
Check if at least one of the given properties exists in the object.
hasAnyProperty(object $object, array<string|int, mixed> $properties[, bool $notNull = false ]) : bool
If $notNull is set to true, the property must also be non-null.
Parameters
- $object : object
-
The object to inspect
- $properties : array<string|int, mixed>
-
List of property names to check
- $notNull : bool = false
-
Whether to check for non-null values (default: false)
Tags
Return values
bool —True if at least one property exists (and is not null if $notNull = true)
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
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
Return values
object —The modified object with the new or updated property.
toAssociativeArray()
Recursively converts an object (or array) into a full associative array.
toAssociativeArray(array<string|int, mixed>|object $document) : array<string|int, mixed>
This function handles nested objects, ensuring the entire array or object tree is converted.
Note that only public properties of the object will be included in the resulting array.
Parameters
- $document : array<string|int, mixed>|object
-
An array or object to convert to a deep associative array .
Tags
Return values
array<string|int, mixed> —The resulting associative array.