1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license https://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
use Yii; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Description of MetaTrait |
19
|
|
|
* |
20
|
|
|
* @version 2.0 |
21
|
|
|
* @author vistart <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait MetaTrait |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Store the guid of blame. |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public function behaviors() |
31
|
|
|
{ |
32
|
|
|
return array_merge($this->getMetaBehaviors(), parent::behaviors()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getKey() |
36
|
|
|
{ |
37
|
|
|
return $this->id; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getValue() |
41
|
|
|
{ |
42
|
|
|
return $this->content; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Skip all behaviors of parent class. |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getMetaBehaviors() |
50
|
|
|
{ |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get meta value by specified key. If key doesn't exist, null will be given. |
56
|
|
|
* @param string $key meta key. |
57
|
|
|
* @return string meta value. |
58
|
|
|
*/ |
59
|
|
|
public static function get($key) |
60
|
|
|
{ |
61
|
|
|
$noInitModel = static::buildNoInitModel(); |
62
|
|
|
$model = static::find()->where([$noInitModel->idAttribute => $key])->one(); |
63
|
|
|
if ($model) { |
64
|
|
|
return $model->value; |
65
|
|
|
} |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get meta values by specified keys. If one of keys doesn't exists, it will |
71
|
|
|
* not appear in return array. |
72
|
|
|
* @param string[] $keys |
73
|
|
|
*/ |
74
|
|
|
public static function gets($keys = null) |
75
|
|
|
{ |
76
|
|
|
$noInitModel = static::buildNoInitModel(); |
77
|
|
|
$query = static::find(); |
78
|
|
|
if ($keys == null) { |
79
|
|
|
$models = $query->all(); |
80
|
|
|
} elseif (is_array($keys)) { |
81
|
|
|
$array = []; |
82
|
|
|
foreach ($keys as $key) { |
83
|
|
|
if (is_string($key) && strlen($key)) { |
84
|
|
|
$array[] = $key; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
$models = $query->where([$noInitModel->idAttribute => $array])->all(); |
88
|
|
|
} |
89
|
|
|
$result = []; |
90
|
|
|
foreach ($models as $key => $model) { |
|
|
|
|
91
|
|
|
$result[$model->key] = $model->value; |
92
|
|
|
} |
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public static function set($key, $value = null, $createdBy = null) |
97
|
|
|
{ |
98
|
|
|
$noInitModel = static::buildNoInitModel(); |
99
|
|
|
$model = static::find()->where([$noInitModel->idAttribute => $key])->one(); |
100
|
|
|
if ($value == null && $model) { |
101
|
|
|
return $model->delete(); |
102
|
|
|
} |
103
|
|
|
if (!$model) { |
104
|
|
|
if (empty($createdBy) && !Yii::$app->user->isGuest) { |
105
|
|
|
$createdBy = Yii::$app->user->identity->guid; |
106
|
|
|
} |
107
|
|
|
$model = new static([$noInitModel->idAttribute => $key, $noInitModel->createdByAttribute => $createdBy]); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
$model->value = $value; |
110
|
|
|
return $model->save(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function sets($keys, $createdBy = null) |
114
|
|
|
{ |
115
|
|
|
if (!is_array($keys)) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
foreach ($keys as $key => $value) { |
119
|
|
|
static::set($key, $value, $createdBy); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function remove($key) |
124
|
|
|
{ |
125
|
|
|
return static::set($key); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: