-
-
Notifications
You must be signed in to change notification settings - Fork 55
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 handling for enum as haystack #168
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,18 @@ | |
|
||
namespace Laminas\Validator; | ||
|
||
use BackedEnum; | ||
use RecursiveArrayIterator; | ||
use RecursiveIteratorIterator; | ||
use UnitEnum; | ||
|
||
use function array_map; | ||
use function in_array; | ||
use function is_bool; | ||
use function is_float; | ||
use function is_int; | ||
use function is_string; | ||
use function is_subclass_of; | ||
|
||
class InArray extends AbstractValidator | ||
{ | ||
|
@@ -46,6 +50,13 @@ class InArray extends AbstractValidator | |
*/ | ||
protected $haystack; | ||
|
||
/** | ||
* Enum of possible values | ||
* | ||
* @var class-string<UnitEnum>|null | ||
*/ | ||
protected $enum; | ||
|
||
/** | ||
* Type of strict check to be used. Due to "foo" == 0 === TRUE with in_array when strict = false, | ||
* an option has been added to prevent this. When $strict = 0/false, the most | ||
|
@@ -66,7 +77,7 @@ class InArray extends AbstractValidator | |
/** | ||
* Returns the haystack option | ||
* | ||
* @return mixed | ||
* @return array | ||
* @throws Exception\RuntimeException If haystack option is not set. | ||
*/ | ||
public function getHaystack() | ||
|
@@ -80,7 +91,7 @@ public function getHaystack() | |
/** | ||
* Sets the haystack option | ||
* | ||
* @param mixed $haystack | ||
* @param array $haystack | ||
* @return $this Provides a fluent interface | ||
*/ | ||
public function setHaystack(array $haystack) | ||
|
@@ -89,6 +100,30 @@ public function setHaystack(array $haystack) | |
return $this; | ||
} | ||
|
||
/** | ||
* @return class-string<UnitEnum>|null | ||
*/ | ||
public function getEnum(): ?string | ||
{ | ||
return $this->enum; | ||
} | ||
|
||
/** | ||
* Set the enum option | ||
* | ||
* @param class-string<UnitEnum> $enum | ||
* @return $this Provides a fluent interface | ||
*/ | ||
public function setEnum(string $enum): self | ||
{ | ||
if (! is_subclass_of($enum, UnitEnum::class)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can skip this particular check: |
||
throw new Exception\RuntimeException('enum has invalid type'); | ||
} | ||
|
||
$this->enum = $enum; | ||
return $this; | ||
} | ||
Comment on lines
+117
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this method should call |
||
|
||
/** | ||
* Returns the strict option | ||
* | ||
|
@@ -169,6 +204,35 @@ public function setRecursive($recursive) | |
*/ | ||
public function isValid($value) | ||
{ | ||
$enum = $this->getEnum(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we make |
||
if (is_string($enum)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should just check for |
||
if (is_subclass_of($enum, 'BackedEnum')) { | ||
$enumHaystack = array_map( | ||
static fn (BackedEnum $case) => $case->value, | ||
$enum::cases() | ||
); | ||
|
||
if (in_array($value, $enumHaystack, (bool) $this->strict)) { | ||
return true; | ||
} | ||
|
||
$this->error(self::NOT_IN_ARRAY); | ||
return false; | ||
} | ||
|
||
$enumHaystack = array_map( | ||
static fn (UnitEnum $case): string => $case->name, | ||
$enum::cases() | ||
); | ||
|
||
if (in_array($value, $enumHaystack, (bool) $this->strict)) { | ||
return true; | ||
} | ||
|
||
$this->error(self::NOT_IN_ARRAY); | ||
return false; | ||
} | ||
|
||
// we create a copy of the haystack in case we need to modify it | ||
$haystack = $this->getHaystack(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Validator\TestAsset\Enum; | ||
|
||
enum TestBackedIntEnum: int | ||
{ | ||
case Foo = 1; | ||
case Bar = 2; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Validator\TestAsset\Enum; | ||
|
||
enum TestBackedStringEnum: string | ||
{ | ||
case Foo = 'foo'; | ||
case Bar = 'bar'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Validator\TestAsset\Enum; | ||
|
||
enum TestUnitEnum | ||
{ | ||
case foo; | ||
case bar; | ||
} |
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.
Let's make this
private
before it spreads :D