Completed
Branch develop (b41e4a)
by Mariano
04:26
created

Required   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 6
Bugs 2 Features 4
Metric Value
wmc 16
c 6
b 2
f 4
lcom 1
cbo 1
dl 0
loc 70
ccs 30
cts 35
cp 0.8571
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSpecification() 0 9 2
A specificationItemisValidOrThrowException() 0 8 3
A specificationIsArrayOrThrowException() 0 8 2
A validate() 0 11 3
A validateObject() 0 10 3
A validateArray() 0 10 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 74
    public function setSpecification($specification = null)
38
    {
39 74
        $this->specificationIsArrayOrThrowException($specification);
40 74
        foreach ($specification as $item) {
41 74
            $this->specificationItemisValidOrThrowException($item);
42 74
        }
43
44 74
        $this->items = $specification;
0 ignored issues
show
Documentation Bug introduced by
It seems like $specification of type * is incompatible with the declared type array<integer,?> of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45 74
    }
46
47 74
    private function specificationItemisValidOrThrowException($item)
48
    {
49 74
        if (!is_string($item) || is_numeric($item)) {
50
            throw new UnspecifiedValidatorException(
51
                "The validator Required is being initialized without a valid array"
52
            );
53
        }
54 74
    }
55
56 74
    private function specificationIsArrayOrThrowException($specification)
57
    {
58 74
        if (!is_array($specification)) {
59
            throw new UnspecifiedValidatorException(
60
                "The validator Required is being initialized without an array"
61
            );
62
        }
63 74
    }
64
65 75
    public function validate($value)
66
    {
67 75
        if (is_array($value)) {
68 3
            return $this->validateArray($value);
69
        }
70 72
        if ($value instanceof \stdClass) {
71 72
            return $this->validateObject($value);
72
        }
73
74
        return false;
75
    }
76
77 72
    private function validateObject(\stdClass $object)
78
    {
79 72
        foreach ($this->items as $item) {
80 72
            if (!property_exists($object, $item)) {
81 1
                return false;
82
            }
83 72
        }
84
85 71
        return true;
86
    }
87
88 3
    private function validateArray(array $array)
89
    {
90 3
        foreach ($this->items as $item) {
91 3
            if (!array_key_exists($item, $array)) {
92 1
                return false;
93
            }
94 3
        }
95
96 2
        return true;
97
    }
98
}
99