simones/dotnot

此包最新版本(1.0.0)没有可用的许可信息。

使用点表示法访问您的数据。数组或对象:无论什么!

1.0.0 2016-09-15 19:32 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:53:04 UTC


README

Build Status

通过点表示法访问您的数据,无论它是数组、stdClass还是自定义对象!

安装

$ composer require simones/dotnot

用法

使用您的数据创建一个 DotNot 实例,并通过 get 方法访问它:就这么简单!您的数据可以由任何组合的数组、stdClass 实例和自定义类的实例组成:它们的行为都是一样的。您还可以使用接受强制 $root 参数和可选 $path 参数进行解析的 dotnot 辅助函数。

以下是一些示例(更多内容请参阅规范)

// 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')