ValidationException::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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  array contains all the set’s exceptions */
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(): bool
47
    {
48
        return 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) {
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) {
89
                            return $e->getMessage();
90
                        },
91
                        $exceptions
92
                    )
93
                )
94
            );
95
            foreach ($exceptions as $exception) {
96
                if ($exception->hasExceptions()) {
97
                    $output .= $exception->getSubFancyMessage($level + 1);
98
                }
99
            }
100
        }
101
102
        return $output;
103
    }
104
105
    /**
106
     * __toString
107
     *
108
     * String representation of this exception.
109
     */
110
    public function __toString(): string
111
    {
112
        return $this->getFancyMessage();
113
    }
114
}
115