Skip to content

Commit

Permalink
Merge pull request #67 from TheDragonCode/3.x
Browse files Browse the repository at this point in the history
Fixed handling of boolean values
  • Loading branch information
andrey-helldar authored May 23, 2023
2 parents 5796aa0 + b574f25 commit c8a4bcb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/Arrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function arrayMap(array $values, callable $callback): array
return $value;
})
->flatten()
->filter(static fn ($value) => ! empty($value) || is_numeric($value))
->filter(static fn ($value) => ! empty($value) || is_numeric($value) || is_bool($value))
->map($callback)
->values()
->toArray();
Expand Down
6 changes: 5 additions & 1 deletion src/Support/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public function get(string $separator, ArrayObject|array|DragonArrayable|Illumin

protected function hash(array $values, bool $hash = true): array
{
return $this->arrayMap($values, static fn ($value) => $hash ? md5((string) $value) : (string) $value);
return $this->arrayMap($values, static function (mixed $value) use ($hash) {
$value = is_bool($value) ? (int) $value : $value;

return $hash ? md5((string) $value) : (string) $value;
});
}

protected function compile(array $values, string $separator): string
Expand Down
13 changes: 11 additions & 2 deletions tests/Support/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public function testArray()
$this->assertSame($expected, $key);
}

public function testBoolean()
{
$key = Key::get(':', [true, false]);

$expected = 'c4ca4238a0b923820dcc509a6f75849b:cfcd208495d565ef66e7dff9f98764da';

$this->assertSame($expected, $key);
}

public function testCombine()
{
$key = Key::get(':', [1, 'Foo', [['Bar', 'Baz']]]);
Expand Down Expand Up @@ -109,9 +118,9 @@ public function testMultiObjectArrays()

public function testEmpties()
{
$key = Key::get(':', [null, '', 0, []]);
$key = Key::get(':', [null, '', 0, [], false]);

$expected = 'cfcd208495d565ef66e7dff9f98764da';
$expected = 'cfcd208495d565ef66e7dff9f98764da:cfcd208495d565ef66e7dff9f98764da';

$this->assertSame($expected, $key);
}
Expand Down

0 comments on commit c8a4bcb

Please sign in to comment.