Skip to content

Commit

Permalink
Added new sendArrayError() method to luya\rest\Controller. Moved helper
Browse files Browse the repository at this point in the history
classes into RestBehvaiorsTrait as they can be used in both situations.
  • Loading branch information
nadar committed Jan 22, 2018
1 parent b251779 commit fd4d508
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 49 deletions.
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Expand Up @@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file. This projec

- [#1754](https://github.com/luyadev/luya/issues/1754) Remove underscore when transliteration is disabled.

### Added

- Added new sendArrayError() method to luya\rest\Controller. Moved helper classes into RestBehvaiorsTrait as they can be used in both situations.

## 1.0.2 (17. January 2018)

### Changed
Expand Down
48 changes: 0 additions & 48 deletions core/rest/Controller.php
Expand Up @@ -2,10 +2,7 @@

namespace luya\rest;

use Yii;
use yii\base\Model;
use luya\traits\RestBehaviorsTrait;
use yii\base\InvalidParamException;

/**
* Basic Rest Controller.
Expand Down Expand Up @@ -47,49 +44,4 @@
class Controller extends \yii\rest\Controller
{
use RestBehaviorsTrait;

/**
* Send Model errors with correct headers.
*
* Helper method to correctly send model errors with the correct response headers.
*
* Example return value:
*
* ```php
* Array
* (
* [0] => Array
* (
* [field] => firstname
* [message] => Firstname cannot be blank.
* )
* [1] => Array
* (
* [field] => email
* [message] => Email cannot be blank.
* )
* )
* ```
*
* @param \yii\base\Model $model The model to find the first error.
* @throws \yii\base\InvalidParamException
* @return array If the model has errors InvalidParamException will be thrown
*/
public function sendModelError(Model $model)
{
if (!$model->hasErrors()) {
throw new InvalidParamException('The model as thrown an uknown Error.');
}

Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
$result = [];
foreach ($model->getFirstErrors() as $name => $message) {
$result[] = [
'field' => $name,
'message' => $message,
];
}

return $result;
}
}
89 changes: 88 additions & 1 deletion core/traits/RestBehaviorsTrait.php
Expand Up @@ -8,8 +8,10 @@
use yii\filters\auth\QueryParamAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use luya\rest\UserBehaviorInterface;
use yii\filters\Cors;
use yii\base\Model;
use yii\base\InvalidParamException;
use luya\rest\UserBehaviorInterface;

/**
* Rest Behaviors Trait.
Expand Down Expand Up @@ -112,4 +114,89 @@ public function behaviors()

return $behaviors;
}

/**
* Send Model errors with correct headers.
*
* Helper method to correctly send model errors with the correct response headers.
*
* Example return value:
*
* ```php
* Array
* (
* [0] => Array
* (
* [field] => firstname
* [message] => Firstname cannot be blank.
* )
* [1] => Array
* (
* [field] => email
* [message] => Email cannot be blank.
* )
* )
* ```
*
* @param \yii\base\Model $model The model to find the first error.
* @throws \yii\base\InvalidParamException
* @return array If the model has errors InvalidParamException will be thrown, otherwise an array with message and field key.
*/
public function sendModelError(Model $model)
{
if (!$model->hasErrors()) {
throw new InvalidParamException('The model as thrown an uknown Error.');
}

Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
$result = [];
foreach ($model->getFirstErrors() as $name => $message) {
$result[] = [
'field' => $name,
'message' => $message,
];
}

return $result;
}

/**
* Send Array validation error.
*
* Example input:
*
* ```php
* return $this->sendArrayError(['firstname' => 'Firstname cannot be blank']);
* ```
*
* Example return value:
*
* ```php
* Array
* (
* [0] => Array
* (
* [field] => firstname
* [message] => Firstname cannot be blank.
* )
* )
* ```
* @param array $errors Provide an array with messages. Where key is the field and value the message.
* @return array Returns an array with field and message keys for each item.
* @since 1.0.3
*/
public function sendArrayError(array $errors)
{
Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
$result = [];
foreach ($errors as $key => $value) {
$messages = (array) $value;

foreach ($messages as $msg) {
$result[] = ['field' => $key, 'message' => $msg];
}
}

return $result;
}
}
14 changes: 14 additions & 0 deletions tests/core/rest/ControllerTest.php
Expand Up @@ -47,4 +47,18 @@ public function testSendModelError()
$this->assertSame('firstname', $response[0]['field']);
$this->arrayHasKey('message', $response[0]);
}

public function testSendArrayError()
{
$ctrl = new Controller('test', Yii::$app);

$this->assertSame([
['field' => 'field1', 'message' => 'message1'],
['field' => 'field2', 'message' => 'message2a'],
['field' => 'field2', 'message' => 'message2b'],
], $ctrl->sendArrayError([
'field1' => 'message1',
'field2' => ['message2a', 'message2b'],
]));
}
}

0 comments on commit fd4d508

Please sign in to comment.