@@ -44,25 +44,74 @@ final class Model {
4444 */
4545 private static $ annotations = array ();
4646
47+ /**
48+ * Gets or sets type of the current model.
49+ *
50+ * @var string|object
51+ */
52+ private static $ modelType ;
53+
54+ /**
55+ * Specifies the model type for the action.
56+ *
57+ * @param string|string[] $actionName The action name.
58+ * @param string|object $modelType The type name of model.
59+ *
60+ * @return void
61+ */
62+ public static function use ($ actionName , $ modelType ) {
63+ if (empty ($ actionName )) {
64+ throw new \Exception ('$actionName must not be empty. ' );
65+ }
66+
67+ if (empty ($ modelType )) {
68+ throw new \Exception ('$modelType must not be empty. ' );
69+ }
70+
71+ $ canUse = false ;
72+
73+ if ($ actionName !== '. ' ) {
74+ if (!is_array ($ actionName )) {
75+ $ actionName = array ($ actionName );
76+ }
77+
78+ foreach ($ actionName as $ action ) {
79+ if (self ::$ actionContext ->actionNameEquals ($ action )) {
80+ $ canUse = true ;
81+ break ;
82+ }
83+ }
84+ }
85+ else {
86+ $ canUse = true ;
87+ }
88+
89+ if (!$ canUse ) {
90+ return ;
91+ }
92+
93+ if (is_object ($ modelType )) {
94+ $ modelType = get_class ($ modelType );
95+ }
96+
97+ self ::$ modelType = $ modelType ;
98+ }
99+
47100 /**
48101 * Marks the specified property with the attribute "requred"
49102 * and it is expected that the property value must be specified.
50103 *
51- * @param string $actionName Action name.
104+ * @param string|object $modelType The name type of model .
52105 * @param string $propertyName The property name.
53106 * @param string $errorMessage The error message.
54107 *
55108 * @return void
56109 */
57- public static function required ($ actionName , $ propertyName , $ errorMessage = null ) {
58- if (!self ::$ actionContext -> actionNameEquals ( $ actionName )) {
110+ public static function required ($ modelType , $ propertyName , $ errorMessage = null ) {
111+ if (!self ::canAnnotate ( $ modelType )) {
59112 return ;
60113 }
61114
62- if (empty ($ actionName )) {
63- throw new \Exception ('$actionName must not be empty. ' );
64- }
65-
66115 if (empty ($ propertyName )) {
67116 throw new \Exception ('$propertyName must not be empty. ' );
68117 }
@@ -75,22 +124,18 @@ public static function required($actionName, $propertyName, $errorMessage = null
75124 /**
76125 * Sets a property whose value needs to be compared to the value of the specified property.
77126 *
78- * @param string $actionName Action name.
127+ * @param string|object $modelType The name type of model .
79128 * @param string $propertyName The property name.
80129 * @param string $compareWith The name of the property with which to compare.
81130 * @param string $errorMessage The error message.
82131 *
83132 * @return void
84133 */
85- public static function compare ($ actionName , $ propertyName , $ compareWith , $ errorMessage = null ) {
86- if (!self ::$ actionContext -> actionNameEquals ( $ actionName )) {
134+ public static function compare ($ modelType , $ propertyName , $ compareWith , $ errorMessage = null ) {
135+ if (!self ::canAnnotate ( $ modelType )) {
87136 return ;
88137 }
89138
90- if (empty ($ actionName )) {
91- throw new \Exception ('$actionName must not be empty. ' );
92- }
93-
94139 if (empty ($ propertyName )) {
95140 throw new \Exception ('$propertyName must not be empty. ' );
96141 }
@@ -111,23 +156,19 @@ public static function compare($actionName, $propertyName, $compareWith, $errorM
111156 /**
112157 * Specifies the minimum and maximum length of characters that are allowed in a data field.
113158 *
114- * @param string $actionName Action name.
159+ * @param string|object $modelType The name type of model .
115160 * @param string $propertyName The property name to check.
116161 * @param int $maxLength The maximum length of a string.
117162 * @param int $minLength The minimum length of a string.
118163 * @param string The error message.
119164 *
120165 * @return void
121166 */
122- public static function stringLength ($ actionName , $ propertyName , $ maxLength , $ minLength = null , $ errorMessage = null ) {
123- if (!self ::$ actionContext -> actionNameEquals ( $ actionName )) {
167+ public static function stringLength ($ modelType , $ propertyName , $ maxLength , $ minLength = null , $ errorMessage = null ) {
168+ if (!self ::canAnnotate ( $ modelType )) {
124169 return ;
125170 }
126171
127- if (empty ($ actionName )) {
128- throw new \Exception ('$actionName must not be empty. ' );
129- }
130-
131172 if (empty ($ propertyName )) {
132173 throw new \Exception ('$propertyName must not be empty. ' );
133174 }
@@ -140,23 +181,19 @@ public static function stringLength($actionName, $propertyName, $maxLength, $min
140181 /**
141182 * Specifies the numeric range constraints for the value of a data field.
142183 *
143- * @param string $actionName Action name.
184+ * @param string|object $modelType The name type of model .
144185 * @param string $propertyName The property name to check.
145186 * @param int $max The maximum allowed field value.
146187 * @param int $min The minimum allowed field value.
147188 * @param string The error message.
148189 *
149190 * @return void
150191 */
151- public static function range ($ actionName , $ propertyName , $ min , $ max , $ errorMessage = null ) {
152- if (!self ::$ actionContext -> actionNameEquals ( $ actionName )) {
192+ public static function range ($ modelType , $ propertyName , $ min , $ max , $ errorMessage = null ) {
193+ if (!self ::canAnnotate ( $ modelType )) {
153194 return ;
154195 }
155196
156- if (empty ($ actionName )) {
157- throw new \Exception ('$actionName must not be empty. ' );
158- }
159-
160197 if (empty ($ propertyName )) {
161198 throw new \Exception ('$propertyName must not be empty. ' );
162199 }
@@ -173,7 +210,7 @@ public static function range($actionName, $propertyName, $min, $max, $errorMessa
173210 /**
174211 * Specifies a custom validation method that is used to validate the property.
175212 *
176- * @param string $actionName Action name.
213+ * @param string|object $modelType The name type of model .
177214 * @param string $propertyName The property name to check.
178215 * @param callback $callback Function to valid the value.
179216 * The function takes a value of the property and must return true,
@@ -182,15 +219,11 @@ public static function range($actionName, $propertyName, $min, $max, $errorMessa
182219 *
183220 * @return void
184221 */
185- public static function validation ($ actionName , $ propertyName , $ callback , $ errorMessage = null ) {
186- if (!self ::$ actionContext -> actionNameEquals ( $ actionName )) {
222+ public static function validation ($ modelType , $ propertyName , $ callback , $ errorMessage = null ) {
223+ if (!self ::canAnnotate ( $ modelType )) {
187224 return ;
188225 }
189226
190- if (empty ($ actionName )) {
191- throw new \Exception ('$actionName must not be empty. ' );
192- }
193-
194227 if (empty ($ propertyName )) {
195228 throw new \Exception ('$propertyName must not be empty. ' );
196229 }
@@ -207,22 +240,18 @@ public static function validation($actionName, $propertyName, $callback, $errorM
207240 /**
208241 * Sets a values that is used for display in the UI.
209242 *
210- * @param string $actionName Action name.
243+ * @param string|object $modelType The name type of model .
211244 * @param string $propertyName The property name to set.
212245 * @param string $name The value that is used for display in the UI.
213246 * @param string $text The value that is used to display a description in the UI.
214247 *
215248 * @return void
216249 */
217- public static function display ($ actionName , $ propertyName , $ name , $ text = null ) {
218- if (!self ::$ actionContext -> actionNameEquals ( $ actionName )) {
250+ public static function display ($ modelType , $ propertyName , $ name , $ text = null ) {
251+ if (!self ::canAnnotate ( $ modelType )) {
219252 return ;
220253 }
221254
222- if (empty ($ actionName )) {
223- throw new \Exception ('$actionName must not be empty. ' );
224- }
225-
226255 if (empty ($ propertyName )) {
227256 throw new \Exception ('$propertyName must not be empty. ' );
228257 }
@@ -233,6 +262,18 @@ public static function display($actionName, $propertyName, $name, $text = null)
233262 self ::$ annotations [$ propertyName ]->displayText = $ text ;
234263 }
235264
265+ private static function canAnnotate ($ modelType ) {
266+ if (empty ($ modelType )) {
267+ throw new \Exception ('$modelType must not be empty. ' );
268+ }
269+
270+ if (is_object ($ modelType )) {
271+ $ modelType = get_class ($ modelType );
272+ }
273+
274+ return self ::$ modelType === $ modelType ;
275+ }
276+
236277 private static function makeDataAnnotation (&$ propertyName ) {
237278 $ propertyName = is_array ($ propertyName ) ? implode ('_ ' , $ propertyName ) : $ propertyName ;
238279
0 commit comments