Required::validateObject()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest\Validator;
19
20
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface;
21
use Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException;
22
23
/**
24
 * Checks if the specified list of properties names are keys in a given array
25
 * or properties in a given object.
26
 *
27
 * @author mcustiel
28
 */
29
class Required implements ValidatorInterface
30
{
31
    /**
32
     *
33
     * @var number[]|string[]
34
     */
35
    protected $items = [];
36
37 87
    public function setSpecification($specification = null)
38
    {
39 87
        $this->specificationIsArrayOrThrowException($specification);
40 85
        foreach ($specification as $item) {
41 85
            $this->specificationItemisValidOrThrowException($item);
42 84
        }
43
44 83
        $this->items = array_unique($specification);
45 83
    }
46
47 85
    private function specificationItemisValidOrThrowException($item)
48
    {
49 85
        if (!is_string($item) || is_numeric($item)) {
50 2
            throw new UnspecifiedValidatorException(
51
                'The validator Required is being initialized without a valid array'
52 2
            );
53
        }
54 84
    }
55
56 87
    private function specificationIsArrayOrThrowException($specification)
57
    {
58 87
        if (!is_array($specification) || empty($specification)) {
59 2
            throw new UnspecifiedValidatorException(
60
                'The validator Required is being initialized without an array'
61 2
            );
62
        }
63 85
    }
64
65 85
    public function validate($value)
66
    {
67 85
        if (is_array($value)) {
68 5
            return $this->validateArray($value);
69
        }
70 80
        if ($value instanceof \stdClass) {
71 79
            return $this->validateObject($value);
72
        }
73
74 1
        return false;
75
    }
76
77 79
    private function validateObject(\stdClass $object)
78
    {
79 79
        foreach ($this->items as $item) {
80 79
            if (!property_exists($object, $item)) {
81 2
                return false;
82
            }
83 79
        }
84
85 77
        return true;
86
    }
87
88 5
    private function validateArray(array $array)
89
    {
90 5
        foreach ($this->items as $item) {
91 5
            if (!array_key_exists($item, $array)) {
92 2
                return false;
93
            }
94 5
        }
95
96 3
        return true;
97
    }
98
}
99