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
|
|
|
* @property string $key |
21
|
|
|
* @property string $value |
22
|
|
|
* @version 2.0 |
23
|
|
|
* @author vistart <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
trait MetaTrait |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Store the guid of blame. |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
2 |
|
public function behaviors() |
33
|
|
|
{ |
34
|
2 |
|
return array_merge($this->getMetaBehaviors(), parent::behaviors()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getKey() |
38
|
|
|
{ |
39
|
|
|
return $this->id; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setKey($key) |
43
|
|
|
{ |
44
|
|
|
return $this->id = $key; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getValue() |
48
|
|
|
{ |
49
|
|
|
return $this->content; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setValue($value) |
53
|
|
|
{ |
54
|
|
|
return $this->content = $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Skip all behaviors of parent class. |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
2 |
|
public function getMetaBehaviors() |
62
|
|
|
{ |
63
|
2 |
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get meta value by specified key. If key doesn't exist, null will be given. |
68
|
|
|
* @param string $key meta key. |
69
|
|
|
* @return string meta value. |
70
|
|
|
*/ |
71
|
2 |
|
public static function get($key) |
72
|
|
|
{ |
73
|
2 |
|
$noInitModel = static::buildNoInitModel(); |
74
|
2 |
|
$model = static::find()->where([$noInitModel->idAttribute => $key])->one(); |
75
|
2 |
|
if ($model) { |
76
|
2 |
|
return $model->value; |
77
|
|
|
} |
78
|
2 |
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get meta values by specified keys. If one of keys doesn't exists, it will |
83
|
|
|
* not appear in return array. |
84
|
|
|
* @param string[] $keys |
85
|
|
|
* @return array meta key-value pairs. |
86
|
|
|
*/ |
87
|
2 |
|
public static function gets($keys = null) |
88
|
|
|
{ |
89
|
2 |
|
$noInitModel = static::buildNoInitModel(); |
90
|
2 |
|
$query = static::find(); |
91
|
2 |
|
if ($keys == null) { |
92
|
2 |
|
$models = $query->all(); |
93
|
2 |
|
} elseif (is_array($keys)) { |
94
|
2 |
|
$array = []; |
95
|
2 |
|
foreach ($keys as $key) { |
96
|
2 |
|
if (is_string($key) && strlen($key)) { |
97
|
2 |
|
$array[] = $key; |
98
|
2 |
|
} |
99
|
2 |
|
} |
100
|
2 |
|
$models = $query->where([$noInitModel->idAttribute => $array])->all(); |
101
|
2 |
|
} |
102
|
2 |
|
$result = []; |
103
|
2 |
|
foreach ($models as $key => $model) { |
|
|
|
|
104
|
2 |
|
$result[$model->key] = $model->value; |
105
|
2 |
|
} |
106
|
2 |
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set value. |
111
|
|
|
* @param string $key |
112
|
|
|
* @param string $value |
113
|
|
|
* @param string $createdBy |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
2 |
|
public static function set($key, $value = null, $createdBy = null) |
117
|
|
|
{ |
118
|
2 |
|
$noInitModel = static::buildNoInitModel(); |
119
|
2 |
|
$model = static::find()->where([$noInitModel->idAttribute => $key])->one(); |
120
|
2 |
|
if ($value == null && $model) { |
|
|
|
|
121
|
2 |
|
return $model->delete(); |
122
|
|
|
} |
123
|
2 |
|
if (!$model) { |
124
|
2 |
|
if (empty($createdBy) && !Yii::$app->user->isGuest) { |
125
|
|
|
$createdBy = Yii::$app->user->identity->guid; |
126
|
|
|
} |
127
|
2 |
|
$model = new static([$noInitModel->idAttribute => $key, $noInitModel->createdByAttribute => $createdBy]); |
|
|
|
|
128
|
2 |
|
} |
129
|
2 |
|
$model->value = $value; |
130
|
2 |
|
return $model->save(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set values in batch. |
135
|
|
|
* @param array $keys meta key-value pairs. |
136
|
|
|
* @param string $createdBy |
137
|
|
|
* @return false if $keys is not an array. |
138
|
|
|
*/ |
139
|
2 |
|
public static function sets($keys, $createdBy = null) |
140
|
|
|
{ |
141
|
2 |
|
if (!is_array($keys)) { |
142
|
2 |
|
return false; |
143
|
|
|
} |
144
|
2 |
|
foreach ($keys as $key => $value) { |
145
|
2 |
|
static::set($key, $value, $createdBy); |
146
|
2 |
|
} |
147
|
2 |
|
} |
148
|
|
|
|
149
|
2 |
|
public static function remove($key) |
150
|
|
|
{ |
151
|
2 |
|
return static::set($key); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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: