PHP Classes

Yii2 Array Storage: Manage lists of values like associative arrays

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 35 All time: 11,005 This week: 82Up
Version License PHP version Categories
yii2-array-storage 1.0MIT/X Consortium ...8Data types, PHP 8
Description 

Author

This class can manage lists of values like associative arrays.

It provides several functions to change the list of values that the class stores.

Currently, the class can:

- Initialize the list of values with an array

- Check if the list has a given key

- Get the value of a key and return a default if the key is missing

- Set the value of a key with the option to throw an exception if there is a key with the same value

- Remove a value with a given key

- Remove all values of the list

Innovation Award
PHP Programming Innovation award nominee
August 2023
Number 6
Arrays are a frequently used data type that PHP developers use in their code.

Often associative arrays are used to store lists of values.

This package provides an alternative class that implements lists of values that can assume default values when retrieving list values that are not set.

It can also prevent setting a list value that is already set.

These features allow to implement more restricted lists of values in PHP.

Manuel Lemos
Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 38 packages by
Country: Russian Federation Russian Federation
Age: 35
All time rank: 280778 in Russian Federation Russian Federation
Week rank: 12 Up1 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 16x

Documentation

yii2-array-storage

????? ??? ???????? ? ?????????? ??????? ? ???????. ????? ???? ???????? ?? ????????, ????????, ? ???????? ??????? ??? ?????? ? ?????, ???????? _json_ ? ?????? _ActiveRecord_.

????????? ? ?????? ?? Yii2

composer require smoren/yii2-array-storage

??????? ?????????????

<?php

use Smoren\Yii2\ArrayStorage\Storage;

// ???????? ??????
$data = [
    'a' => [
        'b1' => [1, 2, 3],
        'b2' => 5,
    ]
];

// ????????????? ?????????
$storage = new Storage($data);

// ????????? ????? ??????? ?????? ?????????
$value = $storage->get();
print_r($value);
/*
Array
(
    [a] => Array
        (
            [b1] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )
            [b2] => 5
        )
)
*/

// ????????? ???????? ?? ?????
$value = $storage->get('a');
print_r($value);
/*
Array
(
    [b1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
    [b2] => 5
)
*/

// ????????? ???????? ?? ????? ? ???????? ???????????
$value = $storage->get('a.b1.0');
var_dump($value);
/int(1)/

// ??????? ????????? ???????? ?? ?????????????? ? ????????? ????? ?? ????????? ?? ?????????
$value = $storage->get('a.b3', '???????? ?? ?????????');
var_dump($value);
/string(40) "???????? ?? ?????????"/

// ??????? ????????? ???????? ?? ?????????????? ? ????????? ????? ??? ???????? ?? ?????????
try {
    $storage->get('a.b3');
} catch(\yii\base\Exception $e) {
    var_dump($e->getMessage());
    /string(39) "key 'a.b3' is not exist in user storage"/
}

// ???????? ????????????? ????? ? ???????
var_dump($storage->has('a.b2'));
/bool(true)/
var_dump($storage->has('a.b3'));
/bool(false)/
try {
    $storage->has('a.b3', true);
} catch(\yii\base\Exception $e) {
    var_dump($e->getMessage());
    /string(39) "key 'a.b3' is not exist in user storage"/
}

// ?????????? ???????? ? ?????????
$storage->set('a.new', '???????? ?????? ????????');
print_r($storage->get());
/*
Array
(
    [a] => Array
        (
            [b1] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )
            [b2] => 5
            [new] => ???????? ?????? ????????
        )
)
*/

// ?????????? ???????? ? ????????? ? ???????? ??????????
$storage->set('a.new_another', '??? ???? ???????? ?????? ????????', false);
print_r($storage->get());
/*
Array
(
    [a] => Array
        (
            [b1] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )
            [b2] => 5
            [new] => ???????? ?????? ????????
            [new_another] => ??? ???? ???????? ?????? ????????
        )
)
*/

// ??????? ?????????? ???????? ? ???????? ??????????
try {
    $storage->set('a.b2', '??? ???????? ?? ?????????', false);
} catch(\yii\base\Exception $e) {
    var_dump($e->getMessage());
    /string(43) "key 'a.b2' is already exist in user storage"/
}
print_r($storage->get());
/*
Array
(
    [a] => Array
        (
            [b1] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )
            [b2] => 5
            [new] => ???????? ?????? ????????
            [new_another] => ??? ???? ???????? ?????? ????????
        )
)
*/

// ???????? ???????? ?? ????????? ?? ?????, ????????? ???????? ?????????? ????????
$removedValue = $storage->remove('a.b1');
print_r($removedValue);
/*
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
*/
print_r($storage->get());
/*
Array
(
    [a] => Array
        (
            [b2] => 5
            [new] => ???????? ?????? ????????
            [new_another] => ??? ???? ???????? ?????? ????????
        )
)
*/

// ??????? ???????? ??????????????? ????????
try {
    $storage->remove('a.?');
} catch(\yii\base\Exception $e) {
    var_dump($e->getMessage());
    /string(39) "key 'a.?' is not exist in user storage"/
}

  Files folder image Files (5)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (5)  /  src  
File Role Description
  Plain text file Storage.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:35
This week:0
All time:11,005
This week:82Up