Downloadyii2-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"/
}
|