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