Удалён лишний компонент ApiNormalizer

This commit is contained in:
ErickSkrauch 2016-11-30 20:57:30 +03:00
parent ce18d07ae2
commit 5de6fa91c8
7 changed files with 20 additions and 82 deletions

View File

@ -79,7 +79,7 @@ class AccountsController extends Controller {
if (!$model->changePassword()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}
@ -94,7 +94,7 @@ class AccountsController extends Controller {
if (!$model->change()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}
@ -110,7 +110,7 @@ class AccountsController extends Controller {
if (!$model->sendCurrentEmailConfirmation()) {
$data = [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
if (ArrayHelper::getValue($data['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
@ -136,7 +136,7 @@ class AccountsController extends Controller {
if (!$model->sendNewEmailConfirmation()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}
@ -152,7 +152,7 @@ class AccountsController extends Controller {
if (!$model->changeEmail()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}
@ -171,7 +171,7 @@ class AccountsController extends Controller {
if (!$model->applyLanguage()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}
@ -187,7 +187,7 @@ class AccountsController extends Controller {
if (!$model->agreeWithLatestRules()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}

View File

@ -53,7 +53,7 @@ class AuthenticationController extends Controller {
if (($result = $model->login()) === false) {
$data = [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
if (ArrayHelper::getValue($data['errors'], 'login') === E::ACCOUNT_NOT_ACTIVATED) {
@ -83,7 +83,7 @@ class AuthenticationController extends Controller {
if ($model->forgotPassword() === false) {
$data = [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
if (ArrayHelper::getValue($data['errors'], 'login') === E::RECENTLY_SENT_MESSAGE) {
@ -119,7 +119,7 @@ class AuthenticationController extends Controller {
if (($result = $model->recoverPassword()) === false) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}
@ -134,7 +134,7 @@ class AuthenticationController extends Controller {
if (($result = $model->renew()) === false) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}

View File

@ -1,7 +1,6 @@
<?php
namespace api\controllers;
use api\traits\ApiNormalize;
use Yii;
use yii\filters\auth\HttpBearerAuth;
@ -12,7 +11,6 @@ use yii\filters\auth\HttpBearerAuth;
* @mixin \yii\filters\auth\CompositeAuth
*/
class Controller extends \yii\rest\Controller {
use ApiNormalize;
public function behaviors() {
$parentBehaviors = parent::behaviors();
@ -22,10 +20,11 @@ class Controller extends \yii\rest\Controller {
'user' => Yii::$app->getUser(),
];
// xml нам не понадобится
unset($parentBehaviors['contentNegotiator']['formats']['application/xml']);
// rate limiter здесь не применяется
unset($parentBehaviors['rateLimiter']);
// xml и rate limiter нам не понадобятся
unset(
$parentBehaviors['contentNegotiator']['formats']['application/xml'],
$parentBehaviors['rateLimiter']
);
return $parentBehaviors;
}

View File

@ -27,7 +27,7 @@ class FeedbackController extends Controller {
if (!$model->sendMessage()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}

View File

@ -43,7 +43,7 @@ class SignupController extends Controller {
if (!$model->signup()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}
@ -58,7 +58,7 @@ class SignupController extends Controller {
if (!$model->sendRepeatMessage()) {
$response = [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
if (ArrayHelper::getValue($response['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
@ -83,7 +83,7 @@ class SignupController extends Controller {
if (!($result = $model->confirm())) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
'errors' => $model->getFirstErrors(),
];
}

View File

@ -1,26 +0,0 @@
<?php
namespace api\traits;
trait ApiNormalize {
/**
* Метод убирает все ошибки для поля, кроме первой и возвращает значения в формате
* [
* 'field1' => 'first_error_of_field1',
* 'field2' => 'first_error_of_field2',
* ]
*
* @param array $errors
* @return array
*/
public function normalizeModelErrors(array $errors) {
$normalized = [];
foreach($errors as $attribute => $attrErrors) {
$normalized[$attribute] = $attrErrors[0];
}
return $normalized;
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace tests\codeception\api\traits;
use api\traits\ApiNormalize;
use tests\codeception\api\unit\TestCase;
class ApiNormalizeTestClass {
use ApiNormalize;
}
class ApiNormalizerTest extends TestCase {
public function testNormalizeModelErrors() {
$object = new ApiNormalizeTestClass();
$normalized = $object->normalizeModelErrors([
'rulesAgreement' => [
'error.you_must_accept_rules',
],
'email' => [
'error.email_required',
],
'username' => [
'error.username_too_short',
'error.username_not_unique',
],
]);
$this->assertEquals([
'rulesAgreement' => 'error.you_must_accept_rules',
'email' => 'error.email_required',
'username' => 'error.username_too_short',
], $normalized);
}
}