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.
- ensureKeyValue() : array<string|int, mixed>|object
- Ensures that one or more keys or properties exist in an array or object.
- 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 (
arrayorobject), or infers the type if$isArrayis 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:
truefor array,falsefor object,nullto 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|array<string|int, mixed> $key[, string $separator = '.' ][, bool|null $isArray = null ][, bool $strict = false ]) : array<string|int, mixed>|object
This utility supports:
- Single key deletion (string)
- Multiple key deletion (array of strings)
- 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|array<string|int, mixed>
-
The key path(s) to delete (e.g. "foo.bar" or ["foo.bar", "baz.*"]).
- $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. - $strict : bool = false
-
If true, throws exception when key doesn't exist. Default: false.
Tags
Return values
array<string|int, mixed>|object —The updated document after deletion.
ensureKeyValue()
Ensures that one or more keys or properties exist in an array or object.
ensureKeyValue(array<string|int, mixed>|object $document, string|array<string|int, mixed> $keys[, mixed $default = null ][, string $separator = '.' ][, bool|null $isArray = null ][, bool $enforce = false ]) : array<string|int, mixed>|object
Missing keys are created and initialized. Defaults can be defined globally or per key. Nested keys are supported using a separator (default: '.'). Intermediate path segments are created automatically if missing.
Features:
- Specific Defaults: Pass an associative array to
$keysto set different defaults per key. - Lazy Loading: Pass a
Closureas a default value to calculate it only if needed. - Typed Properties: With
$enforce = true, strictly checks initialized state of typed properties.
Parameters
- $document : array<string|int, mixed>|object
-
The target document (array or object).
- $keys : string|array<string|int, mixed>
-
A single key, a list of keys
['a', 'b'], or an associative map['key' => 'default']. - $default : mixed = null
-
Global default value (or Closure) for keys without a specific default. 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 to auto-detect.
- $enforce : bool = false
-
Force initialization of non-initialized typed properties. Default: false.
Tags
Return values
array<string|int, mixed>|object —The updated document with ensured keys.
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), supporting nested keys using a separator (default is '.').
Unlike isset(), this function considers keys with null values as existing.
That means a key exists if it is present in the array/object, even if its value is null.
It can optionally force the document type (array or object) via the $isArray parameter.
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.