定制 pklink/dotor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pklink/dotor

最新稳定版本:2.0.0

Composer 安装命令:

composer require pklink/dotor

包简介

Easy access to array values using dot notation. Useful for handling configurations or something like that

README 文档

README

Dotor is a library for PHP 5.6 and higher to access an array by using dot notification. This can be useful for handling array configurations or something like that

Installation

Install Dotor with Composer

Create or update your composer.json

{
    "require": {
        "pklink/dotor": "2.*"
    }
}

And run Composer

php composer.phar install

Finally include Composers autoloader

include __DIR__ . '/vendor/autoload.php';

Usage

Using `Dotor is very simple. Create an instance with your (configurarion) array...

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

... and get the content by the get()-method.

$config->get('name');
$config->get('database.server');

Default values

$config->get('asdasdas');         // returns null
$config->get('asdasdas', 'blah'); // returns 'blah'

Scalar values

$config->getScalar('object');           // returns ''
$config->getScalar('object', 'blah');   // returns 'blah'
$config->getScalar('not-existing');     // returns ''
$config->getScalar('not-existing', []); // throw InvalidArgumentException

Arrays

$config->getArray('database');      // returns the database array
$config->getArray('notexit');       // returns []
$config->getArray('notexit', [1]);  // returns [1]

Boolean

$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

The getBoolean()-method and their alias getBool() handle the string '1' and the integer 1 as true. Otherwise this method returns the given $default or true.

$config->getBoolean('oneString', false);   // returns true
$config->getBoolean('twoString', false);   // returns false
$config->getBoolean('twoString');          // returns true

Run tests

php composer.phar install --dev
php vendor/bin/phpunit tests/

or with code-coverage-report

php composer.phar install --dev
php vendor/bin/phpunit --coverage-html output tests/

License

This package is licensed under the BSD 2-Clause License. See the LICENSE file for details.

Credits

This package is inspired by php-dotty

统计信息

  • 总下载量: 738
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2013-02-11