summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/Filter/QueryString.php
blob: e1bb533dba06d3057a00ff49305aa6f47dd85f87 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

namespace ipl\Web\Filter;

use InvalidArgumentException;
use ipl\Stdlib\Filter;

final class QueryString
{
    /** @var string Emitted for every completely parsed condition */
    const ON_CONDITION = Parser::ON_CONDITION;

    /** @var string Emitted for every completely parsed chain */
    const ON_CHAIN = Parser::ON_CHAIN;

    /**
     * This class is only a factory / helper
     */
    private function __construct()
    {
    }

    /**
     * Derive a rule from the given query string
     *
     * @param string $string
     *
     * @return Parser
     */
    public static function fromString($string)
    {
        return new Parser($string);
    }

    /**
     * Derive a rule from the given query string
     *
     * @param string $string
     *
     * @return Filter\Rule
     */
    public static function parse($string)
    {
        return (new Parser($string))->parse();
    }

    /**
     * Assemble a query string for the given rule
     *
     * @param Filter\Rule $rule
     *
     * @return string
     */
    public static function render(Filter\Rule $rule)
    {
        return (new Renderer($rule))->render();
    }

    /**
     * Get the symbol associated with the given rule
     *
     * @param Filter\Rule $rule
     *
     * @return string
     */
    public static function getRuleSymbol(Filter\Rule $rule)
    {
        switch (true) {
            case $rule instanceof Filter\Unlike:
                return '!~';
            case $rule instanceof Filter\Unequal:
                return '!=';
            case $rule instanceof Filter\Like:
                return '~';
            case $rule instanceof Filter\Equal:
                return '=';
            case $rule instanceof Filter\GreaterThan:
                return '>';
            case $rule instanceof Filter\LessThan:
                return '<';
            case $rule instanceof Filter\GreaterThanOrEqual:
                return '>=';
            case $rule instanceof Filter\LessThanOrEqual:
                return '<=';
            case $rule instanceof Filter\All:
                return '&';
            case $rule instanceof Filter\Any:
            case $rule instanceof Filter\None:
                return '|';
            default:
                throw new InvalidArgumentException('Unknown rule type provided');
        }
    }
}