1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SearchTrait |
10
|
|
|
* @package BestServedCold\LaravelZendSearch\Laravel |
11
|
|
|
*/ |
12
|
|
|
trait SearchTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array $searchFields |
16
|
|
|
*/ |
17
|
|
|
private static $searchFields = [ ]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array $boostFeilds |
21
|
|
|
*/ |
22
|
|
|
private static $boostFeilds = [ ]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Set up |
26
|
|
|
* |
27
|
|
|
* @throws \Exception |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
17 |
|
private static function setup() |
31
|
|
|
{ |
32
|
|
|
// Ignoring PHP bug #53727 here, Eloquent Models implement several interfaces. |
33
|
17 |
|
if (!is_subclass_of(static::class, Model::class)) { |
34
|
1 |
|
throw new \Exception( |
35
|
1 |
|
'SearchTrait must only be used with Eloquent models, [' . get_called_class() . '] used.' |
36
|
1 |
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
17 |
|
static::searchFields(); |
40
|
17 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Search Fields |
44
|
|
|
* |
45
|
|
|
* This should never get called. If it does get called, then it means that the Model which is using |
46
|
|
|
* "SearchTrait" has not declared a "searchFields" method and made it static. |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
1 |
|
private static function searchFields() |
51
|
|
|
{ |
52
|
1 |
|
throw new \Exception("Method [searchFields] must exist and be static."); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $fields |
57
|
|
|
*/ |
58
|
17 |
|
public static function setSearchFields(array $fields) |
59
|
|
|
{ |
60
|
17 |
|
self::$searchFields = $fields; |
61
|
17 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
2 |
|
public static function getSearchFields() |
67
|
|
|
{ |
68
|
2 |
|
return self::$searchFields; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $fields |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
2 |
|
public static function setBoostFields(array $fields) |
76
|
|
|
{ |
77
|
|
|
if (! array_filter($fields, function($value) { |
78
|
2 |
|
return is_int($value) || is_float($value); |
79
|
2 |
|
})) { |
80
|
1 |
|
throw new \Exception('Boost field values must be integers or floats.'); |
81
|
|
|
} |
82
|
1 |
|
self::$boostFeilds = $fields; |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
1 |
|
public static function getBoostFields() |
89
|
|
|
{ |
90
|
1 |
|
return self::$boostFeilds; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
2 |
|
public static function search() |
97
|
|
|
{ |
98
|
2 |
|
self::setup(); |
99
|
|
|
|
100
|
1 |
|
$search = App::make(Search::class); |
101
|
1 |
|
$search->model(new static); |
102
|
|
|
|
103
|
1 |
|
return $search; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
17 |
|
public static function bootSearchTrait() |
110
|
|
|
{ |
111
|
17 |
|
self::setup(); |
112
|
17 |
|
$store = App::make(Store::class); |
113
|
17 |
|
self::saved(self::insertCallback($store)); |
114
|
17 |
|
self::deleting(self::deleteCallback($store)); |
115
|
17 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Store $store |
119
|
|
|
* @return \Closure |
120
|
|
|
*/ |
121
|
17 |
|
private static function insertCallback(Store $store) |
122
|
|
|
{ |
123
|
|
|
return function(Model $model) use ($store) { |
124
|
1 |
|
$store->model($model); |
125
|
1 |
|
$store->insertModel($model); |
126
|
17 |
|
}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Store $store |
131
|
|
|
* @return \Closure |
132
|
|
|
*/ |
133
|
|
|
private static function deleteCallback(Store $store) |
134
|
|
|
{ |
135
|
17 |
|
return function(Model $model) use ($store) { |
136
|
1 |
|
$store->model($model); |
137
|
1 |
|
$store->deleteModel($model); |
138
|
17 |
|
}; |
139
|
|
|
} |
140
|
|
|
} |
|
|
|
|
141
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.