PHP Classes

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

Recommend this page to a friend!

  Author Author  
Picture of Smoren  Freelight
Name: Smoren Freelight <contact>
Classes: 20 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 9x


  Detailed description   Download Download .zip .tar.gz  
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

Details

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

  Classes of Smoren Freelight  >  Nested PHP Accessor  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog (1)  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: Nested PHP Accessor
Base name: nested-accessor-php
Description: Get and set values of nested arrays or objects
Version: -
PHP version: 7.4
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image Algorithms Numerical and statistical algorithms View top rated classes
Group folder image Libraries Frameworks and libraries of cooperating classes View top rated classes
Group folder image PHP 7 Classes using PHP 7 specific features View top rated classes
Group folder image PSR Packages that implement PHP Standard Recommendations by FIG View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
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  /  .github  
File Role Description
Files folder imageworkflows (1 file)

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

  Files folder image Files  /  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  /  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  /  src  /  Exceptions  
File Role Description
  Plain text file NestedAccessorException.php Class Class source

  Files folder image Files  /  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  /  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  /  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  /  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  /  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  /  tests  /  unit  /  Fixtures  
File Role Description
  Plain text file ClassWithAccessibleProperties.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

Download Download all files: nested-accessor-php.tar.gz nested-accessor-php.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  Files folder image Files  
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  /  .github  
File Role Description
Files folder imageworkflows (1 file)

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

  Files folder image Files  /  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  /  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  /  src  /  Exceptions  
File Role Description
  Plain text file NestedAccessorException.php Class Class source

  Files folder image Files  /  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  /  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  /  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  /  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  /  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  /  tests  /  unit  /  Fixtures  
File Role Description
  Plain text file ClassWithAccessibleProperties.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

Download Download all files: nested-accessor-php.tar.gz nested-accessor-php.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.