Adam Pippin
3 years ago
2 changed files with 76 additions and 0 deletions
@ -0,0 +1,75 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Cfnpp\Expression\Token; |
||||
|
|
||||
|
use App\Cfnpp\Expression\Token; |
||||
|
use App\Cfnpp\Expression\TokenLiteral; |
||||
|
|
||||
|
/** |
||||
|
* A boolean literal (true/false). |
||||
|
* |
||||
|
* @author Adam Pippin <hello@adampippin.ca> |
||||
|
*/ |
||||
|
class BooleanLiteral extends TokenLiteral |
||||
|
{ |
||||
|
/** |
||||
|
* Value of this literal. |
||||
|
* @var bool |
||||
|
*/ |
||||
|
protected $value; |
||||
|
|
||||
|
/** |
||||
|
* New boolean literal. |
||||
|
* |
||||
|
* @param bool $value |
||||
|
*/ |
||||
|
public function __construct(bool $value) |
||||
|
{ |
||||
|
$this->value = $value; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the value of this literal. |
||||
|
* |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public function getValue(): bool |
||||
|
{ |
||||
|
return $this->value; |
||||
|
} |
||||
|
|
||||
|
public static function isToken(string $stream): bool |
||||
|
{ |
||||
|
return |
||||
|
(strlen($stream) >= 4 && strtolower(substr($stream, 0, 4)) == 'true') || |
||||
|
(strlen($stream) >= 5 && strtolower(substr($stream, 0, 5)) == 'false'); |
||||
|
} |
||||
|
|
||||
|
public static function getToken(string &$stream): Token |
||||
|
{ |
||||
|
if (strlen($stream) >= 4 && strtolower(substr($stream, 0, 4)) == 'true') |
||||
|
{ |
||||
|
$stream = substr($stream, 4); |
||||
|
return new BooleanLiteral(true); |
||||
|
} |
||||
|
elseif (strlen($stream) >= 5 && strtolower(substr($stream, 0, 5)) == 'false') |
||||
|
{ |
||||
|
$stream = substr($stream, 5); |
||||
|
return new BooleanLiteral(false); |
||||
|
} |
||||
|
throw new \Exception('Unparseable boolean'); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the value of this token. |
||||
|
* |
||||
|
* @param ?\App\Util\GraphNode[] $arguments |
||||
|
* @return \App\Util\GraphNode|Token|scalar|null |
||||
|
*/ |
||||
|
public function execute(?array $arguments = null) |
||||
|
{ |
||||
|
return $this->getValue(); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue