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
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';
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');
$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
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
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/
This package is licensed under the BSD 2-Clause License. See the LICENSE file for details.
This package is inspired by php-dotty