AbsFunction.php
9 months ago
AvgFunction.php
9 months ago
BitAndFunction.php
9 months ago
BitOrFunction.php
9 months ago
ConcatFunction.php
9 months ago
CountFunction.php
9 months ago
CurrentDateFunction.php
9 months ago
CurrentTimeFunction.php
9 months ago
CurrentTimestampFunction.php
9 months ago
DateAddFunction.php
9 months ago
DateDiffFunction.php
9 months ago
DateSubFunction.php
9 months ago
FunctionNode.php
9 months ago
IdentityFunction.php
9 months ago
LengthFunction.php
9 months ago
LocateFunction.php
9 months ago
LowerFunction.php
9 months ago
MaxFunction.php
9 months ago
MinFunction.php
9 months ago
ModFunction.php
9 months ago
SizeFunction.php
9 months ago
SqrtFunction.php
9 months ago
SubstringFunction.php
9 months ago
SumFunction.php
9 months ago
TrimFunction.php
9 months ago
UpperFunction.php
9 months ago
index.php
9 months ago
TrimFunction.php
81 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Query\AST\Functions; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Platforms\TrimMode; |
| 6 | use MailPoetVendor\Doctrine\ORM\Query\AST\Node; |
| 7 | use MailPoetVendor\Doctrine\ORM\Query\Parser; |
| 8 | use MailPoetVendor\Doctrine\ORM\Query\SqlWalker; |
| 9 | use MailPoetVendor\Doctrine\ORM\Query\TokenType; |
| 10 | use function assert; |
| 11 | use function strcasecmp; |
| 12 | class TrimFunction extends FunctionNode |
| 13 | { |
| 14 | public $leading; |
| 15 | public $trailing; |
| 16 | public $both; |
| 17 | public $trimChar = \false; |
| 18 | public $stringPrimary; |
| 19 | public function getSql(SqlWalker $sqlWalker) |
| 20 | { |
| 21 | $stringPrimary = $sqlWalker->walkStringPrimary($this->stringPrimary); |
| 22 | $platform = $sqlWalker->getConnection()->getDatabasePlatform(); |
| 23 | $trimMode = $this->getTrimMode(); |
| 24 | if ($this->trimChar !== \false) { |
| 25 | return $platform->getTrimExpression($stringPrimary, $trimMode, $platform->quoteStringLiteral($this->trimChar)); |
| 26 | } |
| 27 | return $platform->getTrimExpression($stringPrimary, $trimMode); |
| 28 | } |
| 29 | public function parse(Parser $parser) |
| 30 | { |
| 31 | $lexer = $parser->getLexer(); |
| 32 | $parser->match(TokenType::T_IDENTIFIER); |
| 33 | $parser->match(TokenType::T_OPEN_PARENTHESIS); |
| 34 | $this->parseTrimMode($parser); |
| 35 | if ($lexer->isNextToken(TokenType::T_STRING)) { |
| 36 | $parser->match(TokenType::T_STRING); |
| 37 | assert($lexer->token !== null); |
| 38 | $this->trimChar = $lexer->token->value; |
| 39 | } |
| 40 | if ($this->leading || $this->trailing || $this->both || $this->trimChar !== \false) { |
| 41 | $parser->match(TokenType::T_FROM); |
| 42 | } |
| 43 | $this->stringPrimary = $parser->StringPrimary(); |
| 44 | $parser->match(TokenType::T_CLOSE_PARENTHESIS); |
| 45 | } |
| 46 | private function getTrimMode() : int |
| 47 | { |
| 48 | if ($this->leading) { |
| 49 | return TrimMode::LEADING; |
| 50 | } |
| 51 | if ($this->trailing) { |
| 52 | return TrimMode::TRAILING; |
| 53 | } |
| 54 | if ($this->both) { |
| 55 | return TrimMode::BOTH; |
| 56 | } |
| 57 | return TrimMode::UNSPECIFIED; |
| 58 | } |
| 59 | private function parseTrimMode(Parser $parser) : void |
| 60 | { |
| 61 | $lexer = $parser->getLexer(); |
| 62 | assert($lexer->lookahead !== null); |
| 63 | $value = $lexer->lookahead->value; |
| 64 | if (strcasecmp('leading', $value) === 0) { |
| 65 | $parser->match(TokenType::T_LEADING); |
| 66 | $this->leading = \true; |
| 67 | return; |
| 68 | } |
| 69 | if (strcasecmp('trailing', $value) === 0) { |
| 70 | $parser->match(TokenType::T_TRAILING); |
| 71 | $this->trailing = \true; |
| 72 | return; |
| 73 | } |
| 74 | if (strcasecmp('both', $value) === 0) { |
| 75 | $parser->match(TokenType::T_BOTH); |
| 76 | $this->both = \true; |
| 77 | return; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |