Completed
Push — master ( b4c757...366bee )
by grégoire
02:04
created

ValidationException::addException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 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(ValidationException $exception): self
35
    {
36
        $this->exceptions[] = $exception;
37
        $this->message = sprintf(
38
            "%s\n%s",
39
            $this->message,
40
            $exception->getMessage()
41
        );
42
43
        return $this;
44
    }
45
46
    /**
47
     * hasExceptions
48
     *
49
     * Indicates if yes or no some exceptions have been set.
50
     */
51
    public function hasExceptions(): int
52
    {
53
        return (bool) (count($this->exceptions) > 0);
54
    }
55
56
    /**
57
     * getExceptions
58
     *
59
     * Return the list of exceptions.
60
     */
61
    public function getExceptions(): array
62
    {
63
        return $this->exceptions;
64
    }
65
66
    /**
67
     * getMessages
68
     *
69
     * Return an array of the embeded exceptions’ messages.
70
     */
71
    public function getMessages(): array
72
    {
73
        return array_map(
74
            $this->exceptions,
75
            function(\Exception $e) {
76
                return $e->getMessage();
77
            }
78
        );
79
    }
80
}
81