Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.48% |
19 / 21 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Token | |
90.48% |
19 / 21 |
|
75.00% |
3 / 4 |
10.09 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromList | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| len | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| transformList | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
6.22 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Finder\Input; |
| 6 | |
| 7 | use Celema\Quma\Database; |
| 8 | use Cosray\Exception\ParserException; |
| 9 | |
| 10 | readonly class Token |
| 11 | { |
| 12 | /** |
| 13 | * @param list<string> $items The unquoted members of a list token, for |
| 14 | * outputs that cannot use the SQL tuple in |
| 15 | * `$lexeme` because they build their own literals. |
| 16 | */ |
| 17 | public function __construct( |
| 18 | public TokenGroup $group, |
| 19 | public TokenType $type, |
| 20 | public int $position, |
| 21 | public string $lexeme, |
| 22 | private ?int $length = null, |
| 23 | public array $items = [], |
| 24 | ) {} |
| 25 | |
| 26 | /** @param array<Token> $list */ |
| 27 | public static function fromList( |
| 28 | TokenGroup $group, |
| 29 | TokenType $type, |
| 30 | int $position, |
| 31 | array $list, |
| 32 | int $length, |
| 33 | Database $db, |
| 34 | ): self { |
| 35 | return new self( |
| 36 | $group, |
| 37 | $type, |
| 38 | $position, |
| 39 | self::transformList($list, $db), |
| 40 | $length, |
| 41 | array_values(array_map(static fn(Token $item): string => $item->lexeme, $list)), |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | public function len(): int |
| 46 | { |
| 47 | return $this->length ?: strlen($this->lexeme); |
| 48 | } |
| 49 | |
| 50 | /** @param array<Token> $list */ |
| 51 | private static function transformList(array $list, Database $db): string |
| 52 | { |
| 53 | $result = []; |
| 54 | $type = null; |
| 55 | |
| 56 | foreach ($list as $item) { |
| 57 | if ($type === null) { |
| 58 | $type = $item->type; |
| 59 | } else { |
| 60 | if ($type !== $item->type) { |
| 61 | throw new ParserException('Invalid query: mixed list item types'); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if ($type === TokenType::String || $type === TokenType::Number) { |
| 66 | $result[] = $db->quote($item->lexeme); |
| 67 | } else { |
| 68 | throw new ParserException('Invalid query: token type not supported in list'); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return '(' . implode(', ', $result) . ')'; |
| 73 | } |
| 74 | } |