Passed
Push — master ( 21d295...441237 )
by Jesse
02:06
created

KeyRequiring   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 4
dl 0
loc 14
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A mustHaveTheKeyInThe() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydration\Mapping\Property;
5
6
use Stratadox\HydrationMapping\KeyedMapping;
7
use Stratadox\HydrationMapping\MappingFailure;
8
use function array_key_exists;
9
use function assert;
10
11
/**
12
 * Behaviour to assert that the key is present in the data.
13
 *
14
 * @author Stratadox
15
 */
16
trait KeyRequiring
17
{
18
    /**
19
     * Asserts that our key is present in the input array.
20
     *
21
     * @param mixed[] $data   Input data that must contain our key.
22
     * @throws MappingFailure When the input data does not contain our key.
23
     */
24
    private function mustHaveTheKeyInThe(array $data): void
25
    {
26
        $mapping = $this;
27
        assert($mapping instanceof KeyedMapping);
28
        if (!array_key_exists($mapping->key(), $data)) {
29
            throw MissingTheKey::inTheInput($data, $mapping, $mapping->key());
30
        }
31
    }
32
}
33