Passed
Branch master (8cb5b2)
by Christopher
04:37
created

AbstractQuery::mustNot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Triadev\Leopard\Business\Dsl\Search. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Triadev\Leopard\Busines\Dsl\Query\Specialized;
8
use Triadev\Leopard\Business\Dsl\Query\TermLevel;
9
use Triadev\Leopard\Business\Dsl\Query\Fulltext;
10
use Triadev\Leopard\Business\Dsl\Query\Geo;
11
use Triadev\Leopard\Business\Dsl\Query\Joining;
12
use Triadev\Leopard\Business\Dsl\Query\InnerHit;
13
14
abstract class AbstractQuery
15
{
16
    /** @var \ONGR\ElasticsearchDSL\Search */
17
    public $search;
18
    
19
    /** @var string */
20
    public $boolState = BoolQuery::MUST;
21
    
22
    /**
23
     * BoolQuery constructor.
24
     * @param Search|null $search
25
     */
26 35
    public function __construct(?Search $search = null)
27
    {
28 35
        $this->search = $search ?: new Search();
29 35
    }
30
    
31
    /**
32
     * To dsl
33
     *
34
     * @return array
35
     */
36 32
    public function toDsl() : array
37
    {
38 32
        return $this->search->toArray();
39
    }
40
    
41
    /**
42
     * Get search
43
     *
44
     * @return Search
45
     */
46 4
    public function getSearch() : Search
47
    {
48 4
        return $this->search;
49
    }
50
    
51
    /**
52
     * Get query
53
     *
54
     * @return BuilderInterface
55
     */
56 11
    public function getQuery() : BuilderInterface
57
    {
58 11
        return $this->search->getQueries();
59
    }
60
    
61
    /**
62
     * Append
63
     *
64
     * @param BuilderInterface $query
65
     * @return AbstractQuery|TermLevel|Fulltext|Geo|\Triadev\Leopard\Business\Dsl\Search|Joining|Specialized|InnerHit
66
     */
67 31
    public function append(BuilderInterface $query) : AbstractQuery
68
    {
69 31
        $this->search->addQuery($query, $this->boolState);
70 31
        return $this;
71
    }
72
    
73
    /**
74
     * Bool state: must
75
     *
76
     * @return AbstractQuery|TermLevel|Fulltext|Geo|\Triadev\Leopard\Business\Dsl\Search|Joining|Specialized|InnerHit
77
     */
78 3
    public function must(): AbstractQuery
79
    {
80 3
        $this->boolState = BoolQuery::MUST;
81 3
        return $this;
82
    }
83
    
84
    /**
85
     * Bool state: must not
86
     *
87
     * @return AbstractQuery|TermLevel|Fulltext|Geo|\Triadev\Leopard\Business\Dsl\Search|Joining|Specialized|InnerHit
88
     */
89 1
    public function mustNot(): AbstractQuery
90
    {
91 1
        $this->boolState = BoolQuery::MUST_NOT;
92 1
        return $this;
93
    }
94
    
95
    /**
96
     * Bool state: should
97
     *
98
     * @return AbstractQuery|TermLevel|Fulltext|Geo|\Triadev\Leopard\Business\Dsl\Search|Joining|Specialized|InnerHit
99
     */
100 1
    public function should(): AbstractQuery
101
    {
102 1
        $this->boolState = BoolQuery::SHOULD;
103 1
        return $this;
104
    }
105
    
106
    /**
107
     * Bool state: filter
108
     *
109
     * @return AbstractQuery|TermLevel|Fulltext|Geo|\Triadev\Leopard\Business\Dsl\Search|Joining|Specialized|InnerHit
110
     */
111 7
    public function filter(): AbstractQuery
112
    {
113 7
        $this->boolState = BoolQuery::FILTER;
114 7
        return $this;
115
    }
116
}
117