1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace leinonen\Yii2Algolia\ActiveRecord; |
4
|
|
|
|
5
|
|
|
use leinonen\Yii2Algolia\AlgoliaManager; |
6
|
|
|
use Yii; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Simple trait that implements methods needed for the SearchableInterface + couple helpers for dealing with ActiveRecords. |
10
|
|
|
*/ |
11
|
|
|
trait Searchable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @see \yii\base\ArrayableTrait::toArray() |
15
|
|
|
* |
16
|
|
|
* @param array $fields |
17
|
|
|
* @param array $expand |
18
|
|
|
* @param bool $recursive |
19
|
|
|
*/ |
20
|
|
|
abstract public function toArray(array $fields = [], array $expand = [], $recursive = true); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @see \yii\db\BaseActiveRecord::getPrimaryKey() |
24
|
|
|
* |
25
|
|
|
* @param bool $asArray |
26
|
|
|
*/ |
27
|
|
|
abstract public function getPrimaryKey($asArray = false); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* An array of indices that this model uses. If none specified |
31
|
|
|
* The name of the model will be used as the index. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
6 |
|
public function indices() |
36
|
|
|
{ |
37
|
6 |
|
return []; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns an array of indices for this model. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
7 |
|
public function getIndices() |
46
|
|
|
{ |
47
|
7 |
|
$indices = $this->indices(); |
48
|
|
|
|
49
|
7 |
|
if (empty($indices)) { |
50
|
6 |
|
$className = (new \ReflectionClass($this))->getShortName(); |
51
|
|
|
|
52
|
6 |
|
return [$className]; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return $indices; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the model in algolia friendly array form. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
7 |
|
public function getAlgoliaRecord() |
64
|
|
|
{ |
65
|
7 |
|
$record = $this->toArray(); |
66
|
|
|
|
67
|
7 |
|
return $record; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns an unique identifier for the Model. |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
1 |
|
public function getObjectID() |
76
|
|
|
{ |
77
|
1 |
|
return $this->getPrimaryKey(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @see AlgoliaManager::pushToIndices() |
82
|
|
|
* |
83
|
1 |
|
* @return array |
84
|
|
|
*/ |
85
|
1 |
|
public function index() |
86
|
|
|
{ |
87
|
1 |
|
$manager = $this->getAlgoliaManager(); |
88
|
|
|
|
89
|
|
|
return $manager->pushToIndices($this); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @see AlgoliaManager::removeFromIndices() |
94
|
|
|
* |
95
|
1 |
|
* @return array |
96
|
|
|
*/ |
97
|
1 |
|
public function removeFromIndices() |
98
|
|
|
{ |
99
|
1 |
|
$manager = $this->getAlgoliaManager(); |
100
|
1 |
|
|
101
|
|
|
return $manager->removeFromIndices($this); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
1 |
|
* @see AlgoliaManager::updateInIndices() |
106
|
|
|
* |
107
|
1 |
|
* @return array |
108
|
|
|
*/ |
109
|
1 |
|
public function updateInIndices() |
110
|
1 |
|
{ |
111
|
|
|
$manager = $this->getAlgoliaManager(); |
112
|
|
|
|
113
|
|
|
return $manager->updateInIndices($this); |
114
|
|
|
} |
115
|
1 |
|
|
116
|
|
|
/** |
117
|
1 |
|
* @see AlgoliaManager::reindex() |
118
|
|
|
* |
119
|
1 |
|
* @return array |
120
|
1 |
|
*/ |
121
|
|
|
public static function reindex() |
122
|
|
|
{ |
123
|
|
|
$manager = static::getAlgoliaManager(); |
124
|
|
|
|
125
|
1 |
|
return $manager->reindex(__CLASS__); |
126
|
|
|
} |
127
|
1 |
|
|
128
|
|
|
/** |
129
|
1 |
|
* @see AlgoliaManager::clearIndices() |
130
|
1 |
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public static function clearIndices() |
134
|
|
|
{ |
135
|
|
|
$manager = static::getAlgoliaManager(); |
136
|
|
|
|
137
|
|
|
return $manager->clearIndices(__CLASS__); |
138
|
5 |
|
} |
139
|
|
|
|
140
|
5 |
|
/** |
141
|
|
|
* @see AlgoliaManager::search() |
142
|
|
|
* |
143
|
|
|
* @param string $query |
144
|
|
|
* @param null|array $searchParameters |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public static function search($query, array $searchParameters = null) |
149
|
|
|
{ |
150
|
|
|
$manager = static::getAlgoliaManager(); |
151
|
|
|
|
152
|
|
|
return $manager->search(__CLASS__, $query, $searchParameters); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns the AlgoliaManager Instance. |
157
|
|
|
* |
158
|
|
|
* @return AlgoliaManager |
159
|
|
|
* @throws \yii\base\InvalidConfigException |
160
|
|
|
*/ |
161
|
|
|
private static function getAlgoliaManager() |
162
|
|
|
{ |
163
|
|
|
return Yii::$container->get(AlgoliaManager::class); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|