Completed
Push — master ( 366bee...9480a7 )
by grégoire
02:10
created

ValidationException   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addException() 0 6 1
A hasExceptions() 0 4 1
A getExceptions() 0 4 1
A getFancyMessage() 0 4 1
B getSubFancyMessage() 0 26 4
A __toString() 0 4 1
1
<?php
2
/*
3
 * This file is part of Chanmix51’s ParameterJuicer package.
4
 *
5
 * (c) 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Chanmix51\ParameterJuicer\Exception;
11
12
/**
13
 * ValidationException
14
 *
15
 * Store exceptions from the validation process.
16
 *
17
 * @package     ParameterJuicer
18
 * @copyright   2017 Grégoire HUBERT
19
 * @author      Grégoire HUBERT <[email protected]>
20
 * @license     X11 {@link http://opensource.org/licenses/mit-license.php}
21
 *
22
 * @see         \Exception
23
 */
24
class ValidationException extends ParameterJuicerException
25
{
26
    /** @var  bool  Indicate if messages have been set in the current exception.*/
27
    private $exceptions = [];
28
29
    /**
30
     * addMessage
31
     *
32
     * Add a new message to the exception.
33
     */
34
    public function addException(string $field, ValidationException $exception): self
35
    {
36
        $this->exceptions[$field][] = $exception;
37
38
        return $this;
39
    }
40
41
    /**
42
     * hasExceptions
43
     *
44
     * Indicates if yes or no some exceptions have been set.
45
     */
46
    public function hasExceptions(): int
47
    {
48
        return (bool) (count($this->exceptions) > 0);
49
    }
50
51
    /**
52
     * getExceptions
53
     *
54
     * Return the list of exceptions.
55
     */
56
    public function getExceptions(): array
57
    {
58
        return $this->exceptions;
59
    }
60
61
    /**
62
     * getFancyMessage
63
     *
64
     * Output a nicely formatted validation error messages.
65
     */
66
    public function getFancyMessage(): string
67
    {
68
        return sprintf("%s\n", $this->getMessage()) . $this->getSubFancyMessage();
69
    }
70
71
    /**
72
     * getSubFancyMessage
73
     *
74
     * Subrouting to display validation errors.
75
     */
76
    public function getSubFancyMessage(int $level = 0): string
77
    {
78
        $output = '';
79
80
        foreach ($this->exceptions as $field => $exceptions) {
0 ignored issues
show
Bug introduced by
The expression $this->exceptions of type boolean is not traversable.
Loading history...
81
            $output .= sprintf(
82
                "%s[%s] - %s\n",
83
                str_repeat(' ', $level * 4 + 2),
84
                $field,
85
                join(
86
                    ' | ',
87
                    array_map(
88
                        function(ValidationException $e) { return $e->getMessage(); },
89
                        $exceptions
90
                    )
91
                )
92
            );
93
            foreach ($exceptions as $exception) {
94
                if ($exception->hasExceptions()) {
95
                    $output .= $exception->getSubFancyMessage($level + 1);
96
                }
97
            }
98
        }
99
100
        return $output;
101
    }
102
103
    /**
104
     * __toString
105
     *
106
     * String representation of this exception.
107
     */
108
    public function __toString(): string
109
    {
110
        return $this->getFancyMessage();
111
    }
112
}
113