1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Search; |
13
|
|
|
|
14
|
|
|
use Da\User\Model\Rule; |
15
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
16
|
|
|
use yii\base\InvalidConfigException; |
17
|
|
|
use yii\base\InvalidParamException; |
18
|
|
|
use yii\base\Model; |
19
|
|
|
use yii\data\ActiveDataProvider; |
20
|
|
|
use yii\db\Query; |
21
|
|
|
|
22
|
|
|
class RuleSearch extends Rule |
23
|
|
|
{ |
24
|
|
|
use ContainerAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $created_at; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function scenarios() |
35
|
|
|
{ |
36
|
|
|
return Model::scenarios(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function rules() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
['name', 'string'], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $params |
51
|
|
|
* |
52
|
|
|
* @throws InvalidConfigException |
53
|
|
|
* @throws InvalidParamException |
54
|
|
|
* @return ActiveDataProvider |
55
|
|
|
*/ |
56
|
|
|
public function search(array $params = []) |
57
|
|
|
{ |
58
|
|
|
$query = (new Query()) |
59
|
|
|
->select(['name', 'data', 'created_at', 'updated_at']) |
60
|
|
|
->from($this->getAuthManager()->ruleTable) |
|
|
|
|
61
|
|
|
->orderBy(['name' => SORT_ASC]); |
62
|
|
|
|
63
|
|
|
if ($this->load($params)) { |
64
|
|
|
$query->andFilterWhere(['name' => $this->name]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!$this->validate()) { |
68
|
|
|
$query->where('0=1'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->make( |
72
|
|
|
ActiveDataProvider::class, |
73
|
|
|
[], |
74
|
|
|
[ |
75
|
|
|
'query' => $query, |
76
|
|
|
'db' => $this->getAuthManager()->db, |
|
|
|
|
77
|
|
|
'sort' => [ |
78
|
|
|
'attributes' => ['name', 'created_at', 'updated_at'] |
79
|
|
|
] |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.