1 | <?php |
||
25 | trait IDTrait |
||
26 | { |
||
27 | /** |
||
28 | * @var string OPTIONAL. The attribute that will receive the IDentifier No. |
||
29 | * You can set this property to false if you don't use this feature. |
||
30 | */ |
||
31 | public $idAttribute = 'id'; |
||
32 | public static $idTypeString = 0; |
||
33 | public static $idTypeInteger = 1; |
||
34 | public static $idTypeAutoIncrement = 2; |
||
35 | |||
36 | /** |
||
37 | * @var integer type of id attribute. |
||
38 | */ |
||
39 | public $idAttributeType = 0; |
||
40 | |||
41 | /** |
||
42 | * @var boolean Determines whether its ID has been pre-assigned. It will not |
||
43 | * generate or assign ID if true. |
||
44 | */ |
||
45 | public $idPreassigned = false; |
||
46 | |||
47 | /** |
||
48 | * @var string The prefix of ID. When ID type is Auto Increment, this feature |
||
49 | * is skipped. |
||
50 | */ |
||
51 | public $idAttributePrefix = ''; |
||
52 | |||
53 | /** |
||
54 | * @var integer OPTIONAL. The length of id attribute value, and max length |
||
55 | * of this attribute in rules. If you set $idAttribute to false or ID type |
||
56 | * to Auto Increment, this property will be ignored. |
||
57 | */ |
||
58 | public $idAttributeLength = 4; |
||
59 | |||
60 | /** |
||
61 | * @var boolean Determine whether the ID is safe for validation. |
||
62 | */ |
||
63 | protected $idAttributeSafe = false; |
||
64 | |||
65 | /** |
||
66 | * Get ID. |
||
67 | * @return string|integer |
||
68 | */ |
||
69 | 18 | public function getID() |
|
74 | |||
75 | /** |
||
76 | * Set id. |
||
77 | * @param string|integer $identity |
||
78 | * @return string|integer |
||
79 | */ |
||
80 | 222 | public function setID($identity) |
|
85 | |||
86 | /** |
||
87 | * Attach `onInitGuidAttribute` event. |
||
88 | * @param string $eventName |
||
89 | */ |
||
90 | 237 | protected function attachInitIDEvent($eventName) |
|
94 | |||
95 | /** |
||
96 | * Initialize the ID attribute with new generated ID. |
||
97 | * If the model's id is pre-assigned, then it will return directly. |
||
98 | * If the model's id is auto-increment, the id attribute will be marked safe. |
||
99 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
100 | * override or modify it directly, unless you know the consequences. |
||
101 | * @param ModelEvent $event |
||
102 | */ |
||
103 | 237 | public function onInitIDAttribute($event) |
|
104 | { |
||
105 | 237 | $sender = $event->sender; |
|
106 | /* @var $sender static */ |
||
107 | 237 | if ($sender->idPreassigned) { |
|
108 | 108 | return; |
|
109 | } |
||
110 | 237 | if ($sender->idAttributeType === static::$idTypeAutoIncrement) { |
|
111 | 30 | $sender->idAttributeSafe = true; |
|
112 | 30 | return; |
|
113 | } |
||
114 | 222 | $idAttribute = $sender->idAttribute; |
|
115 | 222 | if (is_string($idAttribute) && !empty($idAttribute) && |
|
116 | 222 | is_int($sender->idAttributeLength) && |
|
117 | 222 | $sender->idAttributeLength > 0) { |
|
118 | 222 | $sender->setID($sender->generateId()); |
|
119 | } |
||
120 | 222 | } |
|
121 | |||
122 | /** |
||
123 | * Generate the ID. You can override this method to implement your own |
||
124 | * generation algorithm. |
||
125 | * @return string the generated ID. |
||
126 | */ |
||
127 | 222 | public function generateId() |
|
128 | { |
||
129 | 222 | if ($this->idAttributeType == static::$idTypeInteger) { |
|
130 | do { |
||
131 | 165 | $result = Number::randomNumber($this->idAttributePrefix, $this->idAttributeLength); |
|
132 | 165 | } while ($this->checkIdExists((int) $result)); |
|
133 | 165 | return $result; |
|
134 | } |
||
135 | 119 | if ($this->idAttributeType == static::$idTypeString) { |
|
136 | 119 | return $this->idAttributePrefix . |
|
137 | 119 | Yii::$app->security->generateRandomString($this->idAttributeLength - strlen($this->idAttributePrefix)); |
|
138 | } |
||
139 | 1 | if ($this->idAttributeType == static::$idTypeAutoIncrement) { |
|
140 | 1 | return null; |
|
141 | } |
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Check if $identity existed. |
||
147 | * @param mixed $identity |
||
148 | * @return boolean |
||
149 | */ |
||
150 | 166 | public function checkIdExists($identity) |
|
157 | |||
158 | /** |
||
159 | * Get the rules associated with id attribute. |
||
160 | * @return array |
||
161 | */ |
||
162 | 217 | public function getIdRules() |
|
197 | |||
198 | public static function compositeIDs($models) |
||
211 | } |
||
212 | |||
213 |
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.