pklink / dotor
此包已被弃用,不再维护。未建议替代包。
使用点表示法轻松访问数组值。适用于处理配置或类似情况
2.0.0
2016-01-03 18:10 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ~5.0
README
Dotor 是一个用于 PHP 5.6 及以上版本的库,通过点表示法访问数组。这可以用于处理数组配置或类似情况
安装
使用 Composer 安装 Dotor
创建或更新你的 composer.json
{ "require": { "pklink/dotor": "2.*" } }
然后运行 Composer
php composer.phar install
最后包含 Composers 自动加载器
include __DIR__ . '/vendor/autoload.php';
使用
使用 `Dotor 非常简单。创建一个包含你的(配置)数组的实例...
// config-sample.php return [ 'name' => 'sample configuration', 'database' => [ 'server' => 'localhost', 'username' => 'root', 'password' => 'password', 'database' => 'blah', 'type' => 'sqlite' ], 'object' => new stdClass(), 'false' => false, 'true' => true, 'zeroString' => '0', 'zeroInt' => 0, 'oneString' => '1', 'oneInt' => 1, 'twoString' => '2', ];
$loader = ArrayLoader::createFromFile('./config-sample.php'); $config = new Dotor($loader);
... 然后通过 get()
方法获取内容。
$config->get('name'); $config->get('database.server');
默认值
$config->get('asdasdas'); // returns null $config->get('asdasdas', 'blah'); // returns 'blah'
标量值
$config->getScalar('object'); // returns '' $config->getScalar('object', 'blah'); // returns 'blah' $config->getScalar('not-existing'); // returns '' $config->getScalar('not-existing', []); // throw InvalidArgumentException
数组
$config->getArray('database'); // returns the database array $config->getArray('notexit'); // returns [] $config->getArray('notexit', [1]); // returns [1]
布尔值
$config->getBoolean('database', false); // returns false $config->getBool('database', true); // returns true $config->getBoolean('zeroString', true); // returns false $config->getBoolean('zeroInt', true); // returns false $config->getBoolean('oneString', false); // returns true $config->getBoolean('oneInt', false); // returns true
getBoolean()
方法及其别名 getBool()
将字符串 '1'
和整数 1
作为 true
处理。否则,此方法返回给定的 $default
或 true
。
$config->getBoolean('oneString', false); // returns true $config->getBoolean('twoString', false); // returns false $config->getBoolean('twoString'); // returns true
运行测试
php composer.phar install --dev php vendor/bin/phpunit tests/
或带有代码覆盖率报告
php composer.phar install --dev php vendor/bin/phpunit --coverage-html output tests/
许可协议
此包根据 BSD 2-Clause 许可协议许可。有关详细信息,请参阅 LICENSE 文件。
致谢
此包受到 php-dotty 的启发