Skip to content

Commit

Permalink
Fix key handling bug in KeyWalker
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-redfern committed Jul 16, 2020
1 parent afee3ed commit f70e950
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

2020-07-16 - 3.3.2
------------------

* Fix explode() requires a string and will fail if given an int (in KeyWalker)

2020-04-23 - 3.3.1
------------------

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"require-dev": {
"phpunit/phpunit": "~8.2",
"symfony/var-dumper": "~4.3",
"symfony/var-dumper": "~5",
"victorjonsson/markdowndocs": "^1.3"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/KeyWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function get($collection, $key, $default = null)
return $collection[$key];
}

$key = is_array($key) ? $key : explode('.', $key);
$key = is_array($key) ? $key : explode('.', (string)$key);

while (!is_null($segment = array_shift($key))) {
if ($segment === '*') {
Expand Down Expand Up @@ -92,7 +92,7 @@ public static function has($collection, $key): bool
return true;
}

$key = is_array($key) ? $key : explode('.', $key);
$key = is_array($key) ? $key : explode('.', (string)$key);

while (!is_null($segment = array_shift($key))) {
if ($segment === '*') {
Expand Down Expand Up @@ -163,7 +163,7 @@ public static function extract($collection, $value, $key = null, $default = null
protected static function extractKeyValueParameters($value, $key): array
{
$value = is_string($value) ? explode('.', $value) : $value;
$key = is_null($key) || is_array($key) ? $key : explode('.', $key);
$key = is_null($key) || is_array($key) ? $key : explode('.', (string)$key);

return [$value, $key];
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Behaviours/Query/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public function testGet()
$this->assertNull($col->get('abe'));
}

/**
* @group accessors
*/
public function testGetByInt()
{
$col = new Collection([
'test1',
'test2',
'test3',
'test4',
]);

$this->assertEquals('test3', $col->get(2));
$this->assertNull($col->get(1234));
}

/**
* @group accessors
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Behaviours/Query/HasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public function testHas()
$this->assertFalse($col->has('abe'));
}

/**
* @group collection
*/
public function testHasByInt()
{
$col = new Collection([
'test-1',
'test-2',
'test-abc',
'test-abe',
]);

$this->assertTrue($col->has(3));
$this->assertFalse($col->has(678));
}

/**
* @group collection
*/
Expand Down

0 comments on commit f70e950

Please sign in to comment.