1 | <?php |
||
29 | trait IPTrait |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var integer REQUIRED. Determine whether enabling the IP attributes and |
||
34 | * features, and IP address type if enabled. |
||
35 | * @since 1.1 |
||
36 | * @version 2.0 |
||
37 | */ |
||
38 | public $enableIP = 0x03; |
||
39 | |||
40 | /** |
||
41 | * @var integer Disable IP address features. |
||
42 | */ |
||
43 | public static $noIp = 0x0; |
||
44 | |||
45 | /** |
||
46 | * @var integer Only accept IPv4 address. |
||
47 | */ |
||
48 | public static $ipv4 = 0x1; |
||
49 | |||
50 | /** |
||
51 | * @var integer Only accept IPv6 address. |
||
52 | */ |
||
53 | public static $ipv6 = 0x2; |
||
54 | |||
55 | /** |
||
56 | * @var integer Accept IPv4 and IPv6 address. Judge type of IP address |
||
57 | * automatically. |
||
58 | */ |
||
59 | public static $ipAll = 0x3; |
||
60 | |||
61 | /** |
||
62 | * @var string The attribute name that will receive the beginning 32 bits of |
||
63 | * IPv6, or IPv4. The default value is 'ip_1'. |
||
64 | */ |
||
65 | public $ipAttribute1 = 'ip_1'; |
||
66 | |||
67 | /** |
||
68 | * @var string The attribute name that will receive the 33 - 64 bits of IPv6, |
||
69 | * or 0 of IPv4. The default value is 'ip_2'. |
||
70 | */ |
||
71 | public $ipAttribute2 = 'ip_2'; |
||
72 | |||
73 | /** |
||
74 | * @var string The attribute name that will receive the 65 - 96 bits of IPv6, |
||
75 | * or 0 of IPv4. The default value is 'ip_3'. |
||
76 | */ |
||
77 | public $ipAttribute3 = 'ip_3'; |
||
78 | |||
79 | /** |
||
80 | * @var string The attribute name that will receive the last 32 bits of IPv6, |
||
81 | * or 0 of IPv4. The default value is 'ip_4'. |
||
82 | */ |
||
83 | public $ipAttribute4 = 'ip_4'; |
||
84 | |||
85 | /** |
||
86 | * @var string The attribute name that will receive the type of IP address. |
||
87 | * The default value is 'ip_type'. If you assign $enableIP to $ipAll, this |
||
88 | * attribute is required. |
||
89 | */ |
||
90 | public $ipTypeAttribute = 'ip_type'; |
||
91 | |||
92 | /** |
||
93 | * @var string Request component ID. |
||
94 | */ |
||
95 | public $requestId = 'request'; |
||
96 | |||
97 | /** |
||
98 | * Get web request component. if `$requestId` not specified, Yii::$app->request |
||
99 | * will be taken. |
||
100 | * @return Request |
||
101 | */ |
||
102 | 55 | protected function getWebRequest() |
|
115 | |||
116 | /** |
||
117 | * Attach `onInitGuidAttribute` event. |
||
118 | * @param string $eventName |
||
119 | */ |
||
120 | 55 | protected function attachInitIpEvent($eventName) |
|
124 | |||
125 | /** |
||
126 | * Initialize ip attributes. |
||
127 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
128 | * override or modify it directly, unless you know the consequences. |
||
129 | * @param \yii\base\Event $event |
||
130 | * @since 1.1 |
||
131 | */ |
||
132 | 55 | public function onInitIpAddress($event) |
|
141 | |||
142 | /** |
||
143 | * Return the IP address. |
||
144 | * The IP address is converted from ipAttribute*. |
||
145 | * If you disable($this->enableIP = false) the IP feature, this method will |
||
146 | * return null, or return the significantly IP address(Colon hexadecimal of |
||
147 | * IPv6 or Dotted decimal of IPv4). |
||
148 | * @return string|integer|null |
||
149 | */ |
||
150 | 55 | public function getIpAddress() |
|
171 | |||
172 | /** |
||
173 | * Get the IPv4 address. |
||
174 | * @return string |
||
175 | */ |
||
176 | 1 | private function getIpv4Address() |
|
180 | |||
181 | /** |
||
182 | * Get the IPv6 address. |
||
183 | * @return string |
||
184 | */ |
||
185 | 1 | private function getIpv6Address() |
|
194 | |||
195 | /** |
||
196 | * Convert the IP address to integer, and store it(them) to ipAttribute*. |
||
197 | * If you disable($this->enableIP = false) the IP feature, this method will |
||
198 | * be skipped(return null). |
||
199 | * @param string $ipAddress the significantly IP address. |
||
200 | * @return string|integer|null Integer when succeeded to convert. |
||
201 | */ |
||
202 | 55 | public function setIpAddress($ipAddress) |
|
225 | |||
226 | /** |
||
227 | * Get the rules associated with ip attributes. |
||
228 | * @return array |
||
229 | */ |
||
230 | 17 | public function getIpRules() |
|
257 | |||
258 | 16 | public function enabledIPFields() |
|
276 | } |
||
277 |
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.