PHP code example of setnemo / asterisk-notation
1. Go to this page and download the library: Download setnemo/asterisk-notation library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
setnemo / asterisk-notation example snippets
use Setnemo\Asterisk;
$items = new Asterisk([
'Europe' => [
'Ukraine' => [
'capital' => 'Kyiv',
'currency' => 'UAH'
],
'Poland' => [
'capital' => 'Warsaw',
'currency' => 'PLN'
],
],
'Africa' => [
'South Africa' => [
'capital' => 'Capetown',
'currency' => 'ZAR'
],
'Nigeria' => [
'capital' => 'Abuja',
'currency' => 'NGN'
],
],
]);
$city = 'Kyiv';
$resultTrue = $items->has('*.*.capital', $city);
$resultFalse = $items->has('Africa.*.capital', $city);
if ($resultTrue === true && $resultFalse === false) {
echo "Wow! It works correctly!";
}
use Adbar\Dot;
use Setnemo\Asterisk;
$asterisk = new Asterisk;
$array = ['first_one' => ['second' => 'value'], 'first_two' => ['second' => 'value'],];
// With existing array
$asterisk = new Asterisk($array);
// With existing \Adbar\Dot
$dot = new Dot($array);
$asterisk = new Asterisk($dot);
// or existing Asterisk
$newAsterisk = new Asterisk($asterisk);
$asterisk->clear('department.*.write_rules');
// Equivalent vanilla PHP
$array['department']['project1']['write_rules'] = [];
$array['department']['project2']['write_rules'] = [];
$array['department']['projectN']['write_rules'] = [];
$asterisk->clear(['department.*.write_rules', 'department.*.read_rules']);
$asterisk->clear();
// Equivalent vanilla PHP
$array = [];
$asterisk->count('user.siblings');
$asterisk->count();
// Or use count() function as Dot implements Countable
count($asterisk);
$asterisk->delete('*.name');
// ArrayAccess
unset($asterisk['user1.name']);
unset($asterisk['user2.name']);
// Equivalent vanilla PHP
unset($array['user1']['name']);
unset($array['user2']['name']);
$asterisk->delete([
'*.name',
'*.title'
]);
$result = $asterisk->get('*.name'));
// ArrayAccess
$result['user1.name'] = $asterisk['user1.name'];
$result['user2.name'] = $asterisk['user2.name'];
// Equivalent vanilla PHP
if (isset($array['user1']['name']) {
$result['user1.name'] = $array['user1']['name'];
}
if (isset($array['user1']['name']) {
$result['user2.name'] = $array['user2']['name'];
}
echo $asterisk->get('user.name', 'some default value');
$asterisk->has('user.name');
// ArrayAccess
isset($asterisk['user.name']);
$asterisk->has([
'user.name',
'page.title'
]);
$asterisk = new \Setnemo\Asterisk(['1' => ['second' => 'value'],'2' => ['second' => 'value']]);
$asterisk->has('*.second'); // true
$asterisk = new \Setnemo\Asterisk(['1' => ['first' => ['test' => 42]],'2' => ['second' => 'value'],'3' => ['third' => ['value' => 42]]]);
$asterisk->has('*.*.value'); // true
$asterisk = new \Setnemo\Asterisk(['user1' => ['name' => 'John', 'job' => 'warrior'], 'user2' => ['name' => 'Robin', 'job' => 'archer']);
$asterisk->has('*.spouse'); // false
$asterisk = new \Setnemo\Asterisk([]);
$asterisk->has('*', false); // false
$asterisk = new \Setnemo\Asterisk(['*' => ['second' => 'VALUE']]);
$asterisk->has('*.second', 'VALUE'); // true
$asterisk->has('*.second', 'value'); // false because lowercase
$asterisk = new \Setnemo\Asterisk(['*' => ['second' => 'value'], 0 => [0 => 0], 11 => 11]);
$asterisk->has('*.11', 11); // true
$asterisk = new \Setnemo\Asterisk(['1' => ['second' => 'value'],'2' => ['second' => '-']]);
$asterisk->has('*.second', 'value'); // false because 'second' => '-'
$asterisk = new \Setnemo\Asterisk(['1' => ['second' => 'value'],'2' => ['second' => 'value']]);
$asterisk->has('*.second', 'value'); // true
$asterisk = new \Setnemo\Asterisk([
'locations' => [
'Europe' => [
'Ukraine' => [
'capital' => 'Kyiv',
'currency' => 'UAH'
],
'Poland' => [
'capital' => 'Warsaw',
'currency' => 'PLN'
],
],
'Africa' => [
'South Africa' => [
'capital' => 'Capetown',
'currency' => 'ZAR'
],
'Nigeria' => [
'capital' => 'Abuja',
'currency' => 'NGN'
],
],
]]);
// third parameter is false for non-strict mode
$asterisk->has('locations.*.*.capital', 'Kyiv', false); // true
$asterisk->has('locations.*.*.currency', 'ZAR', false); // true
$asterisk->has('locations.Europe.*.currency', 'ZAR', false); // false
$asterisk->isEmpty('user.name');
// ArrayAccess
empty($asterisk['user.name']);
// Equivalent vanilla PHP
empty($array['user']['name']);
$asterisk->isEmpty([
'user.name',
'page.title'
]);
$asterisk->isEmpty();
$asterisk = new \Setnemo\Asterisk(['user1' => ['name' => 'John'], 'user2' => ['name' => 'Robin']);
$asterisk->isEmpty('*.name'); // false
$asterisk->isEmpty('*.spouse'); // true
echo $asterisk->pull('user.name');
// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;
unset($array['user']['name']);
// Equivalent vanilla PHP >= 7.0
echo $array['user']['name'] ?? null;
unset($array['user']['name']);
echo $asterisk->pull('user.name', 'some default value');
$items = $asterisk->pull();
$asterisk = new \Setnemo\Asterisk([['user1' => ['name' => 'John', 'job' => 'warrior'], 'user2' => ['name' => 'Robin', 'job' => 'archer'],]]);
$result = $asterisk->pull('*.name'); // ['user1.name' => 'John', 'user2.name' => 'Robin']
$all = $asterisk->all(); // ['user1' => ['job' => 'warrior'], 'user2' => ['job' => 'archer'],]
$asterisk->push('users', 'John');
// Equivalent vanilla PHP
$array['users'][] = 'John';
$asterisk->push('John');
// Equivalent vanilla PHP
$array[] = 'John';
$asterisk = new \Setnemo\Asterisk([
'first_one' => ['second' => 'value'],
'first_two' => ['second' => 'value']
]);
$asterisk->push('*.second', 'John');
$asterisk->all();
/*
[
'first_one' => ['second' => ['value','VALUE']],
'first_two' => ['second' => ['value','VALUE']]
]
*/
$asterisk->set('user.name', 'John');
// ArrayAccess
$asterisk['user.name'] = 'John';
// Equivalent vanilla PHP
$array['user']['name'] = 'John';
$asterisk->set([
'user.name' => 'John',
'page.title' => 'Home'
]);