PHP Classes

Nested PHP Accessor: Get and set values of nested arrays or objects

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 yet rated by the usersTotal: 58 All time: 10,535 This week: 74Up
Version License PHP version Categories
nested-accessor-php 1.0MIT/X Consortium ...7.4Algorithms, Libraries, PHP 7, PSR
Description 

Author

This package can get and set values of nested arrays or objects.

It can take an object or an array as a parameter to access the values of its elements.

The package provides accessor functions that take a string parameter that defines the path of the array or object element you want to access.

The path string uses the dot notation that separates the names of the elements and parent elements.

Currently, the package provides accessor functions to:

- Get the value of an element

- Set the value of an element

- Check if there is an element in a position with the given path

- Check if an element is set to a non-null value

- Append new values to the end of a given element

- Delete elements with the given path

Innovation Award
PHP Programming Innovation award nominee
April 2023
Number 3
Dot notation is a convention to define strings that can specify the position of a value that makes part of a nested array or object.

Using the dot notation can simplify the way you can access the array or objects using strings instead of the usual syntax that PHP provides to access element values that may exist in deep locations of the

This package provides classes with functions that can access array or object elements using dot notation strings to specify the elements' path.

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: 280978 in Russian Federation Russian Federation
Week rank: 47 Up6 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 16x

Documentation

nested-accessor

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Accessor for getting and setting values of nested data structures (arrays or objects).

How to install to your project

composer require smoren/nested-accessor

Unit testing

composer install
composer test-init
composer test

Usage

NestedAccessor


use Smoren\NestedAccessor\Components\NestedAccessor;

$source = [
    'data' => [
        'id' => 1,
        'name' => 'Countries classifier',
        'extra' => null,
        'country_names' => ['Russia', 'Belarus'],
    ],
    'countries' => [
        [
            'name' => 'Russia',
            'cities' => [
                [
                    'name' => 'Moscow',
                    'extra' => [
                        'codes' => [
                            ['value' => 7495],
                            ['value' => 7499],
                        ],
                    ],
                ],
                [
                    'name' => 'Petersburg',
                    'extra' => [
                        'codes' => [
                            ['value' => 7812],
                        ],
                    ],
                ],
            ],
        ],
        [
            'name' => 'Belarus',
            'cities' => [
                [
                    'name' => 'Minsk',
                    'extra' => [
                        'codes' => [
                            ['value' => 375017],
                        ],
                    ],
                ],
            ],
        ],
    ]
];

$accessor = new NestedAccessor($input);

echo $accessor->get('data.name'); // 'Countries classifier'
print_r($accessor->get('countries.name')); // ['Russia', 'Belarus']
print_r($accessor->get('countries.cities.name')); // ['Moscow', 'Petersburg', 'Minsk']
print_r($accessor->get('countries.cities.extra.codes.value')); // [7495, 7499, 7812, 375017]

var_dump($accessor->isset('data.name')); // true
var_dump($accessor->isset('data.extra')); // false
var_dump($accessor->isset('this.path.not.exist')); // false

var_dump($accessor->exist('data.name')); // true
var_dump($accessor->exist('data.extra')); // true
var_dump($accessor->exist('this.path.not.exist')); // false

$accessor->set('data.name', 'New name');
echo $accessor->get('data.name'); // 'New name'

$accessor->append('data.country_names', 'Mexico');
echo $accessor->get('data.country_names'); // ['Russia', 'Belarus', 'Mexico']

$accessor->delete('data.name');
var_dump($accessor->exist('data.name')); // false

  Files folder image Files (30)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (5 directories)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (30)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (30)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files (30)  /  src  
File Role Description
Files folder imageComponents (3 files)
Files folder imageExceptions (1 file)
Files folder imageFactories (2 files)
Files folder imageHelpers (3 files)
Files folder imageInterfaces (4 files)

  Files folder image Files (30)  /  src  /  Components  
File Role Description
  Plain text file NestedAccessor.php Class Class source
  Plain text file NestedArrayStorage.php Class Class source
  Plain text file SilentNestedAccessor.php Class Class source

  Files folder image Files (30)  /  src  /  Exceptions  
File Role Description
  Plain text file NestedAccessorException.php Class Class source

  Files folder image Files (30)  /  src  /  Factories  
File Role Description
  Plain text file NestedAccessorFactory.php Class Class source
  Plain text file SilentNestedAccessorFactory.php Class Class source

  Files folder image Files (30)  /  src  /  Helpers  
File Role Description
  Plain text file ArrayHelper.php Class Class source
  Plain text file NestedAccess.php Class Class source
  Plain text file NestedHelper.php Class Class source

  Files folder image Files (30)  /  src  /  Interfaces  
File Role Description
  Plain text file NestedAccessorFactoryInterface.php Class Class source
  Plain text file NestedAccessorInterface.php Class Class source
  Plain text file SilentNestedAccessorFactoryInterface.php Class Class source
  Plain text file SilentNestedAccessorInterface.php Class Class source

  Files folder image Files (30)  /  tests  
File Role Description
Files folder imageunit (6 files, 1 directory)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files (30)  /  tests  /  unit  
File Role Description
Files folder imageFixtures (1 file)
  Plain text file NestedAccessorExceptionTest.php Class Class source
  Plain text file NestedAccessorTest.php Class Class source
  Plain text file NestedFactoryTest.php Class Class source
  Plain text file NestedHelperTest.php Class Class source
  Plain text file NestedStorageTest.php Class Class source
  Plain text file SilentNestedAccessorTest.php Class Class source

  Files folder image Files (30)  /  tests  /  unit  /  Fixtures  
File Role Description
  Plain text file ClassWithAccessibleProperties.php Class Class source

  Files folder image Files (30)  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.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:58
This week:0
All time:10,535
This week:74Up