Oihana PHP

ensureKeyValue.php

Table of Contents

Functions

ensureKeyValue()  : array<string|int, mixed>|object
Ensures that one or more keys or properties exist in an array or object.

Functions

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 $keys to set different defaults per key.
  • Lazy Loading: Pass a Closure as 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
throws
InvalidArgumentException

If the document or key definition is invalid.

example

Ensure simple keys with a global default :

$doc = [];
$doc = ensureKeyValue($doc, ['name', 'age'], null);
// Result: ['name' => null, 'age' => null]

Ensure keys with specific default values :

$doc = [];
$doc = ensureKeyValue($doc, [
'role' => 'guest',   // Specific default
'active'             // Uses global default (true)
], true);
// Result: ['role' => 'guest', 'active' => true]

Use a Closure for lazy evaluation (heavy calculation) :

$doc = [];
$doc = ensureKeyValue($doc, 'created_at', fn() => new DateTimeImmutable());
// Result: ['created_at' => object(DateTimeImmutable)]

Enforce initialization of typed properties (PHP 7.4+) :

class User { public string $name; } // Uninitialized
$doc = new User();

// Without enforce: Property exists, so it's skipped (remains uninitialized).
// With enforce: Detects uninitialized state and sets default.
$doc = ensureKeyValue($doc, 'name', 'Anonymous', '.', null, true);

// Result: User object with $name = 'Anonymous'

Use with named arguments (PHP 8+) to skip optional parameters:

$doc = new User();
// We skip separator and isArray to target 'enforce' directly
ensureKeyValue($doc, 'name', default: 'John', enforce: true);
author

Marc Alcaraz (ekameleon)

since
1.0.8
Return values
array<string|int, mixed>|object

The updated document with ensured keys.


        
On this page

Search results