Completed
Push — master ( 5d9d81...59afcf )
by Basil
02:35
created

RestHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendModelError() 0 17 3
A sendArrayError() 0 14 3
1
<?php
2
3
namespace luya\helpers;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\base\InvalidParamException;
8
9
/**
10
 * Rest API Helper.
11
 * 
12
 * @since 1.0.20
13
 * @author Basil Suter <[email protected]>
14
 */
15
class RestHelper
16
{
17
    /**
18
     * Send Model errors with correct headers.
19
     *
20
     * Helper method to correctly send model errors with the correct response headers.
21
     *
22
     * Example return value:
23
     *
24
     * ```php
25
     * Array
26
     * (
27
     *     [0] => Array
28
     *         (
29
     *             [field] => firstname
30
     *             [message] => Firstname cannot be blank.
31
     *         )
32
     *     [1] => Array
33
     *         (
34
     *             [field] => email
35
     *             [message] => Email cannot be blank.
36
     *         )
37
     * )
38
     * ```
39
     *
40
     * @param \yii\base\Model $model The model to find the first error.
41
     * @throws \yii\base\InvalidParamException
42
     * @return array If the model has errors InvalidParamException will be thrown, otherwise an array with message and field key.
43
     */
44
    public function sendModelError(Model $model)
45
    {
46
        if (!$model->hasErrors()) {
47
            throw new InvalidParamException('The model as thrown an uknown Error.');
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
48
        }
49
        
50
        Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
51
        $result = [];
52
        foreach ($model->getFirstErrors() as $name => $message) {
53
            $result[] = [
54
                'field' => $name,
55
                'message' => $message,
56
            ];
57
        }
58
        
59
        return $result;
60
    }
61
    
62
    /**
63
     * Send Array validation error.
64
     *
65
     * Example input:
66
     *
67
     * ```php
68
     * return $this->sendArrayError(['firstname' => 'Firstname cannot be blank']);
69
     * ```
70
     *
71
     * Example return value:
72
     *
73
     * ```php
74
     * Array
75
     * (
76
     *     [0] => Array
77
     *         (
78
     *             [field] => firstname
79
     *             [message] => Firstname cannot be blank.
80
     *         )
81
     * )
82
     * ```
83
     * @param array $errors Provide an array with messages. Where key is the field and value the message.
84
     * @return array Returns an array with field and message keys for each item.
85
     * @since 1.0.3
86
     */
87
    public function sendArrayError(array $errors)
88
    {
89
        Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
90
        $result = [];
91
        foreach ($errors as $key => $value) {
92
            $messages = (array) $value;
93
            
94
            foreach ($messages as $msg) {
95
                $result[] = ['field' => $key, 'message' => $msg];
96
            }
97
        }
98
        
99
        return $result;
100
    }
101
}