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 | public $requestId = 'request'; |
||
88 | |||
89 | /** |
||
90 | * |
||
91 | * @return \yii\web\Request |
||
92 | */ |
||
93 | 38 | protected function getWebRequest() |
|
106 | |||
107 | /** |
||
108 | * Attach `onInitGuidAttribute` event. |
||
109 | * @param string $eventName |
||
110 | */ |
||
111 | 38 | protected function attachInitIpEvent($eventName) |
|
115 | |||
116 | /** |
||
117 | * Initialize ip attributes. |
||
118 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
119 | * override or modify it directly, unless you know the consequences. |
||
120 | * @param \yii\base\Event $event |
||
121 | * @since 1.1 |
||
122 | */ |
||
123 | 38 | public function onInitIpAddress($event) |
|
132 | |||
133 | /** |
||
134 | * Return the IP address. |
||
135 | * The IP address is converted from ipAttribute*. |
||
136 | * If you disable($this->enableIP = false) the IP feature, this method will |
||
137 | * return null, or return the significantly IP address(Colon hexadecimal of |
||
138 | * IPv6 or Dotted decimal of IPv4). |
||
139 | * @return string|integer|null |
||
140 | */ |
||
141 | 38 | public function getIpAddress() |
|
163 | |||
164 | /** |
||
165 | * Get the IPv4 address. |
||
166 | * @return string |
||
167 | */ |
||
168 | 1 | private function getIpv4Address() |
|
173 | |||
174 | /** |
||
175 | * Get the IPv6 address. |
||
176 | * @return string |
||
177 | */ |
||
178 | 1 | private function getIpv6Address() |
|
191 | |||
192 | /** |
||
193 | * Convert the IP address to integer, and store it(them) to ipAttribute*. |
||
194 | * If you disable($this->enableIP = false) the IP feature, this method will |
||
195 | * be skipped(return null). |
||
196 | * @param string $ipAddress the significantly IP address. |
||
197 | * @return string|integer|null Integer when succeeded to convert. |
||
198 | */ |
||
199 | 38 | public function setIpAddress($ipAddress) |
|
228 | |||
229 | /** |
||
230 | * Get the rules associated with ip attributes. |
||
231 | * @return array |
||
232 | */ |
||
233 | 8 | public function getIpRules() |
|
260 | } |
||
261 |
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.