1 | <?php |
||
26 | trait GUIDTrait |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * @var string REQUIRED. The attribute that will receive the GUID value. |
||
31 | */ |
||
32 | public $guidAttribute = 'guid'; |
||
33 | |||
34 | /** |
||
35 | * DO NOT MODIFY OR OVERRIDE THIS METHOD UNLESS YOU KNOW THE CONSEQUENCES. |
||
36 | * @return string |
||
37 | */ |
||
38 | 3 | public function getReadableGuidAttribute() |
|
42 | |||
43 | /** |
||
44 | * Attach `onInitGUIDAttribute` event. |
||
45 | * @param string $eventName |
||
46 | */ |
||
47 | 392 | protected function attachInitGUIDEvent($eventName) |
|
51 | |||
52 | /** |
||
53 | * Initialize the GUID attribute with new generated GUID. |
||
54 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
55 | * ovveride or modify it directly, unless you know the conquences. |
||
56 | * @param ModelEvent $event |
||
57 | */ |
||
58 | 392 | public function onInitGUIDAttribute($event) |
|
64 | |||
65 | /** |
||
66 | * Generate GUID in binary. |
||
67 | * @return string GUID. |
||
68 | */ |
||
69 | 392 | public static function generateGuid() |
|
73 | |||
74 | /** |
||
75 | * Check if the $guid existed in current database table. |
||
76 | * @param string $guid the GUID to be checked. |
||
77 | * @return boolean Whether the $guid exists or not. |
||
78 | */ |
||
79 | 3 | public static function checkGuidExists($guid) |
|
83 | |||
84 | /** |
||
85 | * Get the rules associated with GUID attribute. |
||
86 | * @return array GUID rules. |
||
87 | */ |
||
88 | 346 | public function getGUIDRules() |
|
89 | { |
||
90 | 346 | $rules = []; |
|
91 | 346 | if (is_string($this->guidAttribute) && !empty($this->guidAttribute)) { |
|
92 | $rules = [ |
||
93 | 325 | [[$this->guidAttribute], 'required',], |
|
94 | 325 | [[$this->guidAttribute], 'unique',], |
|
95 | 325 | [[$this->guidAttribute], 'string', 'max' => 16], |
|
96 | ]; |
||
97 | } |
||
98 | 346 | return $rules; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get GUID, in spite of guid attribute name. |
||
103 | * @return string |
||
104 | */ |
||
105 | 247 | public function getGUID() |
|
110 | |||
111 | /** |
||
112 | * Get Readable GUID. |
||
113 | * @return string |
||
114 | */ |
||
115 | 15 | public function getReadableGUID() |
|
123 | |||
124 | /** |
||
125 | * Set guid, in spite of guid attribute name. |
||
126 | * @param string $guid |
||
127 | * @return string |
||
128 | */ |
||
129 | 377 | public function setGUID($guid) |
|
137 | |||
138 | /** |
||
139 | * Composite GUIDs from models. |
||
140 | * @param array|string $models |
||
141 | * @return array |
||
142 | */ |
||
143 | 51 | public static function compositeGUIDs($models) |
|
168 | } |
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.