Skip to content
Snippets Groups Projects
Unverified Commit 70a44e8a authored by Beau Simensen's avatar Beau Simensen Committed by GitHub
Browse files

Merge pull request #12 from martinssipenko/php7

Only support PHP 7.0 and above
parents 3fbd8749 07a88353
No related branches found
No related tags found
No related merge requests found
language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
before_script:
- wget -nc http://getcomposer.org/composer.phar
- php composer.phar install --dev
- php composer.phar install
script: phpunit --coverage-text
script: ./vendor/bin/phpunit --coverage-text
......@@ -7,7 +7,9 @@ Given a deep data structure, access data by dot notation.
Requirements
------------
* PHP (5.3+)
* PHP (7.0+)
> For PHP (5.3+) please reffer to version `1.0`.
Usage
......@@ -23,15 +25,15 @@ $data = new Data;
$data->set('a.b.c', 'C');
$data->set('a.b.d', 'D1');
$data->append('a.b.d', 'D2');
$data->set('a.b.e', array('E0', 'E1', 'E2'));
$data->set('a.b.e', ['E0', 'E1', 'E2']);
// C
$data->get('a.b.c');
// array('D1', 'D2')
// ['D1', 'D2']
$data->get('a.b.d');
// array('E0', 'E1', 'E2')
// ['E0', 'E1', 'E2']
$data->get('a.b.e');
// true
......@@ -46,32 +48,32 @@ A more concrete example:
```php
use Dflydev\DotAccessData\Data;
$data = new Data(array(
'hosts' => array(
'hewey' => array(
$data = new Data([
'hosts' => [
'hewey' => [
'username' => 'hman',
'password' => 'HPASS',
'roles' => array('web'),
),
'dewey' => array(
'roles' => ['web'],
],
'dewey' => [
'username' => 'dman',
'password' => 'D---S',
'roles' => array('web', 'db'),
'nick' => 'dewey dman'
),
'lewey' => array(
'roles' => ['web', 'db'],
'nick' => 'dewey dman',
],
'lewey' => [
'username' => 'lman',
'password' => 'LP@$$',
'roles' => array('db'),
),
)
));
'roles' => ['db'],
],
],
]);
// hman
$username = $data->get('hosts.hewey.username');
// HPASS
$password = $data->get('hosts.hewey.password');
// array('web')
// ['web']
$roles = $data->get('hosts.hewey.roles');
// dewey dman
$nick = $data->get('hosts.dewey.nick');
......@@ -84,7 +86,7 @@ $dewey = $data->getData('hosts.dewey');
$username = $dewey->get('username');
// D---S
$password = $dewey->get('password');
// array('web', 'db')
// ['web', 'db']
$roles = $dewey->get('roles');
// No more lewey
......@@ -93,11 +95,11 @@ $data->remove('hosts.lewey');
// Add DB to hewey's roles
$data->append('hosts.hewey.roles', 'db');
$data->set('hosts.april', array(
$data->set('hosts.april', [
'username' => 'aman',
'password' => '@---S',
'roles' => array('web'),
));
'roles' => ['web'],
]);
// Check if a key exists (true to this case)
$hasKey = $data->has('hosts.dewey.username');
......
......@@ -23,16 +23,24 @@
}
],
"require": {
"php": ">=5.3.2"
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-0": {
"Dflydev\\DotAccessData": "src"
"psr-4": {
"Dflydev\\DotAccessData\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Dflydev\\DotAccessData\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "2.0-dev"
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="tests/bootstrap.php">
<phpunit colors="true">
<testsuites>
<testsuite name="Dot Access Data Test Suite">
<directory>./tests/Dflydev/DotAccessData</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/Dflydev/DotAccessData/</directory>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
......@@ -11,6 +11,8 @@
namespace Dflydev\DotAccessData;
use RuntimeException;
class Data implements DataInterface
{
/**
......@@ -27,7 +29,7 @@ class Data implements DataInterface
*/
public function __construct(array $data = null)
{
$this->data = $data ?: array();
$this->data = $data ?: [];
}
/**
......@@ -36,7 +38,7 @@ class Data implements DataInterface
public function append($key, $value = null)
{
if (0 == strlen($key)) {
throw new \RuntimeException("Key cannot be an empty string");
throw new RuntimeException("Key cannot be an empty string");
}
$currentValue =& $this->data;
......@@ -44,12 +46,12 @@ class Data implements DataInterface
if (1 == count($keyPath)) {
if (!isset($currentValue[$key])) {
$currentValue[$key] = array();
$currentValue[$key] = [];
}
if (!is_array($currentValue[$key])) {
// Promote this key to an array.
// TODO: Is this really what we want to do?
$currentValue[$key] = array($currentValue[$key]);
$currentValue[$key] = [$currentValue[$key]];
}
$currentValue[$key][] = $value;
......@@ -60,16 +62,16 @@ class Data implements DataInterface
for ( $i = 0; $i < count($keyPath); $i++ ) {
$currentKey =& $keyPath[$i];
if ( ! isset($currentValue[$currentKey]) ) {
$currentValue[$currentKey] = array();
$currentValue[$currentKey] = [];
}
$currentValue =& $currentValue[$currentKey];
}
if (!isset($currentValue[$endKey])) {
$currentValue[$endKey] = array();
$currentValue[$endKey] = [];
}
if (!is_array($currentValue[$endKey])) {
$currentValue[$endKey] = array($currentValue[$endKey]);
$currentValue[$endKey] = [$currentValue[$endKey]];
}
// Promote this key to an array.
// TODO: Is this really what we want to do?
......@@ -82,7 +84,7 @@ class Data implements DataInterface
public function set($key, $value = null)
{
if (0 == strlen($key)) {
throw new \RuntimeException("Key cannot be an empty string");
throw new RuntimeException("Key cannot be an empty string");
}
$currentValue =& $this->data;
......@@ -98,10 +100,10 @@ class Data implements DataInterface
for ( $i = 0; $i < count($keyPath); $i++ ) {
$currentKey =& $keyPath[$i];
if (!isset($currentValue[$currentKey])) {
$currentValue[$currentKey] = array();
$currentValue[$currentKey] = [];
}
if (!is_array($currentValue[$currentKey])) {
throw new \RuntimeException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
throw new RuntimeException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
}
$currentValue =& $currentValue[$currentKey];
}
......@@ -114,7 +116,7 @@ class Data implements DataInterface
public function remove($key)
{
if (0 == strlen($key)) {
throw new \RuntimeException("Key cannot be an empty string");
throw new RuntimeException("Key cannot be an empty string");
}
$currentValue =& $this->data;
......@@ -191,7 +193,7 @@ class Data implements DataInterface
return new Data($value);
}
throw new \RuntimeException("Value at '$key' could not be represented as a DataInterface");
throw new RuntimeException("Value at '$key' could not be represented as a DataInterface");
}
/**
......
File moved
File moved
......@@ -11,43 +11,46 @@
namespace Dflydev\DotAccessData;
class DataTest extends \PHPUnit_Framework_TestCase
use RuntimeException;
use PHPUnit\Framework\TestCase;
class DataTest extends TestCase
{
protected function getSampleData()
{
return array(
return [
'a' => 'A',
'b' => array(
'b' => [
'b' => 'B',
'c' => array('C1', 'C2', 'C3'),
'd' => array(
'c' => ['C1', 'C2', 'C3'],
'd' => [
'd1' => 'D1',
'd2' => 'D2',
'd3' => 'D3',
),
),
'c' => array('c1', 'c2', 'c3'),
'f' => array(
'g' => array(
],
],
'c' => ['c1', 'c2', 'c3'],
'f' => [
'g' => [
'h' => 'FGH',
),
),
'h' => array(
],
],
'h' => [
'i' => 'I',
),
'i' => array(
],
'i' => [
'j' => 'J',
),
);
],
];
}
protected function runSampleDataTests(DataInterface $data)
{
$this->assertEquals('A', $data->get('a'));
$this->assertEquals('B', $data->get('b.b'));
$this->assertEquals(array('C1', 'C2', 'C3'), $data->get('b.c'));
$this->assertEquals(['C1', 'C2', 'C3'], $data->get('b.c'));
$this->assertEquals('D3', $data->get('b.d.d3'));
$this->assertEquals(array('c1', 'c2', 'c3'), $data->get('c'));
$this->assertEquals(['c1', 'c2', 'c3'], $data->get('c'));
$this->assertNull($data->get('foo'), 'Foo should not exist');
$this->assertNull($data->get('f.g.h.i'));
$this->assertEquals($data->get('foo', 'default-value-1'), 'default-value-1', 'Return default value');
......@@ -68,17 +71,17 @@ class DataTest extends \PHPUnit_Framework_TestCase
$data->append('h.i', 'I2');
$data->append('i.k.l', 'L');
$this->assertEquals(array('A', 'B'), $data->get('a'));
$this->assertEquals(array('c1', 'c2', 'c3', 'c4'), $data->get('c'));
$this->assertEquals(array('C1', 'C2', 'C3', 'C4'), $data->get('b.c'));
$this->assertEquals(array('D3', 'D3b'), $data->get('b.d.d3'));
$this->assertEquals(array('D'), $data->get('b.d.d4'));
$this->assertEquals(array('E'), $data->get('e'));
$this->assertEquals(array('b'), $data->get('f.a'));
$this->assertEquals(array('I', 'I2'), $data->get('h.i'));
$this->assertEquals(array('L'), $data->get('i.k.l'));
$this->assertEquals(['A', 'B'], $data->get('a'));
$this->assertEquals(['c1', 'c2', 'c3', 'c4'], $data->get('c'));
$this->assertEquals(['C1', 'C2', 'C3', 'C4'], $data->get('b.c'));
$this->assertEquals(['D3', 'D3b'], $data->get('b.d.d3'));
$this->assertEquals(['D'], $data->get('b.d.d4'));
$this->assertEquals(['E'], $data->get('e'));
$this->assertEquals(['b'], $data->get('f.a'));
$this->assertEquals(['I', 'I2'], $data->get('h.i'));
$this->assertEquals(['L'], $data->get('i.k.l'));
$this->setExpectedException('RuntimeException');
$this->expectException(RuntimeException::class);
$data->append('', 'broken');
}
......@@ -93,15 +96,15 @@ class DataTest extends \PHPUnit_Framework_TestCase
$data->set('a', 'A');
$data->set('b.c', 'C');
$data->set('d.e', array('f' => 'F', 'g' => 'G',));
$data->set('d.e', ['f' => 'F', 'g' => 'G']);
$this->assertEquals('A', $data->get('a'));
$this->assertEquals(array('c' => 'C'), $data->get('b'));
$this->assertEquals(['c' => 'C'], $data->get('b'));
$this->assertEquals('C', $data->get('b.c'));
$this->assertEquals('F', $data->get('d.e.f'));
$this->assertEquals(array('e' => array('f' => 'F', 'g' => 'G',)), $data->get('d'));
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data->get('d'));
$this->setExpectedException('RuntimeException');
$this->expectException(RuntimeException::class);
$data->set('', 'broken');
}
......@@ -112,7 +115,7 @@ class DataTest extends \PHPUnit_Framework_TestCase
$data->set('a.b.c', 'Should not be able to write to a.b.c.d.e');
$this->setExpectedException('RuntimeException');
$this->expectException(RuntimeException::class);
$data->set('a.b.c.d.e', 'broken');
}
......@@ -134,7 +137,7 @@ class DataTest extends \PHPUnit_Framework_TestCase
$this->assertNull(null);
$this->assertEquals('D2', $data->get('b.d.d2'));
$this->setExpectedException('RuntimeException');
$this->expectException(RuntimeException::class);
$data->remove('', 'broken');
}
......@@ -151,13 +154,13 @@ class DataTest extends \PHPUnit_Framework_TestCase
$data = new Data($this->getSampleData());
foreach (
array('a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1') as $existentKey
['a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1'] as $existentKey
) {
$this->assertTrue($data->has($existentKey));
}
foreach (
array('p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1') as $notExistentKey
['p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1'] as $notExistentKey
) {
$this->assertFalse($data->has($notExistentKey));
}
......@@ -165,17 +168,17 @@ class DataTest extends \PHPUnit_Framework_TestCase
public function testGetData()
{
$wrappedData = new Data(array(
'wrapped' => array(
$wrappedData = new Data([
'wrapped' => [
'sampleData' => $this->getSampleData()
),
));
],
]);
$data = $wrappedData->getData('wrapped.sampleData');
$this->runSampleDataTests($data);
$this->setExpectedException('RuntimeException');
$this->expectException(RuntimeException::class);
$data = $wrappedData->getData('wrapped.sampleData.a');
}
......
......@@ -2,7 +2,7 @@
/*
* This file is a part of dflydev/dot-access-data.
*
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
......@@ -11,13 +11,15 @@
namespace Dflydev\DotAccessData;
class UtilTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;
class UtilTest extends TestCase
{
public function testIsAssoc()
{
$this->assertTrue(Util::isAssoc(array('a' => 'A',)));
$this->assertTrue(Util::isAssoc(array()));
$this->assertFalse(Util::isAssoc(array(1 => 'One',)));
$this->assertTrue(Util::isAssoc(['a' => 'A']));
$this->assertTrue(Util::isAssoc([]));
$this->assertFalse(Util::isAssoc([1 => 'One']));
}
/**
......@@ -31,91 +33,90 @@ class UtilTest extends \PHPUnit_Framework_TestCase
public function mergeAssocArrayProvider()
{
return array(
array(
return [
[
'Clobber should replace to value with from value for strings (shallow)',
// to
array('a' => 'A'),
['a' => 'A'],
// from
array('a' => 'B'),
['a' => 'B'],
// clobber
true,
// expected result
array('a' => 'B'),
),
['a' => 'B'],
],
array(
[
'Clobber should replace to value with from value for strings (deep)',
// to
array('a' => array('b' => 'B',),),
['a' => ['b' => 'B']],
// from
array('a' => array('b' => 'C',),),
['a' => ['b' => 'C']],
// clobber
true,
// expected result
array('a' => array('b' => 'C',),),
),
['a' => ['b' => 'C']]
],
array(
[
'Clobber should NOTreplace to value with from value for strings (shallow)',
// to
array('a' => 'A'),
['a' => 'A'],
// from
array('a' => 'B'),
['a' => 'B'],
// clobber
false,
// expected result
array('a' => 'A'),
),
['a' => 'A'],
],
array(
[
'Clobber should NOT replace to value with from value for strings (deep)',
// to
array('a' => array('b' => 'B',),),
['a' => ['b' => 'B']],
// from
array('a' => array('b' => 'C',),),
['a' => ['b' => 'C']],
// clobber
false,
// expected result
array('a' => array('b' => 'B',),),
),
['a' => ['b' => 'B']],
],
array(
[
'Associative arrays should be combined',
// to
array('a' => array('b' => 'B',),),
['a' => ['b' => 'B']],
// from
array('a' => array('c' => 'C',),),
['a' => ['c' => 'C']],
// clobber
null,
// expected result
array('a' => array('b' => 'B', 'c' => 'C',),),
),
['a' => ['b' => 'B', 'c' => 'C']],
],
array(
[
'Arrays should be replaced (with clobber enabled)',
// to
array('a' => array('b', 'c',)),
['a' => ['b', 'c']],
// from
array('a' => array('B', 'C',),),
['a' => ['B', 'C']],
// clobber
true,
// expected result
array('a' => array('B', 'C',),),
),
['a' => ['B', 'C']],
],
array(
[
'Arrays should be NOT replaced (with clobber disabled)',
// to
array('a' => array('b', 'c',)),
['a' => ['b', 'c']],
// from
array('a' => array('B', 'C',),),
['a' => ['B', 'C']],
// clobber
false,
// expected result
array('a' => array('b', 'c',),),
),
);
['a' => ['b', 'c']],
],
];
}
}
<?php
/*
* This file is a part of dflydev/dot-access-data.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
$loader = require dirname(__DIR__).'/vendor/autoload.php';
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment