jnessier/data-handler

Data container for arrays and more.

0.5.0 2023-09-05 20:07 UTC

This package is auto-updated.

Last update: 2024-04-05 21:43:14 UTC


README

Build Status Latest Stable Version Total Downloads License

Data handler for PHP arrays.

Table of Contents

Requirement

  • PHP >= 8.1

Installation

Install package of the data handler via Composer...

composer require jnessier/data-handler

...or manually download the latest release from here.

Usage

use DataHandler\Data;

// Create new data handler.
$data = new Data([
    // Array with key/value pairs
]);

// Get value by key.
$default = null; // Default value, when key doesn't exists
$value = $data->get('key', $default);
   
// Pull value by key and delete it afterward.
$default = null; // Default value, when key doesn't exists
$value = $data->pull('key', $default);

// Set value by key.
$overwrite = true; // Set FALSE to prevent overwrite existing value
$data = $data->set('key', 'value', $overwrite);

// Check whether value exists by key.
$valueExists = $data->has('key');
   
// Delete value by key.
$data->delete('key');

// Count number of values.
$numberOfValues = $data->count();

// Get all values as array.
$array = $data->getAll();

// Iterate trough values.
$data->each(function ($value, string $key) {
    // Callback for each key/value pair
});

// Clear values.
$data = $data->clear();

// Replace values. Existing values with similar keys will be overwritten.
$recursive = true; // Set FALSE to prevent recursive merge
$data = $data->replace([
    // Array with key/value pairs
], $recursive);

// Set array as values. Existing data will be overwritten.
$data = $data->setAll([
    // Array with key/value pairs
]);

// Set referenced array as values. Existing data will be overwritten.
$values = [
    // Array with key/value pairs
];
$data = $data->setAllReferenced($values);

Limitations

  • Only string as key supported
  • Only array as key/value pairs supported
  • No dot notation implementation
  • No type check for values

Contributors

  • Jonathan Nessier (jnessier)

If you would like to see this library develop further, or if you want to support me or show me your appreciation, please donate any amount through PayPal. Thank you! 🍻

Donate

License

Licensed under MIT.

Made in Switzerland with 🧀 and ❤️