Passed
Push — master ( 18ba1d...5701de )
by Damian
02:17
created

ObjectRule::getSample()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * PHPacto - Contract testing solution
5
 *
6
 * Copyright (c) Damian Długosz
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace PHPacto\Matcher\Rules;
23
24
use PHPacto\Matcher\Mismatches;
25
26
class ObjectRule extends AbstractRecursiveRule
27
{
28
    /**
29
     * @param Rule[] $properties
30
     * @param mixed  $sample
31
     */
32
    public function __construct(array $properties, $sample = null)
33
    {
34
        parent::__construct($properties, $sample);
35
    }
36
37
    /**
38
     * @return Rule[]
39
     */
40
    public function getProperties(): array
41
    {
42
        return $this->rules;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->rules returns the type PHPacto\Matcher\Rules\Ru...acto\Matcher\Rules\Rule which is incompatible with the type-hinted return array.
Loading history...
43
    }
44
45
    public function assertMatch($test): void
46
    {
47
        if (!\is_array($test)) {
48
            throw new Mismatches\TypeMismatch('array', \gettype($test));
49
        }
50
51
        $mismatches = [];
52
53
        foreach ($this->rules as $key => $rule) {
54
            try {
55
                if (!\array_key_exists($key, $test)) {
56
                    throw new Mismatches\KeyNotFoundMismatch($key);
57
                }
58
59
                $this->assertMatchRec($rule, $test[$key]);
60
            } catch (Mismatches\Mismatch $e) {
61
                $mismatches[$key] = $e;
62
            }
63
        }
64
65
        if ($mismatches) {
66
            throw new Mismatches\MismatchCollection($mismatches, 'One or more of the {{ count }} properties not matching the rule');
67
        }
68
    }
69
70
    /**
71
     * @param Rule[] $properties
72
     */
73
    protected function assertSupport($properties): void
74
    {
75
        if (!\count($properties)) {
76
            throw new Mismatches\ValueMismatch('The array is empty', 'An array with values', 'An empty array');
77
        }
78
79
        foreach ($properties as $item) {
80
            if (!(\is_array($item) || $item instanceof Rule)) {
81
                throw new Mismatches\TypeMismatch('Rule', \gettype($properties), 'Each item should be an instance of {{ expected }}');
82
            }
83
        }
84
    }
85
86
    public function getSample()
87
    {
88
        if ($this->sample !== null) {
89
            return $this->sample;
90
        }
91
92
        $sample = [];
93
94
        foreach ($this->rules as $key => $rule) {
95
            $sample[$key] = $rule->getSample();
96
        }
97
98
        return $sample;
99
    }
100
}
101