Skip to content

Commit

Permalink
Added typed functions to array-dot library (#1130)
Browse files Browse the repository at this point in the history
* Added typed functions to array-dot library

* Added array_dot_get_enum function

* CS Fixes
  • Loading branch information
norberttech authored Jul 21, 2024
1 parent ae000a8 commit 964409b
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 1 deletion.
89 changes: 89 additions & 0 deletions src/lib/array-dot/src/Flow/ArrayDot/array_dot.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,95 @@ function array_dot_set(array $array, string $path, $value) : array
return \array_merge($array, $newArray);
}

function array_dot_get_int(array $array, string $path) : ?int
{
$result = array_dot_get($array, $path);

if ($result === null) {
return null;
}

return (int) $result;
}

function array_dot_get_string(array $array, string $path) : ?string
{
$result = array_dot_get($array, $path);

if ($result === null) {
return null;
}

return (string) $result;
}

function array_dot_get_bool(array $array, string $path) : ?bool
{
$result = array_dot_get($array, $path);

if ($result === null) {
return null;
}

return (bool) $result;
}

function array_dot_get_float(array $array, string $path) : ?float
{
$result = array_dot_get($array, $path);

if ($result === null) {
return null;
}

return (float) $result;
}

function array_dot_get_datetime(array $array, string $path) : ?\DateTimeImmutable
{
$result = array_dot_get($array, $path);

if ($result === null) {
return null;
}

return new \DateTimeImmutable($result);
}

/**
* @template T is \BackedEnum
*
* @param array<mixed> $array
* @param string $path
* @param class-string<T> $enumClass
*
* @return null|\BackedEnum
*/
function array_dot_get_enum(array $array, string $path, string $enumClass) : ?\BackedEnum
{
if (!\class_exists($enumClass)) {
throw new Exception('Enum class does not exist');
}

if (!\is_subclass_of($enumClass, \BackedEnum::class)) {
throw new Exception('Enum class must be subclass of BackedEnum');
}

$reflection = new \ReflectionEnum($enumClass);

$result = match ((string) $reflection->getBackingType()) {
'int' => array_dot_get_int($array, $path),
'string' => array_dot_get_string($array, $path),
default => throw new Exception('Unsupported enum backing type: ' . $reflection->getBackingType())
};

if ($result === null) {
return null;
}

return $enumClass::tryFrom($result);
}

/**
* @param array<mixed> $array
* @param string $path
Expand Down
115 changes: 114 additions & 1 deletion src/lib/array-dot/tests/Flow/ArrayDot/Tests/Unit/ArrayDotGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

namespace Flow\ArrayDot\Tests\Unit;

use function Flow\ArrayDot\{array_dot_exists, array_dot_get};
use function Flow\ArrayDot\{array_dot_exists,
array_dot_get,
array_dot_get_bool,
array_dot_get_datetime,
array_dot_get_enum,
array_dot_get_float,
array_dot_get_int,
array_dot_get_string};
use Flow\ArrayDot\Exception\InvalidPathException;
use Flow\ArrayDot\Tests\Unit\Fixtures\{Letters, Numbers};
use PHPUnit\Framework\TestCase;

final class ArrayDotGetTest extends TestCase
Expand Down Expand Up @@ -516,6 +524,111 @@ public function test_all_multi_key_get_nested() : void
);
}

public function test_array_dot_get_boolean() : void
{
self::assertTrue(
array_dot_get_bool(['active' => true], 'active')
);

self::assertFalse(
array_dot_get_bool(['active' => false], 'active')
);

self::assertNull(
array_dot_get_bool(['activate' => 'true'], '?active')
);
}

public function test_array_dot_get_datetime() : void
{
self::assertEquals(
new \DateTimeImmutable('2021-01-01 00:00:00'),
array_dot_get_datetime(['created_at' => '2021-01-01 00:00:00'], 'created_at')
);

self::assertNull(
array_dot_get_datetime(['created_at' => '2021-01-01 00:00:00'], '?updated_at')
);
}

public function test_array_dot_get_enum() : void
{
self::assertSame(
Numbers::ONE,
array_dot_get_enum(['id' => 1], 'id', Numbers::class)
);

self::assertSame(
Letters::A,
array_dot_get_enum(['id' => 'A'], 'id', Letters::class)
);

self::assertSame(
Numbers::ONE,
array_dot_get_enum(['id' => '1'], 'id', Numbers::class)
);

self::assertNull(
array_dot_get_enum(['identifier' => 1], '?id', Numbers::class)
);
}

public function test_array_dot_get_float() : void
{
self::assertSame(
1.0,
array_dot_get_float(['id' => 1.0], 'id')
);

self::assertSame(
10.0,
array_dot_get_float(['id' => 10], 'id')
);

self::assertSame(
1.0,
array_dot_get_float(['id' => '1.0'], 'id')
);

self::assertNull(
array_dot_get_float(['identifier' => 1.0], '?id')
);
}

public function test_array_dot_get_int() : void
{
self::assertSame(
1,
array_dot_get_int(['id' => 1], 'id')
);

self::assertSame(
1,
array_dot_get_int(['id' => '01'], 'id')
);

self::assertNull(
array_dot_get_int(['identifier' => 1], '?id')
);
}

public function test_array_dot_get_string() : void
{
self::assertSame(
'foo',
array_dot_get_string(['name' => 'foo'], 'name')
);

self::assertSame(
'1',
array_dot_get_string(['name' => 1], 'name')
);

self::assertNull(
array_dot_get_string(['identifier' => 'foo'], '?name')
);
}

public function test_escape_dot_path() : void
{
self::assertSame(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Flow\ArrayDot\Tests\Unit\Fixtures;

enum Letters : string
{
case A = 'A';
case B = 'B';
case C = 'C';
case D = 'D';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Flow\ArrayDot\Tests\Unit\Fixtures;

enum Numbers : int
{
case ONE = 1;
case THREE = 3;
case TWO = 2;
case ZERO = 0;
}

0 comments on commit 964409b

Please sign in to comment.