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

KeyRequiring::mustHaveTheKeyInThe()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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