1 | <?php |
||
25 | trait IPTrait |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var integer REQUIRED. Determine whether enabling the IP attributes and |
||
30 | * features, and IP address type if enabled. |
||
31 | * @since 1.1 |
||
32 | * @version 2.0 |
||
33 | */ |
||
34 | public $enableIP = 0x03; |
||
35 | |||
36 | /** |
||
37 | * @var integer Disable IP address features. |
||
38 | */ |
||
39 | public static $noIp = 0x0; |
||
40 | |||
41 | /** |
||
42 | * @var integer Only accept IPv4 address. |
||
43 | */ |
||
44 | public static $ipv4 = 0x1; |
||
45 | |||
46 | /** |
||
47 | * @var integer Only accept IPv6 address. |
||
48 | */ |
||
49 | public static $ipv6 = 0x2; |
||
50 | |||
51 | /** |
||
52 | * @var integer Accept IPv4 and IPv6 address. Judge type of IP address |
||
53 | * automatically. |
||
54 | */ |
||
55 | public static $ipAll = 0x3; |
||
56 | |||
57 | /** |
||
58 | * @var string The attribute name that will receive the beginning 32 bits of |
||
59 | * IPv6, or IPv4. The default value is 'ip_1'. |
||
60 | */ |
||
61 | public $ipAttribute1 = 'ip_1'; |
||
62 | |||
63 | /** |
||
64 | * @var string The attribute name that will receive the 33 - 64 bits of IPv6, |
||
65 | * or 0 of IPv4. The default value is 'ip_2'. |
||
66 | */ |
||
67 | public $ipAttribute2 = 'ip_2'; |
||
68 | |||
69 | /** |
||
70 | * @var string The attribute name that will receive the 65 - 96 bits of IPv6, |
||
71 | * or 0 of IPv4. The default value is 'ip_3'. |
||
72 | */ |
||
73 | public $ipAttribute3 = 'ip_3'; |
||
74 | |||
75 | /** |
||
76 | * @var string The attribute name that will receive the last 32 bits of IPv6, |
||
77 | * or 0 of IPv4. The default value is 'ip_4'. |
||
78 | */ |
||
79 | public $ipAttribute4 = 'ip_4'; |
||
80 | |||
81 | /** |
||
82 | * @var string The attribute name that will receive the type of IP address. |
||
83 | * The default value is 'ip_type'. If you assign $enableIP to $ipAll, this |
||
84 | * attribute is required. |
||
85 | */ |
||
86 | public $ipTypeAttribute = 'ip_type'; |
||
87 | |||
88 | /** |
||
89 | * @var string Request component ID. |
||
90 | */ |
||
91 | public $requestId = 'request'; |
||
92 | |||
93 | /** |
||
94 | * Get web request component. if `$requestId` not specified, Yii::$app->request |
||
95 | * will be taken. |
||
96 | * @return \yii\web\Request |
||
97 | */ |
||
98 | 50 | protected function getWebRequest() |
|
111 | |||
112 | /** |
||
113 | * Attach `onInitGuidAttribute` event. |
||
114 | * @param string $eventName |
||
115 | */ |
||
116 | 50 | protected function attachInitIpEvent($eventName) |
|
120 | |||
121 | /** |
||
122 | * Initialize ip attributes. |
||
123 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
124 | * override or modify it directly, unless you know the consequences. |
||
125 | * @param \yii\base\Event $event |
||
126 | * @since 1.1 |
||
127 | */ |
||
128 | 50 | public function onInitIpAddress($event) |
|
129 | { |
||
130 | 50 | $sender = $event->sender; |
|
131 | /* @var $sender \vistart\Models\models\BaseEntityModel */ |
||
132 | 50 | $request = $sender->getWebRequest(); |
|
133 | 50 | if ($sender->enableIP && $request && empty($sender->ipAddress)) { |
|
134 | 50 | $sender->ipAddress = $request->userIP; |
|
135 | 50 | } |
|
136 | 50 | } |
|
137 | |||
138 | /** |
||
139 | * Return the IP address. |
||
140 | * The IP address is converted from ipAttribute*. |
||
141 | * If you disable($this->enableIP = false) the IP feature, this method will |
||
142 | * return null, or return the significantly IP address(Colon hexadecimal of |
||
143 | * IPv6 or Dotted decimal of IPv4). |
||
144 | * @return string|integer|null |
||
145 | */ |
||
146 | 50 | public function getIpAddress() |
|
147 | { |
||
148 | 50 | if (!$this->enableIP) { |
|
149 | return null; |
||
150 | } |
||
151 | 50 | if ($this->enableIP & static::$ipAll) { |
|
152 | 50 | $ipTypeAttribute = $this->ipTypeAttribute; |
|
153 | 50 | if ($this->$ipTypeAttribute == Ip::IPv4) { |
|
154 | 1 | return $this->getIpv4Address(); |
|
155 | } |
||
156 | 50 | if ($this->$ipTypeAttribute == Ip::IPv6) { |
|
157 | 1 | return $this->getIpv6Address(); |
|
158 | } |
||
159 | 50 | } else |
|
160 | if ($this->enableIP & static::$ipv4) { |
||
161 | return $this->getIpv4Address(); |
||
162 | } else |
||
163 | if ($this->enableIP & static::$ipv6) { |
||
164 | return $this->getIpv6Address(); |
||
165 | } |
||
166 | 50 | return null; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Get the IPv4 address. |
||
171 | * @return string |
||
172 | */ |
||
173 | 1 | private function getIpv4Address() |
|
178 | |||
179 | /** |
||
180 | * Get the IPv6 address. |
||
181 | * @return string |
||
182 | */ |
||
183 | 1 | private function getIpv6Address() |
|
196 | |||
197 | /** |
||
198 | * Convert the IP address to integer, and store it(them) to ipAttribute*. |
||
199 | * If you disable($this->enableIP = false) the IP feature, this method will |
||
200 | * be skipped(return null). |
||
201 | * @param string $ipAddress the significantly IP address. |
||
202 | * @return string|integer|null Integer when succeeded to convert. |
||
203 | */ |
||
204 | 50 | public function setIpAddress($ipAddress) |
|
233 | |||
234 | /** |
||
235 | * Get the rules associated with ip attributes. |
||
236 | * @return array |
||
237 | */ |
||
238 | 15 | public function getIpRules() |
|
265 | |||
266 | 13 | public function enabledIPFields() |
|
284 | } |
||
285 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.