-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Complete baking of enums. * Update src/Command/EnumCommand.php Co-authored-by: othercorey <[email protected]> * Fix up index/view for enums. * Fix up index/view for enums. * Switch key/value, use CamelCase * Stricter validation * Update src/Command/ModelCommand.php Co-authored-by: Mark Story <[email protected]> * Cleanup * Cleanup * Add more tests. --------- Co-authored-by: othercorey <[email protected]> Co-authored-by: Mark Story <[email protected]>
- Loading branch information
1 parent
b5f106f
commit c7cb269
Showing
15 changed files
with
468 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Bake\Utility\Model; | ||
|
||
use InvalidArgumentException; | ||
|
||
enum EnumParser | ||
{ | ||
/** | ||
* @param string|null $casesString | ||
* @param bool $int | ||
* @return array<string, int|string> | ||
*/ | ||
public static function parseCases(?string $casesString, bool $int): array | ||
{ | ||
if ($casesString === null || $casesString === '') { | ||
return []; | ||
} | ||
|
||
$enumCases = explode(',', $casesString); | ||
|
||
$definition = []; | ||
foreach ($enumCases as $k => $enumCase) { | ||
$case = $value = trim($enumCase); | ||
if (str_contains($case, ':')) { | ||
$value = trim(mb_substr($case, strpos($case, ':') + 1)); | ||
$case = mb_substr($case, 0, strpos($case, ':')); | ||
} elseif ($int) { | ||
$value = $k; | ||
} | ||
|
||
if (!preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $case)) { | ||
throw new InvalidArgumentException(sprintf('`%s` is not a valid enum case', $case)); | ||
} | ||
if (is_string($value) && str_contains($value, '\'')) { | ||
throw new InvalidArgumentException(sprintf('`%s` value cannot contain `\'` character', $case)); | ||
} | ||
|
||
$definition[$case] = $int ? (int)$value : $value; | ||
} | ||
|
||
return $definition; | ||
} | ||
} |
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
Oops, something went wrong.