ValueExtractor::fromArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2
1
<?php
2
declare(strict_types = 1);
3
4
5
namespace AmmitPhp\Ammit\UI\Resolver;
6
use AmmitPhp\Ammit\UI\Resolver\Validator\InvalidArgumentException;
7
8
9
class ValueExtractor
10
{
11
    /**
12
     * @param mixed $params
13
     * @param string $key
14
     *
15
     * @return mixed
16
     * @throws InvalidArgumentException If unable to extract
17
     */
18
    public function fromArray($params, string $key)
19
    {
20
        if (false === is_array($params)) {
21
            throw new InvalidArgumentException(
22
                sprintf('Value "%s" is not an array.', $params),
23
                0,
24
                null,
25
                $params
26
            );
27
        }
28
29
        if (false === array_key_exists($key, $params)) {
30
            throw new InvalidArgumentException(
31
                sprintf('Array does not contain an element with key "%s"', $key),
32
                0,
33
                null,
34
                $params
35
            );
36
        }
37
38
        return $params[$key];
39
    }
40
}
41