1: <?php
2:
3: namespace BN\Compiler\Scanner\Converter;
4:
5: use BN\Compiler\Token\Token;
6: use BN\Compiler\Token\TokenType;
7: use BN\Compiler\Scanner\LexemeConverter;
8:
9: class LexemeToSignedNumber implements LexemeConverter
10: {
11: public function canConvertLexeme($lexeme)
12: {
13: return $this->isStringNumeric($lexeme)
14: || $this->numberWithUnarySign($lexeme);
15: }
16:
17: private function isStringNumeric($lexeme)
18: {
19: return is_string($lexeme) && is_numeric($lexeme);
20: }
21:
22: private function numberWithUnarySign($lexeme)
23: {
24: $afterSign = substr($lexeme, 1);
25: return $this->startWithUnarySign($lexeme)
26: && $this->isStringNumeric($afterSign)
27: && $this->noUnarySign($afterSign);
28: }
29:
30: private function noUnarySign($lexeme)
31: {
32: return !$this->startWithUnarySign($lexeme);
33: }
34:
35: private function startWithUnarySign($lexeme)
36: {
37: return in_array($lexeme[0], array('+', '-'), true);
38: }
39:
40: public function convertLexeme($lexeme)
41: {
42: return new Token(TokenType::NUMBER, $lexeme);
43: }
44: }
45: