Issues (74)

Query/Suggest/AbstractSuggestQuery.php (2 issues)

1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Query\Suggest;
4
5
use Mdiyakov\DoctrineSolrBundle\Exception\SuggestQueryException;
6
use Mdiyakov\DoctrineSolrBundle\Query\Hydrator\SuggestQueryHydrator;
7
use Mdiyakov\DoctrineSolrBundle\Query\Suggest\Solarium\Query;
8
use Mdiyakov\DoctrineSolrBundle\Schema\Schema;
9
use Solarium\Client;
10
11
abstract class AbstractSuggestQuery
12
{
13
    const COUNT_DEFAULT = 10;
14
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
20
    /**
21
     * @var Schema
22
     */
23
    private $schema;
24
25
    /**
26
     * @var string
27
     */
28
    private $term;
29
30
    /**
31
     * @var string[]
32
     */
33
    private $suggesters = [];
34
35
    /**
36
     * @var int
37
     */
38
    private $count;
39
40
    /**
41
     * @param Client $client
42
     * @param Schema $schema
43
     */
44
    public function __construct(Client $client, Schema $schema)
45
    {
46
        $this->client = $client;
47
        $this->schema = $schema;
48
    }
49
50
    /**
51
     * @param string $term
52
     */
53
    public function setTerm($term)
54
    {
55
        $this->term = $term;
56
    }
57
58
    /**
59
     * @param string $entityFieldName
60
     * @throws SuggestQueryException
61
     */
62
    public function addField($entityFieldName)
63
    {
64
        $field = $this->schema->getFieldByEntityFieldName($entityFieldName);
65
66
        if (!$field->getSuggester()) {
67
            throw new SuggestQueryException(
68
                sprintf(
69
                    'Schema "%s"  does not support a suggestion by field %s',
70
                    $this->schema->getName(),
71
                    $entityFieldName
72
                )
73
            );
74
        }
75
76
        $this->suggesters[] = $field->getSuggester();
77
    }
78
79
    /**
80
     * @return \Solarium\Core\Query\Result\ResultInterface
81
     * @throws SuggestQueryException
82
     */
83
    public function suggest()
84
    {
85
        if (!$this->term) {
86
            throw new SuggestQueryException('Search term is not specified');
87
        }
88
89
        if (!$this->suggesters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->suggesters of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
90
            throw new SuggestQueryException('No entity field is specified for suggestion');
91
        }
92
93
        $solrQuery = new Query();
94
        $solrQuery->setQuery($this->term);
95
96
        $this->initDiscriminatorConditions($solrQuery);
97
98
        foreach ($this->suggesters as $suggesterName) {
99
            $solrQuery->addDictionary($suggesterName);
100
        }
101
102
        $solrQuery->setCount($this->getCount());
103
104
        /** @var Solarium\Result\Result $result */
105
        $result = $this->client->execute($solrQuery);
106
107
        $hydrator = new SuggestQueryHydrator($this->schema);
108
109
        return $hydrator->hydrate($result);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $hydrator->hydrate($result) returns the type Mdiyakov\DoctrineSolrBun...y\Suggest\Result\Result which is incompatible with the documented return type Solarium\Core\Query\Result\ResultInterface.
Loading history...
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function getCount()
116
    {
117
        return $this->count;
118
    }
119
120
    /**
121
     * @param int $count
122
     */
123
    public function setCount($count)
124
    {
125
        $this->count = $count;
126
    }
127
128
    /**
129
     * @param Query $solrQuery
130
     */
131
    abstract protected function initDiscriminatorConditions(Query $solrQuery);
132
133
    /**
134
     * @return Schema
135
     */
136
    public function getSchema()
137
    {
138
        return $this->schema;
139
    }
140
141
    /**
142
     * @return AbstractSuggestQuery
143
     */
144
    public function reset()
145
    {
146
        $this->suggesters = [];
147
        $this->term = null;
148
        $this->count = self::COUNT_DEFAULT;
149
150
        return $this;
151
    }
152
}