Oihana PHP System

EnsureKeysTrait

Provides functionality to guarantee the existence of specific keys or properties within document structures or collections.

This trait processes the configuration provided in ModelParam::ENSURE to automatically populate missing keys with default values.

Usage example:

class MyModel
{
   use EnsureKeysTrait;

   public function fetchAndProcess($init)
   {
       $data = ['id' => 1];

       // Ensures 'status' exists, defaults to 'draft'
       $this->ensureDocumentKeys($data, $init);

       return $data;
    }
}

// Usage
$model->fetchAndProcess
([
    ModelParam::ENSURE  =>
    [
        ModelParam::KEYS    => ['status'],
        ModelParam::DEFAULT => 'draft'
    ]
]);
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

ensureDocumentKeys()  : void
Ensures that specific attributes (keys or properties) exist on a document or a collection of documents.

Methods

ensureDocumentKeys()

Ensures that specific attributes (keys or properties) exist on a document or a collection of documents.

protected ensureDocumentKeys(mixed &$data[, array<string|int, mixed> $init = [] ]) : void

It parses the configuration from $init[ModelParam::ENSURE] and applies it to $data. It automatically detects if $data is a collection (indexed array) or a single item.

Parameters
$data : mixed

Reference to the document(s). Modified in place.

$init : array<string|int, mixed> = []

The initialization options containing ModelParam::ENSURE.


        
On this page

Search results