-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enums allow to avoid having to come up with values to assign for each name (unless you use a backed enum), and will provide more type safety.
- Loading branch information
Showing
5 changed files
with
99 additions
and
4 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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Common\Lexer; | ||
|
||
use Doctrine\Common\Lexer\AbstractLexer; | ||
|
||
use function in_array; | ||
use function is_numeric; | ||
use function is_string; | ||
|
||
/** @extends AbstractLexer<TokenType> */ | ||
class EnumLexer extends AbstractLexer | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function getCatchablePatterns() | ||
{ | ||
return [ | ||
'=|<|>', | ||
'[a-z]+', | ||
'\d+', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function getNonCatchablePatterns() | ||
{ | ||
return [ | ||
'\s+', | ||
'(.)', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function getType(&$value) | ||
{ | ||
if (is_numeric($value)) { | ||
$value = (int) $value; | ||
|
||
return TokenType::INT; | ||
} | ||
|
||
if (in_array($value, ['=', '<', '>'])) { | ||
return TokenType::OPERATOR; | ||
} | ||
|
||
if (is_string($value)) { | ||
return TokenType::STRING; | ||
} | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Common\Lexer; | ||
|
||
enum TokenType | ||
{ | ||
case INT; | ||
case OPERATOR; | ||
case STRING; | ||
} |