-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow retrieving the enum itself and add a second argument to the "en…
…um()" function to retrieve it's name or value
- Loading branch information
Showing
7 changed files
with
218 additions
and
32 deletions.
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
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
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
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Presta\BehatEvaluator\ExpressionLanguage\Compiler; | ||
|
||
use Presta\BehatEvaluator\ExpressionLanguage\ArgumentGuesser\DateTime\ArgumentGuesserInterface; | ||
|
||
/** | ||
* @phpstan-import-type IntlFormats from ArgumentGuesserInterface | ||
*/ | ||
final class EnumCompiler | ||
{ | ||
public function __invoke(string $enum, string $property = null): string | ||
{ | ||
return ''; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Presta\BehatEvaluator\ExpressionLanguage\Evaluator; | ||
|
||
final class EnumEvaluator | ||
{ | ||
/** | ||
* @param array<string, mixed> $arguments | ||
*/ | ||
public function __invoke(array $arguments, string $enumClass, string $property = null): mixed | ||
{ | ||
try { | ||
$enum = constant($enumClass); | ||
} catch (\Throwable) { | ||
throw new \RuntimeException("\"$enumClass\" is not a valid enum."); | ||
} | ||
|
||
if (!$enum instanceof \UnitEnum) { | ||
$debugType = get_debug_type($enum); | ||
|
||
throw new \RuntimeException("\"$debugType\" is not a valid enum."); | ||
} | ||
|
||
if (null === $property) { | ||
return $enum; | ||
} | ||
|
||
if (!\in_array($property, ['name', 'value'], true)) { | ||
throw new \RuntimeException("You can not get the \"$property\" property of an enum."); | ||
} | ||
|
||
if (!$enum instanceof \BackedEnum) { | ||
throw new \RuntimeException("You can not get the \"$property\" of a UnitEnum."); | ||
} | ||
|
||
return match ($property) { | ||
'value' => $enum->value, | ||
'name' => $enum->name, | ||
}; | ||
} | ||
} |
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
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