-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add new filter to filter to an enum #94
Open
reinfi
wants to merge
10
commits into
laminas:2.31.x
Choose a base branch
from
reinfi:to-enum-filter
base: 2.31.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fdef40c
add new filter to filter to an enum
reinfi 84f400f
fix some issues with types
reinfi cd769fb
fix psalm errors and return value instead of null
reinfi 8a6fd86
remove setter method
reinfi 7de3e2e
remove useless return in constructor
reinfi 2fca6f1
adjust codingstyle
reinfi 7182172
specify return type of filter function
reinfi fc001a4
register filter in plugin manager
reinfi 18ece90
add default value for constructor
reinfi 7d7f97d
fix return type of function
reinfi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Filter; | ||
|
||
use BackedEnum; | ||
use Laminas\Filter\Exception\RuntimeException; | ||
use Laminas\Stdlib\ArrayUtils; | ||
use Traversable; | ||
use UnitEnum; | ||
|
||
use function array_column; | ||
use function constant; | ||
use function in_array; | ||
use function is_array; | ||
use function is_int; | ||
use function is_string; | ||
use function is_subclass_of; | ||
|
||
/** | ||
* @psalm-type Options = array{ | ||
* enum: class-string<UnitEnum>, | ||
* } | ||
*/ | ||
final class ToEnum implements FilterInterface | ||
{ | ||
/** @var class-string<UnitEnum>|null */ | ||
private ?string $enumClass = null; | ||
|
||
/** | ||
* @param Traversable|class-string<UnitEnum>|Options $enumOrOptions | ||
*/ | ||
public function __construct($enumOrOptions) | ||
{ | ||
if ($enumOrOptions instanceof Traversable) { | ||
/** @var Options $enumOrOptions */ | ||
$enumOrOptions = ArrayUtils::iteratorToArray($enumOrOptions); | ||
} | ||
|
||
if ( | ||
is_array($enumOrOptions) && | ||
isset($enumOrOptions['enum']) | ||
) { | ||
$this->enumClass = $enumOrOptions['enum']; | ||
} | ||
|
||
if (is_string($enumOrOptions)) { | ||
$this->enumClass = $enumOrOptions; | ||
} | ||
} | ||
|
||
/** | ||
* Defined by Laminas\Filter\FilterInterface | ||
* | ||
* Returns an enum representation of $value if matching. | ||
* | ||
* @param mixed $value | ||
*/ | ||
reinfi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public function filter($value): mixed | ||
{ | ||
$enum = $this->enumClass; | ||
|
||
if ($enum === null) { | ||
throw new RuntimeException( | ||
'enum class not set' | ||
); | ||
} | ||
|
||
if (! is_string($value) && ! is_int($value)) { | ||
return $value; | ||
} | ||
|
||
if (is_subclass_of($enum, BackedEnum::class)) { | ||
return $enum::tryFrom($value) ?: $value; | ||
} | ||
|
||
if (! is_subclass_of($enum, UnitEnum::class)) { | ||
return $value; | ||
} | ||
|
||
if (in_array($value, array_column($enum::cases(), 'name'), true)) { | ||
return constant($enum . '::' . $value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Filter\TestAsset; | ||
|
||
enum TestIntBackedEnum: int | ||
{ | ||
case Foo = 1; | ||
case Bar = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Filter\TestAsset; | ||
|
||
enum TestStringBackedEnum: string | ||
{ | ||
case Foo = 'foo'; | ||
case Bar = 'bar'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Filter\TestAsset; | ||
|
||
enum TestUnitEnum | ||
{ | ||
case foo; | ||
case bar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Filter; | ||
|
||
use BackedEnum; | ||
use Laminas\Filter\Exception\RuntimeException; | ||
use Laminas\Filter\ToEnum; | ||
use LaminasTest\Filter\TestAsset\TestIntBackedEnum; | ||
use LaminasTest\Filter\TestAsset\TestStringBackedEnum; | ||
use LaminasTest\Filter\TestAsset\TestUnitEnum; | ||
use PHPUnit\Framework\TestCase; | ||
use UnitEnum; | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
class ToEnumTest extends TestCase | ||
{ | ||
/** @return array<string, array{0: class-string<UnitEnum>, 1: string|int, 2: UnitEnum|BackedEnum}> */ | ||
public function filterableValuesProvider(): array | ||
{ | ||
return [ | ||
'unit enum' => [TestUnitEnum::class, 'foo', TestUnitEnum::foo], | ||
'backed string enum' => [TestStringBackedEnum::class, 'foo', TestStringBackedEnum::Foo], | ||
'backed integer enum' => [TestIntBackedEnum::class, 2, TestIntBackedEnum::Bar], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider filterableValuesProvider | ||
* @param class-string<UnitEnum> $enumClass | ||
*/ | ||
public function testCanFilterToEnum(string $enumClass, string|int $value, UnitEnum $expectedFilteredValue): void | ||
{ | ||
$filter = new ToEnum($enumClass); | ||
|
||
self::assertSame($expectedFilteredValue, $filter->filter($value)); | ||
} | ||
|
||
/** | ||
* @dataProvider filterableValuesProvider | ||
* @param class-string<UnitEnum> $enumClass | ||
*/ | ||
public function testCanFilterToEnumWithOptions( | ||
string $enumClass, | ||
string|int $value, | ||
UnitEnum $expectedFilteredValue | ||
): void { | ||
$filter = new ToEnum(['enum' => $enumClass]); | ||
|
||
self::assertSame($expectedFilteredValue, $filter->filter($value)); | ||
} | ||
|
||
/** @return array<string, array{0: class-string<UnitEnum>, 1: mixed}> */ | ||
public function unfilterableValuesProvider(): array | ||
{ | ||
return [ | ||
'array' => [TestUnitEnum::class, []], | ||
'float' => [TestUnitEnum::class, 1.1], | ||
'bool' => [TestUnitEnum::class, false], | ||
'unit enum' => [TestUnitEnum::class, 'baz'], | ||
'backed string enum' => [TestStringBackedEnum::class, 'baz'], | ||
'backed integer enum' => [TestIntBackedEnum::class, 3], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider unfilterableValuesProvider | ||
* @param class-string<UnitEnum> $enumClass | ||
*/ | ||
public function testFiltersToNull(string $enumClass, mixed $value): void | ||
{ | ||
$filter = new ToEnum($enumClass); | ||
|
||
self::assertEquals($value, $filter->filter($value)); | ||
} | ||
|
||
public function testThrowsExceptionIfEnumNotSet(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('enum class not set'); | ||
|
||
/** | ||
* @psalm-suppress InvalidArgument | ||
*/ | ||
$filter = new ToEnum([]); | ||
|
||
$filter->filter('foo'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love for this to only accept
class-string<UnitEnum>
:DThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructor arg needs to be
or
(null would also need to be added to the
@param
in this case)The
FilterPluginManager
will justnew
the filter without arguments unless a factory is created (which is pointless).In order for the filter to be available in the plugin manager,
FilterPluginManager
must be updated with entries underfactories
andaliases
The problem is that at this point, PHPUnit will fail CI on PHP 8.0 (Along with Psalm)
I don't see an easy way around this at the moment unless dropping support for PHP 8.0 is an option.
Thoughts @Ocramius ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct! 👍🏻