- <?php
-
- namespace common\helpers;
-
- class Ajax
- {
- const RESPONSE_TYPE_ERROR = 'error';
- const RESPONSE_TYPE_SUCCESS = 'success';
-
- public static function responseSuccess(string $message, array $datas = []): array
- {
- return self::response(self::RESPONSE_TYPE_SUCCESS, $message, $datas);
- }
-
- public static function responseError(string $message, array $datas = []): array
- {
- return self::response(self::RESPONSE_TYPE_ERROR, $message, $datas);
- }
-
- private static function response(string $responseType, string $message, array $datas = []): array
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- return [
- 'return' => $responseType,
- 'alert' => [
- 'type' => self::getAlertClass($responseType),
- 'message' => $message
- ],
- 'datas' => $datas
- ];
- }
-
- private static function getAlertClass(string $responseType)
- {
- $class = '';
- if($responseType == self::RESPONSE_TYPE_SUCCESS) {
- $class = 'success';
- }
- elseif($responseType == self::RESPONSE_TYPE_ERROR) {
- $class = 'danger';
- }
-
- return $class;
- }
- }
|