Completed
Push — master ( c915d2...2ad4b2 )
by Alexander
04:32
created

ValidationFailedException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 63
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setValidationMessages() 0 11 2
A getValidationMessages() 0 4 1
1
<?php
2
3
namespace Flugg\Responder\Exceptions;
4
5
use Illuminate\Contracts\Validation\Validator;
6
7
class ValidationFailedException extends ApiException
8
{
9
    /**
10
     * The HTTP status code.
11
     *
12
     * @var int
13
     */
14
    protected $statusCode = 422;
15
16
    /**
17
     * The error code used for API responses.
18
     *
19
     * @var string
20
     */
21
    protected $errorCode = 'validation_failed';
22
23
    /**
24
     * The array of validation error messages.
25
     *
26
     * @var array
27
     */
28
    protected $validationMessages = [ ];
29
30
    /**
31
     * Create a new exception instance.
32
     *
33
     * @param Validator $validator
34
     */
35
    public function __construct( Validator $validator )
36
    {
37
        $this->setValidationMessages( $validator );
38
39
        parent::__construct( 'Form request validation has failed.' );
40
    }
41
42
    /**
43
     * Set the array of validation error messages.
44
     *
45
     * @param  Validator $validator
46
     * @return void
47
     */
48
    public function setValidationMessages( Validator $validator )
49
    {
50
        $messages = [ ];
51
        $attributes = $validator->getMessageBag()->toArray();
52
53
        foreach ( $attributes as $attribute ) {
54
            $messages = array_merge( $messages, $attribute );
55
        }
56
57
        $this->validationMessages = $messages;
58
    }
59
60
    /**
61
     * Get the array of validation error messages.
62
     *
63
     * @return array
64
     */
65
    public function getValidationMessages()
66
    {
67
        return $this->validationMessages;
68
    }
69
}