Completed
Branch Array_Validator (c5246d)
by Philip
05:35
created

Collection::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Valdi\Validator;
13
14
use Valdi\ValidationException;
15
use Valdi\Validator;
16
17
/**
18
 * Validator for array values fulfilling a rule.
19
 */
20
class Collection implements ValidatorInterface {
21
22
23
    /**
24
     * Holds the invalid array values.
25
     */
26
    protected $invalidDetails;
27
28
    /**
29
     * Checks whether the given parameters fulfil:
30
     * - At two given
31
     * - The first one is a Validator or a subclass of it
32
     *
33
     * @param array $parameters
34
     * the validation parameters
35
     *
36
     * @throws ValidationException
37
     * thrown if the amount of parameters is not equal two or the first parameter is not a Validator
38
     */
39 View Code Duplication
    protected function checkParameters(array $parameters) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
        if (count($parameters) !== 2) {
41
            throw new ValidationException('"collection" expects two parameters.');
42
        }
43
        if (!($parameters[0] instanceof Validator)) {
44
            throw new ValidationException('"collection" expects the first parameter to be an instance of a Validator.');
45
        }
46
    }
47
48
    /**
49
     * Checks whether the given values are of the expected array.
50
     *
51
     * @param mixed $values
52
     * the potential array values to check
53
     * @param Validator $validator
54
     * the validator to check with
55
     * @param array $rule
56
     * the rule which each element must fulfill
57
     *
58
     * @return boolean
59
     * true if all the $values are a valid array, else false with the invalid details set
60
     */
61
    protected function isValidCollection($values, Validator $validator, array $rule) {
62
        if (!is_array($values)) {
63
            $this->invalidDetails = $values;
64
            return false;
65
        }
66
67
        $this->invalidDetails = [];
68
        foreach ($values as $key => $value) {
69
            $elementValidation = $validator->isValid(['value' => [$rule]], ['value' => $value]);
70
            if (!$elementValidation['valid']) {
71
                $this->invalidDetails[$key] = $elementValidation['errors']['value'][0];
72
            }
73
        }
74
        return count($this->invalidDetails) === 0;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function isValid($value, array $parameters) {
81
        $this->checkParameters($parameters);
82
        return in_array($value, ['', null], true) ||
83
            $this->isValidCollection($value, $parameters[0], $parameters[1]);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getInvalidDetails() {
90
        return ['collection' => $this->invalidDetails];
91
    }
92
}
93