1 | <?php |
||
29 | trait ConfirmationTrait |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var int Unconfirmed. |
||
34 | */ |
||
35 | public static $confirmFalse = 0; |
||
36 | |||
37 | /** |
||
38 | * @var int Confirmed. |
||
39 | */ |
||
40 | public static $confirmTrue = 1; |
||
41 | |||
42 | /** |
||
43 | * @var string|false attribute name of confirmation, or false if disable confirmation features. |
||
44 | */ |
||
45 | public $confirmationAttribute = false; |
||
46 | |||
47 | /** |
||
48 | * @var string This attribute specify the name of confirm_code attribute, if |
||
49 | * this attribute is assigned to false, this feature will be ignored. |
||
50 | * if $confirmationAttribute is empty or false, this attribute will be skipped. |
||
51 | */ |
||
52 | public $confirmCodeAttribute = 'confirm_code'; |
||
53 | |||
54 | /** |
||
55 | * @var integer The expiration in seconds. If $confirmCodeAttribute is |
||
56 | * specified, this attribute must be specified. |
||
57 | */ |
||
58 | public $confirmCodeExpiration = 3600; |
||
59 | |||
60 | /** |
||
61 | * @var string This attribute specify the name of confirm_time attribute. if |
||
62 | * this attribute is assigned to false, this feature will be ignored. |
||
63 | * if $confirmationAttribute is empty or false, this attribute will be skipped. |
||
64 | */ |
||
65 | public $confirmTimeAttribute = 'confirm_time'; |
||
66 | |||
67 | /** |
||
68 | * @var string initialization confirm time. |
||
69 | */ |
||
70 | public $initConfirmTime = '1970-01-01 00:00:00'; |
||
71 | public static $eventConfirmationChanged = "confirmationChanged"; |
||
72 | public static $eventConfirmationCanceled = "confirmationCanceled"; |
||
73 | public static $eventConfirmationSuceeded = "confirmationSucceeded"; |
||
74 | |||
75 | /** |
||
76 | * Apply confirmation. |
||
77 | * @return boolean |
||
78 | * @throws \yii\base\NotSupportedException |
||
79 | */ |
||
80 | public function applyConfirmation() |
||
90 | |||
91 | /** |
||
92 | * Set confirm code. |
||
93 | * @param string $code |
||
94 | */ |
||
95 | 8 | public function setConfirmCode($code) |
|
112 | |||
113 | /** |
||
114 | * Get confirm code. |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getConfirmCode() |
||
122 | |||
123 | /** |
||
124 | * Confirm the current content. |
||
125 | * @param string $code |
||
126 | * @return boolean |
||
127 | */ |
||
128 | public function confirm($code) |
||
136 | |||
137 | /** |
||
138 | * Generate confirmation code. |
||
139 | * @return string code |
||
140 | */ |
||
141 | public function generateConfirmationCode() |
||
145 | |||
146 | /** |
||
147 | * Validate the confirmation code. |
||
148 | * @param string $code |
||
149 | * @return boolean Whether the confirmation code is valid. |
||
150 | */ |
||
151 | public function validateConfirmationCode($code) |
||
159 | |||
160 | /** |
||
161 | * Get confirmation status of current model. |
||
162 | * @return boolean Whether current model has been confirmed. |
||
163 | */ |
||
164 | public function getIsConfirmed() |
||
169 | |||
170 | /** |
||
171 | * Initialize the confirmation status. |
||
172 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
173 | * override or modify it directly, unless you know the consequences. |
||
174 | * @param ModelEvent $event |
||
175 | */ |
||
176 | 46 | public function onInitConfirmation($event) |
|
185 | |||
186 | /** |
||
187 | * Set confirmation. |
||
188 | * @param mixed $value |
||
189 | */ |
||
190 | 10 | public function setConfirmation($value) |
|
199 | |||
200 | /** |
||
201 | * Get confirmation. |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function getConfirmation() |
||
209 | |||
210 | /** |
||
211 | * When confirmation status changed, this event will be triggered. If |
||
212 | * confirmation succeeded, the confirm_time will be assigned to current time, |
||
213 | * or the confirm_time will be assigned to initConfirmTime. |
||
214 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
215 | * override or modify it directly, unless you know the consequences. |
||
216 | * @param ModelEvent $event |
||
217 | */ |
||
218 | 8 | public function onConfirmationChanged($event) |
|
235 | |||
236 | /** |
||
237 | * Get rules associated with confirmation attributes. |
||
238 | * if not enable confirmation feature, it will return empty array. |
||
239 | * @return array |
||
240 | */ |
||
241 | 15 | public function getConfirmationRules() |
|
251 | |||
252 | /** |
||
253 | * When the content changed, reset confirmation status. |
||
254 | */ |
||
255 | 15 | protected function resetConfirmation() |
|
272 | |||
273 | /** |
||
274 | * Reset others' confirmation when the others own the same content. |
||
275 | */ |
||
276 | protected function resetOthersConfirmation() |
||
290 | } |
||
291 |
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.