Skip to content

Commit

Permalink
Added wildcard to HAS resolves: #44 (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschoenbauer committed Dec 8, 2016
1 parent f4d9a04 commit 01d37bb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
33 changes: 25 additions & 8 deletions src/DotNotation/ArrayDotNotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function recursiveGet($data, $keys) {

protected function wildCardGet(array $keys, $data) {
$output = [];
foreach ($data as $key => $value) {
foreach (array_keys($data) as $key) {
try {
$tempKeys = $keys;
array_unshift($tempKeys, $key);
Expand All @@ -157,6 +157,7 @@ protected function wildCardGet(array $keys, $data) {
if ($this->getGetMode() !== self::MODE_RETURN_FOUND) {
throw $exc;
}
//else do nothing
}
}
return $output;
Expand Down Expand Up @@ -300,15 +301,31 @@ public function setNotationType($notationType = ".") {
*/
public function has($dotNotation) {
$keys = $this->getKeys($dotNotation);
$dataRef = &$this->_data;
foreach ($keys as $key) {
if (!is_array($dataRef) || !array_key_exists($key, $dataRef)) {
return false;
} else {
$dataRef = &$dataRef[$key];
$data = $this->_data;
return $this->recursiveHas($keys, $data);
}

protected function recursiveHas(array $keys, $data) {
$key = array_shift($keys);
if (is_array($data) && $key === static::WILD_CARD_CHARACTER) {
return $this->wildCardHas($keys, $data);
} elseif (!is_array($data) || !array_key_exists($key, $data)) {
return false;
} elseif ($key && count($keys) == 0) { //Last Key
return true;
}
return $this->recursiveHas($keys, $data[$key]);
}

public function wildCardHas($keys, $data) {
foreach (array_keys($data) as $key) {
$tempKeys = $keys;
array_unshift($tempKeys, $key);
if ($this->recursiveHas($tempKeys, $data)) {
return true;
}
}
return true;
return false;
}

/**
Expand Down
35 changes: 29 additions & 6 deletions tests/DotNotation/ArrayDotNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class ArrayDotNotationTest extends PHPUnit_Framework_TestCase {
protected function setUp() {
$data = [
'levelA' => [
'levelB' => 'someValueB'
'levelB' => 'someValueB',
'levelB2' => [
'levelC2' => [
'levelD2' => 'someValueD2'
]
]
],
'levelB' => 'levelB',
'level1' => [
Expand Down Expand Up @@ -58,7 +63,7 @@ public function testGet() {
}

public function testGetWildCardEndWithWild() {
$this->assertEquals(['someValueB'], $this->_object->get('levelA.*'));
$this->assertEquals(['someValue2'], $this->_object->get('level1.*'));
}

public function testGetWildCardDefault() {
Expand All @@ -84,8 +89,8 @@ public function testGetWildTable() {
];
$this->assertEquals(['a', 'b', 'c', 'd', 'e', 'f'], $this->_object->setData($data)->get('test.test.*.value'));
}
public function testGetWildCardNotFoundException(){

public function testGetWildCardNotFoundException() {
$this->expectException(PathNotFoundException::class);
$this->_object->setGetMode(ArrayDotNotation::MODE_THROW_EXCEPTION)->get('*.levelC', 'noValue');
}
Expand Down Expand Up @@ -137,7 +142,7 @@ public function testSetDeepLevelExistingValue() {
}

public function testSetDataTypeConversion() {
$this->assertEquals(['levelB' => 'someValueB'], $this->_object->get('levelA'));
$this->assertEquals(['levelB' => 'someValueB', 'levelB2' => ['levelC2' => ['levelD2' => 'someValueD2']]], $this->_object->get('levelA'));
$this->assertEquals('newValue', $this->_object->set('levelA', 'newValue')->get('levelA'));
$this->assertEquals('someValue2', $this->_object->get('level1.level2'), "existing value compromised");
}
Expand Down Expand Up @@ -178,7 +183,13 @@ public function testMergeNotPresentKey() {

public function testRemove() {
$data = [
'levelA' => [],
'levelA' => [
'levelB2' => [
'levelC2' => [
'levelD2' => 'someValueD2'
]
]
],
'levelB' => 'levelB',
'level1' => [
'level2' => 'someValue2'
Expand Down Expand Up @@ -224,6 +235,18 @@ public function testHas() {
$this->assertFalse($this->_object->has('level2'));
}

public function testHasWildCard() {
$this->assertTrue($this->_object->has('*'));
$this->assertTrue($this->_object->has('*.levelB'));
$this->assertTrue($this->_object->has('*.*.*.*'));
$this->assertTrue($this->_object->has('levelB'));
$this->assertTrue($this->_object->has('level1'));
$this->assertTrue($this->_object->has('level1.*'));
$this->assertFalse($this->_object->has('*.*.*.*.*'));
$this->assertFalse($this->_object->has('level1.level2.*'));
$this->assertFalse($this->_object->has('level1.level2.*'));
}

public function testGetMode() {
$this->assertEquals(ArrayDotNotation::MODE_RETURN_DEFAULT, $this->_object->setGetMode(ArrayDotNotation::MODE_RETURN_DEFAULT)->getGetMode());
$this->assertEquals(ArrayDotNotation::MODE_RETURN_FOUND, $this->_object->setGetMode(ArrayDotNotation::MODE_RETURN_FOUND)->getGetMode());
Expand Down

0 comments on commit 01d37bb

Please sign in to comment.