Skip to content

SimoneS93/DotNot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Access you data via dot-notation, no matter if it's an array, a stdClass or a custom object!

Install

$ composer require simones/dotnot

Usage

Construct a DotNot instance with your data and access it with the get method: that's all! Your data can be composed of any combination of arrays, stdClass instances and custom classes' instances: they all behave the same. You can also use the dotnot helper function, which accepts a mandatory $root argument and an optional $path to resolve.

Here are some examples (for more, head to the spec):

// stdClass + array
dotnot((object) [
    'author' => [
        'name' => 'Simone Salerno'
    ]
])->get('author.name')

// Custom class, with getter method
class Author {
    public function getAuthorName() {
        return 'Simone Salerno';
    }
}

// this looks for a method named "get" . ucfirst($getter)
dotnot(new Author)->get('authorName')

// when it can't resolve the path, it throws an exception
// so you should catch it or test for existence
dotnot(new Author)->get('foo') // throws DotNotException
//or
$dotnot = dotnot(new Author);

if ($dotnot->has('foo')) {
    // do some work...
}

// it also works with numeric indexes
dotnot([
    'people' => [
        0 => (object) [
            'author' => new Author
        ]
    ]
])->get('people.0.author.authorName')

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages