1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yiicod\base\models\behaviors; |
4
|
|
|
|
5
|
|
|
use yii\base\Behavior; |
6
|
|
|
use yii\base\InvalidParamException; |
7
|
|
|
|
8
|
|
|
class AttributesMapBehavior extends Behavior |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Mapping |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public $attributesMap = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns a value indicating whether the model has an attribute with the specified name. |
19
|
|
|
* |
20
|
|
|
* @param string $name the name of the attribute |
21
|
|
|
* |
22
|
|
|
* @return bool whether the model has an attribute with the specified name |
23
|
|
|
*/ |
24
|
|
|
public function hasAttr($name) |
25
|
|
|
{ |
26
|
|
|
$fieldAttr = $this->prepareField($name); |
27
|
|
|
|
28
|
|
|
return isset($this->attributesMap[$fieldAttr]) && |
29
|
|
|
$this->owner->hasAttribute($this->attributesMap[$fieldAttr]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the named attribute value. |
34
|
|
|
* If this record is the result of a query and the attribute is not loaded, |
35
|
|
|
* null will be returned. |
36
|
|
|
* |
37
|
|
|
* @param string $name the attribute name |
38
|
|
|
* |
39
|
|
|
* @return mixed the attribute value. Null if the attribute is not set or does not exist. |
40
|
|
|
* |
41
|
|
|
* @see hasAttribute() |
42
|
|
|
*/ |
43
|
|
|
public function getAttr($name) |
44
|
|
|
{ |
45
|
|
|
$fieldAttr = $this->prepareField($name); |
46
|
|
|
|
47
|
|
|
return $this->owner->getAttribute($this->attributesMap[$fieldAttr]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets the named attribute value. |
52
|
|
|
* |
53
|
|
|
* @param string $name the attribute name |
54
|
|
|
* @param mixed $value the attribute value |
55
|
|
|
* |
56
|
|
|
* @throws InvalidParamException if the named attribute does not exist |
57
|
|
|
* |
58
|
|
|
* @see hasAttribute() |
59
|
|
|
*/ |
60
|
|
|
public function setAttr($name, $value) |
61
|
|
|
{ |
62
|
|
|
if ($this->hasAttr($name)) { |
63
|
|
|
$fieldAttr = $this->prepareField($name); |
64
|
|
|
$this->owner->{$this->attributesMap[$fieldAttr]} = $value; |
65
|
|
|
} else { |
66
|
|
|
throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".'); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* @param mixed $value |
73
|
|
|
*/ |
74
|
|
|
public function __set($name, $value) |
75
|
|
|
{ |
76
|
|
|
if ($this->hasAttr($name)) { |
77
|
|
|
$this->setAttr($name, $value); |
78
|
|
|
} else { |
79
|
|
|
parent::__set($name, $value); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $name |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function __get($name) |
89
|
|
|
{ |
90
|
|
|
if (0 === strpos($name, 'field') && $this->hasFieldByModelMap($name)) { |
91
|
|
|
return $this->getFieldByModelMap($name); |
92
|
|
|
} |
93
|
|
|
if ($this->hasAttr($name)) { |
94
|
|
|
return $this->getAttr($name); |
95
|
|
|
} else { |
96
|
|
|
return parent::__get($name); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param type $name |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function canGetProperty($name, $checkVars = true) |
106
|
|
|
{ |
107
|
|
|
if (0 === strpos($name, 'field') && $this->hasFieldByModelMap($name)) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($this->hasAttr($name)) { |
112
|
|
|
return true; |
113
|
|
|
} else { |
114
|
|
|
return parent::canGetProperty($name, $checkVars = true); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $name |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function canSetProperty($name, $checkVars = true) |
124
|
|
|
{ |
125
|
|
|
if ($this->hasAttr($name)) { |
126
|
|
|
return true; |
127
|
|
|
} else { |
128
|
|
|
return parent::canSetProperty($name, $checkVars = true); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $name |
134
|
|
|
* |
135
|
|
|
* @return type |
136
|
|
|
*/ |
137
|
|
|
public function getFieldByModelMap($name) |
138
|
|
|
{ |
139
|
|
|
if ($this->hasFieldByModelMap($name)) { |
140
|
|
|
return $this->attributesMap[$name]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $name |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public function hasFieldByModelMap($name) |
152
|
|
|
{ |
153
|
|
|
return isset($this->attributesMap[$name]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Prepare dyn field |
158
|
|
|
* |
159
|
|
|
* @param $name |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function prepareField($name) |
164
|
|
|
{ |
165
|
|
|
$parts = explode('_', $name); |
166
|
|
|
$fieldAttr = 'field'; |
167
|
|
|
foreach ($parts as $part) { |
168
|
|
|
$fieldAttr .= ucfirst($part); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $fieldAttr; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.