accessors
Table of Contents
Functions
- assertDocumentKeyValid() : bool
- Validates the key, separator, and type of the provided document before performing key-based operations.
- deleteKeyValue() : array<string|int, mixed>|object
- Deletes a value from an array or object using a dot-notated key path.
- getKeyValue() : mixed
- Retrieves a value from an array or object using a dot-notated key path.
- hasKeyValue() : bool
- Checks whether a given key or property exists in an array or object, including nested paths.
- resolveReferencePath() : mixed
- Navigates to the parent container of the last key segment in a nested structure.
- setKeyValue() : array<string|int, mixed>|object
- Sets a value in an array or object using a dot-notated key path.
Functions
assertDocumentKeyValid()
Validates the key, separator, and type of the provided document before performing key-based operations.
assertDocumentKeyValid(array<string|int, mixed>|object $document, string $key[, string $separator = '.' ][, bool|null &$isArray = null ]) : bool
This internal helper ensures that:
- The key is not empty.
- The separator is not empty.
- The document matches the expected type (
array
orobject
), or infers the type if$isArray
is null.
If a mismatch between the inferred or forced type and the actual document type occurs,
an InvalidArgumentException
is thrown.
Parameters
- $document : array<string|int, mixed>|object
-
The input document (either an array or an object).
- $key : string
-
The key or property name/path to validate.
- $separator : string = '.'
-
The separator for nested paths (default is '.').
- $isArray : bool|null = null
-
Optional reference:
true
for array,false
for object,null
to infer automatically.
Tags
Return values
bool —Returns the resolved value of $isArray
: true
for array, false
for object.
deleteKeyValue()
Deletes a value from an array or object using a dot-notated key path.
deleteKeyValue(array<string|int, mixed>|object $document, string $key[, string $separator = '.' ][, bool|null $isArray = null ]) : array<string|int, mixed>|object
This utility supports:
- Flat keys (e.g., "name")
- Nested keys (e.g., "user.profile.name")
- Wildcard deletion for sub-containers (e.g., "user.*")
- Global wildcard "*" to clear the entire document
The input can be an associative array or a stdClass-like object. Intermediate paths are ensured to exist before deletion.
Parameters
- $document : array<string|int, mixed>|object
-
The data source (array or object) to operate on.
- $key : string
-
The key path to delete (e.g. "foo.bar" or "foo." or "").
- $separator : string = '.'
-
The separator used to split the key path. Defaults to '.'.
- $isArray : bool|null = null
-
Optional: force array (
true
) or object (false
) mode; ifnull
, auto-detects.
Tags
Return values
array<string|int, mixed>|object —The updated document after deletion.
getKeyValue()
Retrieves a value from an array or object using a dot-notated key path.
getKeyValue(array<string|int, mixed>|object $document, string $key[, mixed $default = null ][, string $separator = '.' ][, bool|null $isArray = null ]) : mixed
This function returns the value associated with a flat or nested key from the given
array or object. It supports nested keys using a separator (default: '.').
If the path does not exist or a type mismatch occurs, the $default
value is returned.
The structure type can be explicitly specified using $isArray
, or it will be inferred automatically.
Parameters
- $document : array<string|int, mixed>|object
-
The source structure (array or object).
- $key : string
-
The key or property to retrieve, supports nesting (e.g. 'user.name').
- $default : mixed = null
-
The fallback value if the key does not exist. Default is
null
. - $separator : string = '.'
-
Separator used to split nested keys. Default is '.'.
- $isArray : bool|null = null
-
Optional: true for array mode, false for object mode, null for auto-detection.
Tags
Return values
mixed —The value found, or the default if the key path is not valid or not found.
hasKeyValue()
Checks whether a given key or property exists in an array or object, including nested paths.
hasKeyValue(array<string|int, mixed>|object $document, string $key[, string $separator = '.' ][, bool|null $isArray = null ]) : bool
This helper determines if the specified key exists in the given document (array or object). It supports nested access via a separator (default is '.') and can optionally force the document type (array or object).
If any part of the path does not exist, false
is returned. This function does not rely on
__get()
or __isset()
magic methods for objects.
Parameters
- $document : array<string|int, mixed>|object
-
The document (array or object) to inspect.
- $key : string
-
The key or property path to check. Supports nesting with separator.
- $separator : string = '.'
-
Separator used for nested paths. Default is '.'.
- $isArray : bool|null = null
-
Optional: true if document is an array, false if object, null to auto-detect.
Tags
Return values
bool —True if the full key path exists, false otherwise.
resolveReferencePath()
Navigates to the parent container of the last key segment in a nested structure.
& resolveReferencePath(array<string|int, mixed>|object &$document, array<string|int, mixed> $keys, bool $isArray) : mixed
This function traverses through nested arrays or objects based on the provided key path, ensuring that each intermediate level exists. It returns a reference to the parent container that should contain the final key segment.
It is typically used to prepare for read/write operations on nested values.
Requires external helpers:
ensureArrayPath()
(creates intermediate arrays)ensureObjectPath()
(creates intermediate objects)
Parameters
- $document : array<string|int, mixed>|object
-
The root document to navigate through (passed by reference).
- $keys : array<string|int, mixed>
-
The exploded path as array segments (e.g. ['user', 'profile', 'name']).
- $isArray : bool
-
If true, treat the structure as arrays; if false, as objects.
Tags
Return values
mixed —Reference to the container (array or object) that should hold the final key.
setKeyValue()
Sets a value in an array or object using a dot-notated key path.
setKeyValue(array<string|int, mixed>|object $document, string $key, mixed $value[, string $separator = '.' ][, bool|null $isArray = null ]) : array<string|int, mixed>|object
This function assigns a value to a key or property in the given array or object. It supports nested assignment using a separator (default: '.'). If any intermediate path segment does not exist, it is created automatically.
The type of the structure can be explicitly forced with $isArray
, or inferred automatically.
Parameters
- $document : array<string|int, mixed>|object
-
The target array or object to modify.
- $key : string
-
The key or property name to set. Supports nesting (e.g., "user.name").
- $value : mixed
-
The value to assign.
- $separator : string = '.'
-
Separator used to split nested keys. Default is '.'.
- $isArray : bool|null = null
-
Optional: true for array mode, false for object mode, null to auto-detect.
Tags
Return values
array<string|int, mixed>|object —The updated document after the value has been set.