Completed
Push — master ( 16a564...d98303 )
by
unknown
14s
created

src/Search/Queries/SearchQuery.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 SilverStripe\FullTextSearch\Search\Queries;
4
5
use SilverStripe\View\ViewableData;
6
use stdClass;
7
8
/**
9
 * Represents a search query
10
 *
11
 * API very much still in flux.
12
 */
13
class SearchQuery extends ViewableData
14
{
15
    public static $missing = null;
16
    public static $present = null;
17
18
    public static $default_page_size = 10;
19
20
    /** These are public, but only for index & variant access - API users should not manually access these */
21
22
    public $search = [];
23
24
    public $classes = [];
25
26
    public $require = [];
27
    public $exclude = [];
28
29
    protected $start = 0;
30
    protected $limit = -1;
31
32
    /** These are the API functions */
33
34
    public function __construct()
35
    {
36
        if (self::$missing === null) {
37
            self::$missing = new stdClass();
38
        }
39
        if (self::$present === null) {
40
            self::$present = new stdClass();
41
        }
42
    }
43
44
    /**
45
     * @param string $text   Search terms. Exact format (grouping, boolean expressions, etc.) depends on
46
     *                       the search implementation.
47
     * @param array  $fields Limits the search to specific fields (using composite field names)
48
     * @param array  $boost  Map of composite field names to float values. The higher the value,
49
     *                       the more important the field gets for relevancy.
50
     */
51 View Code Duplication
    public function search($text, $fields = null, $boost = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $this->search[] = [
54
            'text' => $text,
55
            'fields' => $fields ? (array) $fields : null,
56
            'boost' => $boost,
57
            'fuzzy' => false
58
        ];
59
    }
60
61
    /**
62
     * Similar to {@link search()}, but uses stemming and other similarity algorithms
63
     * to find the searched terms. For example, a term "fishing" would also likely find results
64
     * containing "fish" or "fisher". Depends on search implementation.
65
     *
66
     * @param string $text   See {@link search()}
67
     * @param array  $fields See {@link search()}
68
     * @param array  $boost  See {@link search()}
69
     */
70 View Code Duplication
    public function fuzzysearch($text, $fields = null, $boost = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $this->search[] = [
73
            'text' => $text,
74
            'fields' => $fields ? (array) $fields : null,
75
            'boost' => $boost,
76
            'fuzzy' => true
77
        ];
78
    }
79
80
    public function inClass($class, $includeSubclasses = true)
81
    {
82
        $this->classes[] = [
83
            'class' => $class,
84
            'includeSubclasses' => $includeSubclasses
85
        ];
86
    }
87
88
    /**
89
     * Similar to {@link search()}, but typically used to further narrow down
90
     * based on other facets which don't influence the field relevancy.
91
     *
92
     * @param string $field  Composite name of the field
93
     * @param mixed  $values Scalar value, array of values, or an instance of SearchQuery_Range
94
     */
95
    public function filter($field, $values)
96
    {
97
        $requires = isset($this->require[$field]) ? $this->require[$field] : [];
98
        $values = is_array($values) ? $values : [$values];
99
        $this->require[$field] = array_merge($requires, $values);
100
    }
101
102
    /**
103
     * Excludes results which match these criteria, inverse of {@link filter()}.
104
     *
105
     * @param string $field
106
     * @param mixed $values
107
     */
108
    public function exclude($field, $values)
109
    {
110
        $excludes = isset($this->exclude[$field]) ? $this->exclude[$field] : [];
111
        $values = is_array($values) ? $values : [$values];
112
        $this->exclude[$field] = array_merge($excludes, $values);
113
    }
114
115
    public function start($start)
116
    {
117
        $this->start = $start;
118
    }
119
120
    public function limit($limit)
121
    {
122
        $this->limit = $limit;
123
    }
124
125
    public function page($page)
126
    {
127
        $this->start = $page * self::$default_page_size;
128
        $this->limit = self::$default_page_size;
129
    }
130
131
    public function isfiltered()
132
    {
133
        return $this->search || $this->classes || $this->require || $this->exclude;
134
    }
135
136
    public function __toString()
137
    {
138
        return "Search Query\n";
139
    }
140
}
141