Passed
Push — master ( 39f7ea...d023fc )
by Christopher
04:00
created

AbstractQuery::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 2
dl 0
loc 28
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Leopard\Business\Dsl;
3
4
use ONGR\ElasticsearchDSL\BuilderInterface;
5
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
6
use ONGR\ElasticsearchDSL\Search as OngrSearch;
7
use Triadev\Leopard\Busines\Dsl\Query\Specialized;
8
use Triadev\Leopard\Business\Dsl\Query\Compound;
9
use Triadev\Leopard\Business\Dsl\Query\TermLevel;
10
use Triadev\Leopard\Business\Dsl\Query\Fulltext;
11
use Triadev\Leopard\Business\Dsl\Query\Geo;
12
use Triadev\Leopard\Business\Dsl\Query\Joining;
13
use Triadev\Leopard\Business\Dsl\Query\InnerHit;
14
use Triadev\Leopard\Business\Dsl\Search as SearchDsl;
15
use Triadev\Leopard\Facade\Leopard;
16
17
/**
18
 * Class AbstractQuery
19
 * @package Triadev\Leopard\Business\Dsl
20
 *
21
 * @method TermLevel termLevel(\Closure $termLevel)
22
 * @method Fulltext fulltext(\Closure $fulltext)
23
 * @method Geo geo(\Closure $geo)
24
 * @method Compound compound(\Closure $compound)
25
 * @method Joining joining(\Closure $joining)
26
 * @method Specialized specialized(\Closure $specialized)
27
 * @method InnerHit innerHit(\Closure $innerHit)
28
 */
29
abstract class AbstractQuery
30
{
31
    /** @var OngrSearch */
32
    public $search;
33
    
34
    /** @var string */
35
    public $boolState = BoolQuery::MUST;
36
    
37
    /**
38
     * BoolQuery constructor.
39
     * @param OngrSearch|null $search
40
     */
41 36
    public function __construct(?OngrSearch $search = null)
42
    {
43 36
        $this->search = $search ?: new OngrSearch();
44 36
    }
45
    
46
    /**
47
     * To dsl
48
     *
49
     * @return array
50
     */
51 33
    public function toDsl() : array
52
    {
53 33
        return $this->search->toArray();
54
    }
55
    
56
    /**
57
     * Get search
58
     *
59
     * @return OngrSearch
60
     */
61 4
    public function getSearch() : OngrSearch
62
    {
63 4
        return $this->search;
64
    }
65
    
66
    /**
67
     * Get query
68
     *
69
     * @return BuilderInterface
70
     */
71 12
    public function getQuery() : BuilderInterface
72
    {
73 12
        return $this->search->getQueries();
74
    }
75
    
76
    /**
77
     * Append
78
     *
79
     * @param BuilderInterface $query
80
     * @return AbstractQuery|TermLevel|Fulltext|Geo|SearchDsl|Joining|Specialized|InnerHit
81
     */
82 32
    public function append(BuilderInterface $query) : AbstractQuery
83
    {
84 32
        $this->search->addQuery($query, $this->boolState);
85 32
        return $this;
86
    }
87
    
88
    /**
89
     * Bool state: must
90
     *
91
     * @return AbstractQuery|TermLevel|Fulltext|Geo|SearchDsl|Joining|Specialized|InnerHit
92
     */
93 3
    public function must(): AbstractQuery
94
    {
95 3
        $this->boolState = BoolQuery::MUST;
96 3
        return $this;
97
    }
98
    
99
    /**
100
     * Bool state: must not
101
     *
102
     * @return AbstractQuery|TermLevel|Fulltext|Geo|SearchDsl|Joining|Specialized|InnerHit
103
     */
104 1
    public function mustNot(): AbstractQuery
105
    {
106 1
        $this->boolState = BoolQuery::MUST_NOT;
107 1
        return $this;
108
    }
109
    
110
    /**
111
     * Bool state: should
112
     *
113
     * @return AbstractQuery|TermLevel|Fulltext|Geo|SearchDsl|Joining|Specialized|InnerHit
114
     */
115 1
    public function should(): AbstractQuery
116
    {
117 1
        $this->boolState = BoolQuery::SHOULD;
118 1
        return $this;
119
    }
120
    
121
    /**
122
     * Bool state: filter
123
     *
124
     * @return AbstractQuery|TermLevel|Fulltext|Geo|SearchDsl|Joining|Specialized|InnerHit
125
     */
126 8
    public function filter(): AbstractQuery
127
    {
128 8
        $this->boolState = BoolQuery::FILTER;
129 8
        return $this;
130
    }
131
    
132
    /**
133
     * Call
134
     *
135
     * @param string $name
136
     * @param array $arguments
137
     *
138
     * @return AbstractQuery|null
139
     */
140 1
    public function __call(string $name, array $arguments) : ?AbstractQuery
141
    {
142
        $validFunctions = [
143 1
            'termLevel',
144
            'fulltext',
145
            'geo',
146
            'compound',
147
            'joining',
148
            'specialized',
149
            'innerHit'
150
        ];
151
        
152 1
        if (in_array($name, $validFunctions)) {
153 1
            $closure = $arguments[0];
154
            
155 1
            if (is_callable($closure)) {
156
                /** @var Search $search */
157 1
                $search = Leopard::search()->$name($closure);
158
                
159 1
                $this->search->addQuery(
160 1
                    $search->getQuery()
161
                );
162
                
163 1
                return $this;
164
            }
165
        }
166
        
167
        return null;
168
    }
169
}
170