Completed
Push — master ( 904c19...2c4bed )
by
unknown
10:30
created

AbstractSearch::hasResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Search;
4
5
use \RuntimeException;
6
7
use \Psr\Log\LoggerAwareInterface;
8
use \Psr\Log\LoggerAwareTrait;
9
10
use \Charcoal\Factory\FactoryInterface;
11
12
/**
13
 * A Basic Search Request
14
 *
15
 * Abstract implementation of {@see \Charcoal\Search\SearchInterface}.
16
 */
17
abstract class AbstractSearch implements
18
    SearchInterface,
19
    LoggerAwareInterface
20
{
21
    use LoggerAwareTrait;
22
23
    /**
24
     * The (raw) search results.
25
     *
26
     * @var array
27
     */
28
    protected $results;
29
30
    /**
31
     * Store the factory instance for the current class.
32
     *
33
     * @var FactoryInterface
34
     */
35
    private $modelFactory;
36
37
    /**
38
     * Return a new search object.
39
     *
40
     * @param array|\ArrayAccess $data The class options and dependencies.
41
     */
42
    public function __construct($data)
43
    {
44
        $this->setLogger($data['logger']);
45
        $this->setModelFactory($data['model_factory']);
46
    }
47
48
    /**
49
     * Set an object model factory.
50
     *
51
     * @param  FactoryInterface $factory The model factory, to create objects.
52
     * @return self
53
     */
54
    protected function setModelFactory(FactoryInterface $factory)
55
    {
56
        $this->modelFactory = $factory;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Retrieve the object model factory.
63
     *
64
     * @throws RuntimeException If the model factory was not previously set.
65
     * @return FactoryInterface
66
     */
67
    public function modelFactory()
68
    {
69
        if (!isset($this->modelFactory)) {
70
            throw new RuntimeException(
71
                sprintf('Model Factory is not defined for "%s"', get_class($this))
72
            );
73
        }
74
75
        return $this->modelFactory;
76
    }
77
78
    /**
79
     * Process the search query.
80
     *
81
     * @param  string $keyword       The search term(s).
82
     * @param  array  $searchOptions Additional options.
83
     * @return array|\Traversable The results.
84
     */
85
    abstract public function search($keyword, array $searchOptions = []);
86
87
    /**
88
     * Alias of {@see self::search()}.
89
     *
90
     * A search is always callable.
91
     *
92
     * @param  string $keyword       The search term(s).
93
     * @param  array  $searchOptions Additional options.
94
     * @return array The results.
95
     */
96
    final public function __invoke($keyword, array $searchOptions = [])
97
    {
98
        return $this->search($keyword, $searchOptions);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->search($keyword, $searchOptions); of type array|Traversable adds the type Traversable to the return on line 98 which is incompatible with the return type declared by the interface Charcoal\Search\SearchInterface::__invoke of type array.
Loading history...
99
    }
100
101
    /**
102
     * Retrieve the results from the latest search.
103
     *
104
     * @param  string $resultType The type of results to search. Can be only "raw" for now.
105
     * @throws RuntimeException If this method is called before a search was executed.
106
     * @return array The results from the last search operation.
107
     */
108
    final public function lastResults($resultType = 'raw')
109
    {
110
        if ($this->results === null) {
111
            throw new RuntimeException(
112
                'Search was never performed'
113
            );
114
        }
115
116
        if ($resultType === 'raw') {
117
            return $this->results;
118
        }
119
120
        return [];
121
    }
122
123
    /**
124
     * Determine if the latest search has any results.
125
     *
126
     * @return boolean
127
     */
128
    final public function hasResults()
129
    {
130
        return boolval($this->results);
131
    }
132
133
    /**
134
     * Set the results from a search.
135
     *
136
     * @param  array $results The (raw) search results.
137
     * @return void
138
     */
139
    final protected function setResults(array $results)
140
    {
141
        $this->results = $results;
142
    }
143
}
144