1 | <?php |
||
33 | trait RestBehaviorsTrait |
||
34 | { |
||
35 | /** |
||
36 | * @var boolean Whether CORS is enabled or not. |
||
37 | */ |
||
38 | public $enableCors = false; |
||
39 | |||
40 | /** |
||
41 | * Whether the rest controller is protected or not. |
||
42 | * |
||
43 | * @return boolean|\yii\web\User |
||
44 | */ |
||
45 | private function getUserAuthClass() |
||
63 | |||
64 | /** |
||
65 | * Override the default {{yii\rest\Controller::behaviors()}} method. |
||
66 | * The following changes are differ to the base implementation: |
||
67 | * |
||
68 | * + If {{luya\rest\UserBehaviorInterface}} is **not** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is removed. |
||
69 | * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is enabled. |
||
70 | * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `contentNegotiator` behavior ({{yii\filters\ContentNegotiator}}) is enabled. |
||
71 | * + The `rateLimiter` behavior filter is **removed** by default. |
||
72 | * |
||
73 | * @return array Returns an array with registered behavior filters based on the implementation type. |
||
74 | */ |
||
75 | public function behaviors() |
||
117 | |||
118 | /** |
||
119 | * Send Model errors with correct headers. |
||
120 | * |
||
121 | * Helper method to correctly send model errors with the correct response headers. |
||
122 | * |
||
123 | * Example return value: |
||
124 | * |
||
125 | * ```php |
||
126 | * Array |
||
127 | * ( |
||
128 | * [0] => Array |
||
129 | * ( |
||
130 | * [field] => firstname |
||
131 | * [message] => Firstname cannot be blank. |
||
132 | * ) |
||
133 | * [1] => Array |
||
134 | * ( |
||
135 | * [field] => email |
||
136 | * [message] => Email cannot be blank. |
||
137 | * ) |
||
138 | * ) |
||
139 | * ``` |
||
140 | * |
||
141 | * @param \yii\base\Model $model The model to find the first error. |
||
142 | * @throws \yii\base\InvalidParamException |
||
143 | * @return array If the model has errors InvalidParamException will be thrown, otherwise an array with message and field key. |
||
144 | */ |
||
145 | public function sendModelError(Model $model) |
||
162 | |||
163 | /** |
||
164 | * Send Array validation error. |
||
165 | * |
||
166 | * Example input: |
||
167 | * |
||
168 | * ```php |
||
169 | * return $this->sendArrayError(['firstname' => 'Firstname cannot be blank']); |
||
170 | * ``` |
||
171 | * |
||
172 | * Example return value: |
||
173 | * |
||
174 | * ```php |
||
175 | * Array |
||
176 | * ( |
||
177 | * [0] => Array |
||
178 | * ( |
||
179 | * [field] => firstname |
||
180 | * [message] => Firstname cannot be blank. |
||
181 | * ) |
||
182 | * ) |
||
183 | * ``` |
||
184 | * @param array $errors Provide an array with messages. Where key is the field and value the message. |
||
185 | * @return array Returns an array with field and message keys for each item. |
||
186 | * @since 1.0.3 |
||
187 | */ |
||
188 | public function sendArrayError(array $errors) |
||
202 | } |
||
203 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.