Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
NullComparison
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getSqlExpression
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Finder\Output;
6
7use Cosray\Context;
8use Cosray\Exception\ParserOutputException;
9use Cosray\Finder\Input\Token;
10use Cosray\Finder\Input\TokenType;
11
12final readonly class NullComparison extends Expression implements Output
13{
14    public function __construct(
15        private Token $left,
16        private Token $operator,
17        private Token $right,
18        private Context $context,
19        private array $builtins,
20    ) {}
21
22    public function get(): string
23    {
24        switch ($this->operator->type) {
25            case TokenType::Equal:
26                return $this->getSqlExpression(true);
27            case TokenType::Unequal:
28                return $this->getSqlExpression(false);
29        }
30
31        throw new ParserOutputException(
32            $this->operator,
33            'Only equal (=) or unequal (!=) operators are allowed in queries with an null value.',
34        );
35    }
36
37    private function getSqlExpression(bool $equal): string
38    {
39        return sprintf(
40            '%s %s %s',
41            $this->getOperand($this->left, $this->context->db, $this->builtins, $this->context),
42            $equal ? 'IS' : 'IS NOT',
43            $this->getOperand($this->right, $this->context->db, $this->builtins, $this->context),
44        );
45    }
46}