1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the yii2-search package. |
5
|
|
|
* |
6
|
|
|
* (c) Vladimir Kuprienko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace vintage\search\models; |
13
|
|
|
|
14
|
|
|
use yii\base\BaseObject; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Model for store of search result. |
18
|
|
|
* |
19
|
|
|
* @property string $title |
20
|
|
|
* @property string $description |
21
|
|
|
* @property string $url |
22
|
|
|
* |
23
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
24
|
|
|
* @since 1.0 |
25
|
|
|
*/ |
26
|
|
|
class SearchResult extends BaseObject |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $title; |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $description; |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $url; |
40
|
|
|
/** |
41
|
|
|
* @var integer |
42
|
|
|
*/ |
43
|
|
|
public $modelId; |
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $modelName; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Building the result from Active Record object. |
52
|
|
|
* |
53
|
|
|
* @param \yii\db\BaseActiveRecord|\vintage\search\interfaces\SearchInterface $modelObject |
54
|
|
|
* |
55
|
|
|
* @return SearchResult |
56
|
|
|
*/ |
57
|
|
|
public static function build($modelObject) |
58
|
|
|
{ |
59
|
|
|
return new self([ |
60
|
|
|
'modelId' => $modelObject->getPrimaryKey(), |
|
|
|
|
61
|
|
|
'modelName' => $modelObject::className(), |
|
|
|
|
62
|
|
|
'title' => $modelObject->getSearchTitle(), |
63
|
|
|
'description' => $modelObject->getSearchDescription(), |
64
|
|
|
'url' => $modelObject->getSearchUrl(), |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Multiply building of result from Active Record objects. |
70
|
|
|
* |
71
|
|
|
* @param \yii\db\BaseActiveRecord[]|\vintage\search\interfaces\SearchInterface[] $modelObjects |
72
|
|
|
* |
73
|
|
|
* @return SearchResult[] |
74
|
|
|
*/ |
75
|
|
|
public static function buildMultiply($modelObjects) |
76
|
|
|
{ |
77
|
|
|
$results = []; |
78
|
|
|
|
79
|
|
|
foreach ($modelObjects as $object) { |
80
|
|
|
$results[] = static::build($object); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $results; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Method for sorting results of search by model name. |
88
|
|
|
* |
89
|
|
|
* @param SearchResult[] $searchResults |
90
|
|
|
* |
91
|
|
|
* @return SearchResult[] |
92
|
|
|
*/ |
93
|
|
|
public static function sortByModel(array $searchResults) |
94
|
|
|
{ |
95
|
|
|
$sorted = []; |
96
|
|
|
|
97
|
|
|
foreach ($searchResults as $obj) { |
98
|
|
|
$sorted[$obj->modelName][] = $obj; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $sorted; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|