1 | <?php |
||
26 | trait IDTrait |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * @var string OPTIONAL.The attribute that will receive the IDentifier No. |
||
31 | * You can set this property to false if you don't use this feature. |
||
32 | * @since 1.1 |
||
33 | */ |
||
34 | public $idAttribute = 'id'; |
||
35 | public static $idTypeString = 0; |
||
36 | public static $idTypeInteger = 1; |
||
37 | public static $idTypeAutoIncrement = 2; |
||
38 | |||
39 | /** |
||
40 | * @var integer type of id attribute. |
||
41 | * @since 2.0 |
||
42 | */ |
||
43 | public $idAttributeType = 0; |
||
44 | |||
45 | /** |
||
46 | * @var boolean Determines whether its id has been pre-assigned. It will not |
||
47 | * generate or assign ID if true. |
||
48 | */ |
||
49 | public $idPreassigned = false; |
||
50 | |||
51 | /** |
||
52 | * @var string The prefix of ID. When ID type is Auto Increment, this feature |
||
53 | * is skipped. |
||
54 | * @since 2.0 |
||
55 | */ |
||
56 | public $idAttributePrefix = ''; |
||
57 | |||
58 | /** |
||
59 | * @var integer OPTIONAL. The length of id attribute value, and max length |
||
60 | * of this attribute in rules. If you set $idAttribute to false or ID type |
||
61 | * to Auto Increment, this property will be ignored. |
||
62 | * @since 1.1 |
||
63 | */ |
||
64 | public $idAttributeLength = 4; |
||
65 | |||
66 | /** |
||
67 | * @var boolean Determine whether the ID is safe for validation. |
||
68 | * @since 1.1 |
||
69 | */ |
||
70 | protected $idAttributeSafe = false; |
||
71 | |||
72 | /** |
||
73 | * Get id. |
||
74 | * @return string|integer |
||
75 | */ |
||
76 | 3 | public function getId() |
|
81 | |||
82 | /** |
||
83 | * Set id. |
||
84 | * @param string|integer $identity |
||
85 | * @return string|integer |
||
86 | */ |
||
87 | public function setId($identity) |
||
92 | |||
93 | /** |
||
94 | * Attach `onInitGuidAttribute` event. |
||
95 | * @param string $eventName |
||
96 | */ |
||
97 | 62 | protected function attachInitIdEvent($eventName) |
|
101 | |||
102 | /** |
||
103 | * Initialize the ID attribute with new generated ID. |
||
104 | * If the model's id is pre-assigned, then it will return directly. |
||
105 | * If the model's id is auto-increment, the id attribute will be marked safe. |
||
106 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
107 | * override or modify it directly, unless you know the consequences. |
||
108 | * @param ModelEvent $event |
||
109 | * @since 1.1 |
||
110 | */ |
||
111 | 62 | public function onInitIdAttribute($event) |
|
128 | |||
129 | /** |
||
130 | * Generate the ID. You can override this method to implement your own |
||
131 | * generation algorithm. |
||
132 | * @return string the generated ID. |
||
133 | */ |
||
134 | 61 | public function generateId() |
|
135 | { |
||
136 | 61 | if ($this->idAttributeType == self::$idTypeInteger) { |
|
137 | do { |
||
138 | 60 | $result = Number::randomNumber($this->idAttributePrefix, $this->idAttributeLength); |
|
139 | 60 | } while ($this->checkIdExists((int) $result)); |
|
140 | 60 | return $result; |
|
141 | } |
||
142 | 37 | if ($this->idAttributeType == self::$idTypeString) { |
|
143 | 37 | return $this->idAttributePrefix . |
|
144 | 37 | Yii::$app->security->generateRandomString($this->idAttributeLength - strlen($this->idAttributePrefix)); |
|
145 | } |
||
146 | if ($this->idAttributeType == self::$idTypeAutoIncrement) { |
||
147 | return null; |
||
148 | } |
||
149 | return false; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Check if $identity existed. |
||
154 | * @param mixed $identity |
||
155 | * @return boolean |
||
156 | */ |
||
157 | 60 | public function checkIdExists($identity) |
|
158 | { |
||
159 | 60 | if ($identity == null) { |
|
160 | return false; |
||
161 | } |
||
162 | 60 | return (static::findOne([$this->idAttribute => $identity]) !== null); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get the rules associated with id attribute. |
||
167 | * @return array |
||
168 | */ |
||
169 | 17 | public function getIdRules() |
|
204 | } |
||
205 |
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.