mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Удалён лишний компонент ApiNormalizer
This commit is contained in:
@ -79,7 +79,7 @@ class AccountsController extends Controller {
|
|||||||
if (!$model->changePassword()) {
|
if (!$model->changePassword()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ class AccountsController extends Controller {
|
|||||||
if (!$model->change()) {
|
if (!$model->change()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ class AccountsController extends Controller {
|
|||||||
if (!$model->sendCurrentEmailConfirmation()) {
|
if (!$model->sendCurrentEmailConfirmation()) {
|
||||||
$data = [
|
$data = [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
|
|
||||||
if (ArrayHelper::getValue($data['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
if (ArrayHelper::getValue($data['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
||||||
@ -136,7 +136,7 @@ class AccountsController extends Controller {
|
|||||||
if (!$model->sendNewEmailConfirmation()) {
|
if (!$model->sendNewEmailConfirmation()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ class AccountsController extends Controller {
|
|||||||
if (!$model->changeEmail()) {
|
if (!$model->changeEmail()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +171,7 @@ class AccountsController extends Controller {
|
|||||||
if (!$model->applyLanguage()) {
|
if (!$model->applyLanguage()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ class AccountsController extends Controller {
|
|||||||
if (!$model->agreeWithLatestRules()) {
|
if (!$model->agreeWithLatestRules()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ class AuthenticationController extends Controller {
|
|||||||
if (($result = $model->login()) === false) {
|
if (($result = $model->login()) === false) {
|
||||||
$data = [
|
$data = [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
|
|
||||||
if (ArrayHelper::getValue($data['errors'], 'login') === E::ACCOUNT_NOT_ACTIVATED) {
|
if (ArrayHelper::getValue($data['errors'], 'login') === E::ACCOUNT_NOT_ACTIVATED) {
|
||||||
@ -83,7 +83,7 @@ class AuthenticationController extends Controller {
|
|||||||
if ($model->forgotPassword() === false) {
|
if ($model->forgotPassword() === false) {
|
||||||
$data = [
|
$data = [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
|
|
||||||
if (ArrayHelper::getValue($data['errors'], 'login') === E::RECENTLY_SENT_MESSAGE) {
|
if (ArrayHelper::getValue($data['errors'], 'login') === E::RECENTLY_SENT_MESSAGE) {
|
||||||
@ -119,7 +119,7 @@ class AuthenticationController extends Controller {
|
|||||||
if (($result = $model->recoverPassword()) === false) {
|
if (($result = $model->recoverPassword()) === false) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ class AuthenticationController extends Controller {
|
|||||||
if (($result = $model->renew()) === false) {
|
if (($result = $model->renew()) === false) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace api\controllers;
|
namespace api\controllers;
|
||||||
|
|
||||||
use api\traits\ApiNormalize;
|
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\filters\auth\HttpBearerAuth;
|
use yii\filters\auth\HttpBearerAuth;
|
||||||
|
|
||||||
@ -12,7 +11,6 @@ use yii\filters\auth\HttpBearerAuth;
|
|||||||
* @mixin \yii\filters\auth\CompositeAuth
|
* @mixin \yii\filters\auth\CompositeAuth
|
||||||
*/
|
*/
|
||||||
class Controller extends \yii\rest\Controller {
|
class Controller extends \yii\rest\Controller {
|
||||||
use ApiNormalize;
|
|
||||||
|
|
||||||
public function behaviors() {
|
public function behaviors() {
|
||||||
$parentBehaviors = parent::behaviors();
|
$parentBehaviors = parent::behaviors();
|
||||||
@ -22,10 +20,11 @@ class Controller extends \yii\rest\Controller {
|
|||||||
'user' => Yii::$app->getUser(),
|
'user' => Yii::$app->getUser(),
|
||||||
];
|
];
|
||||||
|
|
||||||
// xml нам не понадобится
|
// xml и rate limiter нам не понадобятся
|
||||||
unset($parentBehaviors['contentNegotiator']['formats']['application/xml']);
|
unset(
|
||||||
// rate limiter здесь не применяется
|
$parentBehaviors['contentNegotiator']['formats']['application/xml'],
|
||||||
unset($parentBehaviors['rateLimiter']);
|
$parentBehaviors['rateLimiter']
|
||||||
|
);
|
||||||
|
|
||||||
return $parentBehaviors;
|
return $parentBehaviors;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ class FeedbackController extends Controller {
|
|||||||
if (!$model->sendMessage()) {
|
if (!$model->sendMessage()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ class SignupController extends Controller {
|
|||||||
if (!$model->signup()) {
|
if (!$model->signup()) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ class SignupController extends Controller {
|
|||||||
if (!$model->sendRepeatMessage()) {
|
if (!$model->sendRepeatMessage()) {
|
||||||
$response = [
|
$response = [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
|
|
||||||
if (ArrayHelper::getValue($response['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
if (ArrayHelper::getValue($response['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
||||||
@ -83,7 +83,7 @@ class SignupController extends Controller {
|
|||||||
if (!($result = $model->confirm())) {
|
if (!($result = $model->confirm())) {
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'errors' => $model->getFirstErrors(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Reference in New Issue
Block a user