aboutsummaryrefslogtreecommitdiff
path: root/src/Search/FTSLogicOp.php
blob: 5b8b7389cd872a3c67d707bc8463a0691e83b5b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php


namespace Micropoly\Search;


class FTSLogicOp extends AbstractFTSExpr
{
    private string $op;
    private AbstractFTSExpr $a;
    private AbstractFTSExpr $b;

    /**
     * FTSLogicOp constructor.
     * @param string $op
     * @param AbstractFTSExpr $a
     * @param AbstractFTSExpr $b
     */
    public function __construct(string $op, AbstractFTSExpr $a, AbstractFTSExpr $b)
    {
        if (!LogicOp::checkOp($op))
            throw new \DomainException("{$op} is not a valid operator");

        $this->op = $op;
        $this->a = $a;
        $this->b = $b;
    }

    private const FTSOPS = [
        LogicOp::OP_AND => "",
        LogicOp::OP_OR => "OR",
    ];

    protected function fts4Query(): string
    {
        assert(isset(self::FTSOPS[$this->op]));
        $ftsop = self::FTSOPS[$this->op];

        return "({$this->a->fts4Query()} {$ftsop} {$this->b->fts4Query()})";
    }

    public function toString(): string
    {
        return "({$this->a->toString()} FTS-{$this->op} {$this->b->toString()})";
    }
}