1 | <?php |
||
25 | trait IDTrait |
||
26 | { |
||
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 | * @since 1.1 |
||
32 | */ |
||
33 | public $idAttribute = 'id'; |
||
34 | public static $idTypeString = 0; |
||
35 | public static $idTypeInteger = 1; |
||
36 | public static $idTypeAutoIncrement = 2; |
||
37 | |||
38 | /** |
||
39 | * @var integer type of id attribute. |
||
40 | * @since 2.0 |
||
41 | */ |
||
42 | public $idAttributeType = 0; |
||
43 | |||
44 | /** |
||
45 | * @var boolean Determines whether its id has been pre-assigned. It will not |
||
46 | * generate or assign ID if true. |
||
47 | */ |
||
48 | public $idPreassigned = false; |
||
49 | |||
50 | /** |
||
51 | * @var string The prefix of ID. When ID type is Auto Increment, this feature |
||
52 | * is skipped. |
||
53 | * @since 2.0 |
||
54 | */ |
||
55 | public $idAttributePrefix = ''; |
||
56 | |||
57 | /** |
||
58 | * @var integer OPTIONAL. The length of id attribute value, and max length |
||
59 | * of this attribute in rules. If you set $idAttribute to false or ID type |
||
60 | * to Auto Increment, this property will be ignored. |
||
61 | * @since 1.1 |
||
62 | */ |
||
63 | public $idAttributeLength = 4; |
||
64 | |||
65 | /** |
||
66 | * @var boolean Determine whether the ID is safe for validation. |
||
67 | * @since 1.1 |
||
68 | */ |
||
69 | protected $idAttributeSafe = false; |
||
70 | |||
71 | /** |
||
72 | * Get id. |
||
73 | * @return string|integer |
||
74 | */ |
||
75 | 1 | public function getId() |
|
76 | { |
||
77 | 1 | $idAttribute = $this->idAttribute; |
|
78 | 1 | return is_string($idAttribute) ? $this->$idAttribute : null; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Set id. |
||
83 | * @param string|integer $identity |
||
84 | * @return string|integer |
||
85 | */ |
||
86 | public function setId($identity) |
||
91 | |||
92 | /** |
||
93 | * Attach `onInitGuidAttribute` event. |
||
94 | * @param string $eventName |
||
95 | */ |
||
96 | 38 | protected function attachInitIdEvent($eventName) |
|
100 | |||
101 | /** |
||
102 | * Initialize the ID attribute with new generated ID. |
||
103 | * If the model's id is pre-assigned, then it will return directly. |
||
104 | * If the model's id is auto-increment, the id attribute will be marked safe. |
||
105 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
106 | * override or modify it directly, unless you know the consequences. |
||
107 | * @param \yii\base\Event $event |
||
108 | * @since 1.1 |
||
109 | */ |
||
110 | 38 | public function onInitIdAttribute($event) |
|
111 | { |
||
112 | 38 | $sender = $event->sender; |
|
113 | 38 | if ($sender->idPreassigned) { |
|
114 | 22 | return; |
|
115 | } |
||
116 | 38 | if ($sender->idAttributeType === self::$idTypeAutoIncrement) { |
|
117 | 1 | $sender->idAttributeSafe = true; |
|
118 | 1 | return; |
|
119 | } |
||
120 | 37 | if (is_string($sender->idAttribute) && |
|
121 | 37 | is_int($sender->idAttributeLength) && |
|
122 | 37 | $sender->idAttributeLength > 0) { |
|
123 | 37 | $idAttribute = $sender->idAttribute; |
|
124 | 37 | $sender->$idAttribute = $sender->generateId(); |
|
125 | 37 | } |
|
126 | 37 | } |
|
127 | |||
128 | /** |
||
129 | * Generate the ID. You can override this method to implement your own |
||
130 | * generation algorithm. |
||
131 | * @return string the generated ID. |
||
132 | */ |
||
133 | 37 | public function generateId() |
|
150 | |||
151 | /** |
||
152 | * Check if $identity existed. |
||
153 | * @param mixed $identity |
||
154 | * @return boolean |
||
155 | */ |
||
156 | 36 | public function checkIdExists($identity) |
|
163 | |||
164 | /** |
||
165 | * Get the rules associated with id attribute. |
||
166 | * @return array |
||
167 | */ |
||
168 | 8 | public function getIdRules() |
|
203 | } |
||
204 |
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.