Completed
Push — master ( d3ac62...0cb203 )
by Federico
02:08
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
3
namespace Elastica\Query;
4
5
use Elastica\Exception\InvalidException;
6
7
/**
8
 * Fuzzy query.
9
 *
10
 * @author Nicolas Ruflin <[email protected]>
11
 *
12
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
13
 */
14
class Fuzzy extends AbstractQuery
15
{
16
    /**
17
     * Construct a fuzzy query.
18
     *
19
     * @param string $fieldName Field name
20
     * @param string $value     String to search for
21
     */
22
    public function __construct($fieldName = null, $value = null)
23
    {
24
        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...
25
            $this->setField($fieldName, $value);
26
        }
27
    }
28
29
    /**
30
     * Set field for fuzzy query.
31
     *
32
     * @param string $fieldName Field name
33
     * @param string $value     String to search for
34
     *
35
     * @return $this
36
     */
37
    public function setField($fieldName, $value)
38
    {
39
        if (!is_string($value) || !is_string($fieldName)) {
40
            throw new InvalidException('The field and value arguments must be of type string.');
41
        }
42
        if (count($this->getParams()) > 0 && key($this->getParams()) !== $fieldName) {
43
            throw new InvalidException('Fuzzy query can only support a single field.');
44
        }
45
46
        return $this->setParam($fieldName, ['value' => $value]);
47
    }
48
49
    /**
50
     * Set optional parameters on the existing query.
51
     *
52
     * @param string $option option name
53
     * @param mixed  $value  Value of the parameter
54
     *
55
     * @return $this
56
     */
57
    public function setFieldOption($option, $value)
58
    {
59
        //Retrieve the single existing field for alteration.
60
        $params = $this->getParams();
61
        if (count($params) < 1) {
62
            throw new InvalidException('No field has been set');
63
        }
64
        $key = key($params);
65
        $params[$key][$option] = $value;
66
67
        return $this->setParam($key, $params[$key]);
68
    }
69
}
70