gwValidationException::getMessagesForFieldname()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 11
rs 9.4286
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Gwa\Exception;
4
5
/**
6
 * A validation exception.
7
 */
8
class gwValidationException extends gwCoreException
9
{
10
    /**
11
     * @var array
12
     */
13
    private $errors = [];
14
15
    const REQUIRED = 'gwValidationException::required';
16
    const INVALID  = 'gwValidationException::invalid';
17
    const METHOD_DOES_NOT_EXIST = 'gwValidationException::method_does_not_exist';
18
19
    public function __construct($message = 'gwValidationException::invalid', $info = null, $code = 0, \Exception $previous = null)
20
    {
21
        parent::__construct($message, $info, $code, $previous);
22
    }
23
24
    /**
25
     * @return array
26
     */
27
    public function getErrors()
28
    {
29
        return $this->errors;
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    public function getMessages()
36
    {
37
        $ret = [];
38
        foreach ($this->errors as $v) {
39
            $ret[] = $v->message;
40
        }
41
42
        return $ret;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getMessagesForFieldname($fieldname)
49
    {
50
        $ret = [];
51
        foreach ($this->errors as $v) {
52
            if ($v->field === $fieldname) {
53
                $ret[] = $v->message;
54
            }
55
        }
56
57
        return $ret;
58
    }
59
60
    /**
61
     * Add an error to this exception.
62
     *
63
     * @param string $fieldname
64
     * @param string $message
65
     */
66
    public function appendError($fieldname, $message)
67
    {
68
        $obj = new \stdClass();
69
        $obj->field = $fieldname;
70
        $obj->message = $message;
71
        $this->errors[] = $obj;
72
    }
73
74
    /**
75
     * @return boolean
76
     */
77
    public function hasErrors()
78
    {
79
        return count($this->errors) > 0;
80
    }
81
}
82