Completed
Push — master ( 3a2d29...d0475e )
by Nicolas
02:57
created

lib/Elastica/Query/Fuzzy.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
 * Fuzzy query.
8
 *
9
 * @author Nicolas Ruflin <[email protected]>
10
 *
11
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
12
 */
13
class Fuzzy extends AbstractQuery
14
{
15
    /**
16
     * Construct a fuzzy query.
17
     *
18
     * @param string $fieldName Field name
19
     * @param string $value     String to search for
20
     */
21
    public function __construct($fieldName = null, $value = null)
22
    {
23
        if ($fieldName && $value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $value of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
24
            $this->setField($fieldName, $value);
25
        }
26
    }
27
28
    /**
29
     * Set field for fuzzy query.
30
     *
31
     * @param string $fieldName Field name
32
     * @param string $value     String to search for
33
     *
34
     * @return $this
35
     */
36
    public function setField($fieldName, $value)
37
    {
38
        if (!is_string($value) || !is_string($fieldName)) {
39
            throw new InvalidException('The field and value arguments must be of type string.');
40
        }
41
        if (count($this->getParams()) > 0 && key($this->getParams()) !== $fieldName) {
42
            throw new InvalidException('Fuzzy query can only support a single field.');
43
        }
44
45
        return $this->setParam($fieldName, ['value' => $value]);
46
    }
47
48
    /**
49
     * Set optional parameters on the existing query.
50
     *
51
     * @param string $option option name
52
     * @param mixed  $value  Value of the parameter
53
     *
54
     * @return $this
55
     */
56
    public function setFieldOption($option, $value)
57
    {
58
        //Retrieve the single existing field for alteration.
59
        $params = $this->getParams();
60
        if (count($params) < 1) {
61
            throw new InvalidException('No field has been set');
62
        }
63
        $key = key($params);
64
        $params[$key][$option] = $value;
65
66
        return $this->setParam($key, $params[$key]);
67
    }
68
}
69