Skip to content

Commit

Permalink
Merge pull request #39 from jingu/support_enum
Browse files Browse the repository at this point in the history
add support for enum value to ParamConverter
  • Loading branch information
koriym authored Oct 12, 2022
2 parents c82cee1 + 3f9effd commit 4497aef
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<testsuites>
<testsuite name="Ray.MediaQuery test suite" >
<directory>tests</directory>
<directory phpVersion="8.1.0" phpVersionOperator=">=">tests-php81</directory>
</testsuite>
</testsuites>
<php>
Expand Down
11 changes: 11 additions & 0 deletions src/ParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
use DateTimeInterface;
use Ray\MediaQuery\Exception\CouldNotBeConvertedException;

use function assert;
use function enum_exists;
use function function_exists;
use function get_class;
use function is_object;
use function method_exists;
use function print_r;
use function property_exists;

class ParamConverter implements ParamConverterInterface
{
Expand Down Expand Up @@ -41,6 +46,12 @@ public function __invoke(array &$values): void
continue;
}

if (function_exists('enum_exists') && enum_exists(get_class($value))) {
assert(property_exists($value, 'name'));
$value = $value->name;
continue;
}

throw new CouldNotBeConvertedException(print_r($value, true));
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests-php81/EnumParamConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Ray\MediaQuery;

use PHPUnit\Framework\TestCase;

class EnumParamConverterTest extends TestCase
{
public function testInvoke(): void
{
$values = ['status' => FakeEnum::public];
(new ParamConverter())($values);
$this->assertSame(['status' => 'public'], $values);
}
}
1 change: 1 addition & 0 deletions tests/DbQueryModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function setUp(): void
]);
$sqlDir = dirname(__DIR__) . '/tests/sql';
$dbQueryConfig = new DbQueryConfig($sqlDir);
/** @phpstan-ignore-next-line */
$module = new MediaQueryModule($mediaQueries, [$dbQueryConfig], new AuraSqlModule('sqlite::memory:', '', '', '', [PDO::ATTR_STRINGIFY_FETCHES => true]));
$this->injector = new Injector($module, __DIR__ . '/tmp');
$pdo = $this->injector->getInstance(ExtendedPdoInterface::class);
Expand Down
9 changes: 9 additions & 0 deletions tests/Fake/FakeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Ray\MediaQuery;

enum FakeEnum
{
case draft;
case public;
}
2 changes: 1 addition & 1 deletion tests/ParamConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testInvoke(): void
$this->assertSame(['date_val' => UnixEpocTime::TEXT, 'bool_val' => true, 'string_val' => 'a'], $values);
}

public function testInvaliParam(): void
public function testInvalidParam(): void
{
$this->expectException(CouldNotBeConvertedException::class);
$values = ['invalid' => new stdClass()];
Expand Down

0 comments on commit 4497aef

Please sign in to comment.