1 | <?php |
||
26 | trait IDTrait |
||
27 | { |
||
28 | /** |
||
29 | * @var string OPTIONAL. The attribute that will receive the IDentifier No. |
||
30 | * You can set this property to false if you don't use this feature. |
||
31 | */ |
||
32 | public $idAttribute = 'id'; |
||
33 | public static $idTypeString = 0; |
||
34 | public static $idTypeInteger = 1; |
||
35 | public static $idTypeAutoIncrement = 2; |
||
36 | |||
37 | /** |
||
38 | * @var integer type of id attribute. |
||
39 | */ |
||
40 | public $idAttributeType = 0; |
||
41 | |||
42 | /** |
||
43 | * @var boolean Determines whether its ID has been pre-assigned. It will not |
||
44 | * generate or assign ID if true. |
||
45 | */ |
||
46 | public $idPreassigned = false; |
||
47 | |||
48 | /** |
||
49 | * @var string The prefix of ID. When ID type is Auto Increment, this feature |
||
50 | * is skipped. |
||
51 | */ |
||
52 | public $idAttributePrefix = ''; |
||
53 | |||
54 | /** |
||
55 | * @var integer OPTIONAL. The length of id attribute value, and max length |
||
56 | * of this attribute in rules. If you set $idAttribute to false or ID type |
||
57 | * to Auto Increment, this property will be ignored. |
||
58 | */ |
||
59 | public $idAttributeLength = 4; |
||
60 | |||
61 | /** |
||
62 | * @var boolean Determine whether the ID is safe for validation. |
||
63 | */ |
||
64 | protected $idAttributeSafe = false; |
||
65 | |||
66 | /** |
||
67 | * Get ID. |
||
68 | * @return string|integer |
||
69 | */ |
||
70 | 30 | public function getID() |
|
75 | |||
76 | /** |
||
77 | * Set id. |
||
78 | * @param string|integer $identity |
||
79 | * @return string|integer |
||
80 | */ |
||
81 | 377 | public function setID($identity) |
|
86 | |||
87 | /** |
||
88 | * Attach `onInitGuidAttribute` event. |
||
89 | * @param string $eventName |
||
90 | */ |
||
91 | 392 | protected function attachInitIDEvent($eventName) |
|
95 | |||
96 | /** |
||
97 | * Initialize the ID attribute with new generated ID. |
||
98 | * If the model's id is pre-assigned, then it will return directly. |
||
99 | * If the model's id is auto-increment, the id attribute will be marked safe. |
||
100 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
101 | * override or modify it directly, unless you know the consequences. |
||
102 | * @param ModelEvent $event |
||
103 | */ |
||
104 | 392 | public function onInitIDAttribute($event) |
|
122 | |||
123 | /** |
||
124 | * Generate the ID. You can override this method to implement your own |
||
125 | * generation algorithm. |
||
126 | * @return string the generated ID. |
||
127 | */ |
||
128 | 377 | public function generateId() |
|
145 | |||
146 | /** |
||
147 | * Check if $identity existed. |
||
148 | * @param mixed $identity |
||
149 | * @return boolean |
||
150 | */ |
||
151 | 308 | public function checkIdExists($identity) |
|
152 | { |
||
153 | 308 | if ($identity == null) { |
|
154 | 4 | return false; |
|
155 | } |
||
156 | 308 | return static::find()->where([$this->idAttribute => $identity])->exists(); |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get the rules associated with id attribute. |
||
161 | * @return array |
||
162 | */ |
||
163 | 361 | public function getIdRules() |
|
193 | |||
194 | /** |
||
195 | * Composite IDs from models. |
||
196 | * @param $models |
||
197 | * @return array|int|string |
||
198 | */ |
||
199 | 1 | public static function compositeIDs($models) |
|
212 | } |
||
213 | |||
214 |
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.