PHP Classes

Yii2 Array Storage: Manage lists of values like associative arrays

Recommend this page to a friend!
  Info   View files Documentation   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-08-16 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 17 This week: 1All time: 11,045 This week: 102Up
Version License PHP version Categories
yii2-array-storage 1.0MIT/X Consortium ...8Data types, PHP 8
Description 

Author

Smoren


Contributor

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
Nominee
Vote
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: 29 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 14x

Details

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  
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  /  src  
File Role Description
  Plain text file Storage.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:17
This week:1
All time:11,045
This week:102Up