|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/vuongxuongminh/yii2-searchable |
|
4
|
|
|
* @copyright Copyright (c) 2019 Vuong Xuong Minh |
|
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace vxm\searchable\expression; |
|
9
|
|
|
|
|
10
|
|
|
use yii\base\BaseObject; |
|
11
|
|
|
use yii\base\InvalidConfigException; |
|
12
|
|
|
use yii\db\ExpressionInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Expression make a searchable expression to query. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Vuong Minh <[email protected]> |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Expression extends BaseObject implements ExpressionInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \yii\db\ActiveQuery |
|
24
|
|
|
*/ |
|
25
|
|
|
public $query; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int[]|string[] |
|
29
|
|
|
*/ |
|
30
|
|
|
public $ids = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritDoc |
|
34
|
|
|
* @throws InvalidConfigException |
|
35
|
|
|
*/ |
|
36
|
4 |
|
public function init() |
|
37
|
|
|
{ |
|
38
|
4 |
|
if (empty($this->ids)) { |
|
39
|
|
|
throw new InvalidConfigException('`ids` property must be set to detect id instance!'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
if ($this->query === null) { |
|
43
|
|
|
throw new InvalidConfigException('`query` property must be set to create condition instance!'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
parent::init(); |
|
47
|
4 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Creating an specific expression to apply to query. |
|
51
|
|
|
* |
|
52
|
|
|
* @return ExpressionInterface apply to query. |
|
53
|
|
|
*/ |
|
54
|
|
|
abstract public function getExpression(): ExpressionInterface; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get pretty searchable key via model with an alias of the table. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string the searchable key name |
|
60
|
|
|
*/ |
|
61
|
4 |
|
protected function searchableKey(): string |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var \yii\db\ActiveRecord $modelClass */ |
|
64
|
4 |
|
$modelClass = $this->query->modelClass; |
|
65
|
4 |
|
list(, $alias) = $this->getTableNameAndAlias(); |
|
66
|
|
|
|
|
67
|
4 |
|
return '{{' . $alias . '}}.[[' . $modelClass::searchableKey() . ']]'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the table name and the table alias for [[query::modelClass]]. |
|
72
|
|
|
* This method extract from \yii\db\ActiveQuery. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array the table name and the table alias. |
|
75
|
|
|
*/ |
|
76
|
4 |
|
private function getTableNameAndAlias(): array |
|
77
|
|
|
{ |
|
78
|
|
|
/** @var \yii\db\ActiveRecord $modelClass */ |
|
79
|
4 |
|
$query = $this->query; |
|
80
|
4 |
|
$modelClass = $query->modelClass; |
|
81
|
|
|
|
|
82
|
4 |
|
if (empty($query->from)) { |
|
83
|
|
|
$tableName = $modelClass::tableName(); |
|
84
|
|
|
} else { |
|
85
|
4 |
|
$tableName = ''; |
|
86
|
|
|
// if the first entry in "from" is an alias-tablename-pair return it directly |
|
87
|
4 |
|
foreach ($query->from as $alias => $tableName) { |
|
88
|
4 |
|
if (is_string($alias)) { |
|
89
|
|
|
return [$tableName, $alias]; |
|
90
|
|
|
} |
|
91
|
4 |
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) { |
|
96
|
|
|
$alias = $matches[2]; |
|
97
|
|
|
} else { |
|
98
|
4 |
|
$alias = $tableName; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
return [$tableName, $alias]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|