Completed
Push — master ( 4cdb71...eee063 )
by Nicolas
02:28
created

lib/Elastica/Query/SpanNear.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elastica\Query;
3
4
use Elastica\Exception\InvalidException;
5
6
/**
7
 * SpanNear query.
8
 *
9
 * @author Marek Hernik <[email protected]>
10
 *
11
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html
12
 */
13
class SpanNear extends AbstractSpanQuery
14
{
15
    /**
16
     * Constructs a SpanNear query object.
17
     *
18
     * @param AbstractSpanQuery[] $clauses OPTIONAL
19
     * @param int $slop OPTIONAL maximum proximity
20
     * @param bool $inOrder OPTIONAL true if order of searched clauses is important
21
     */
22
    public function __construct($clauses = [], $slop = 1, $inOrder = false)
23
    {
24 View Code Duplication
        if (!empty($clauses)) {
25
            foreach ($clauses as $clause) {
26
                if (!is_subclass_of($clause, AbstractSpanQuery::class)) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Elastica\Query\AbstractSpanQuery::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
27
                    throw new InvalidException(
28
                        'Invalid parameter. Has to be array or instance of Elastica\Query\SpanQuery'
29
                    );
30
                }
31
            }
32
        }
33
        $this->setParams(['clauses' => $clauses]);
34
        $this->setSlop($slop);
35
        $this->setInOrder($inOrder);
36
    }
37
38
    /**
39
     * @param int $slop
40
     */
41
    public function setSlop($slop)
42
    {
43
        $this->setParam('slop', $slop);
44
    }
45
46
    /**
47
     * @param bool $inOrder
48
     */
49
    public function setInOrder($inOrder)
50
    {
51
        $this->setParam('in_order', $inOrder);
52
    }
53
54
    /**
55
     * Add clause part to query.
56
     *
57
     * @param AbstractSpanQuery $clause
58
     *
59
     * @throws InvalidException If not valid query
60
     *
61
     * @return $this
62
     */
63 View Code Duplication
    public function addClause($clause)
64
    {
65
        if (!is_subclass_of($clause, AbstractSpanQuery::class)) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Elastica\Query\AbstractSpanQuery::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
66
            throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\SpanQuery');
67
        }
68
69
        return $this->addParam('clauses', $clause);
70
    }
71
}