ValidationResult   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 90
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isValid() 0 4 1
A isNotValid() 0 4 1
A getMessages() 0 10 3
A getFailures() 0 4 1
A getValues() 0 4 1
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator;
10
11
/**
12
 * The ValidationResult class holds the validation result and the validation messages.
13
 *
14
 * @package Particle\Validator
15
 */
16
class ValidationResult
17
{
18
    /**
19
     * @var bool
20
     */
21
    protected $isValid;
22
23
    /**
24
     * @var array
25
     */
26
    protected $messages;
27
28
    /**
29
     * @var array
30
     */
31
    protected $values;
32
33
    /**
34
     * @var Failure[]
35
     */
36
    protected $failures;
37
38
    /**
39
     * Construct the validation result.
40
     *
41
     * @param bool $isValid
42
     * @param array $failures
43
     * @param array $values
44
     */
45 261
    public function __construct($isValid, array $failures, array $values)
46
    {
47 261
        $this->isValid = $isValid;
48 261
        $this->failures = $failures;
49 261
        $this->values = $values;
50 261
    }
51
52
    /**
53
     * Returns whether or not the validator has validated the values.
54
     *
55
     * @return bool
56
     */
57 255
    public function isValid()
58
    {
59 255
        return $this->isValid;
60
    }
61
62
    /**
63
     * Returns whether or not the validator has validated the values.
64
     *
65
     * @return bool
66
     */
67 1
    public function isNotValid()
68
    {
69 1
        return !$this->isValid;
70
    }
71
72
    /**
73
     * Returns the array of messages that were collected during validation.
74
     *
75
     * @return array
76
     */
77 185
    public function getMessages()
78
    {
79 185
        if ($this->messages === null) {
80 185
            $this->messages = [];
81 185
            foreach ($this->failures as $failure) {
82 122
                $this->messages[$failure->getKey()][$failure->getReason()] = $failure->format();
83 185
            }
84 185
        }
85 185
        return $this->messages;
86
    }
87
88
    /**
89
     * @return Failure[]
90
     */
91 3
    public function getFailures()
92
    {
93 3
        return $this->failures;
94
    }
95
96
    /**
97
     * Returns all validated values
98
     *
99
     * @return array
100
     */
101 5
    public function getValues()
102
    {
103 5
        return $this->values;
104
    }
105
}
106