Skip to content
This repository has been archived by the owner on Apr 10, 2022. It is now read-only.

pklink/dotor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dotor Build Status Scrutinizer Quality Score

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

About

Easy access to array values using dot notation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages