Passed
Push — master ( c05883...1d5c29 )
by Nikolay
02:30
created

FillFromArrayTrait::fill()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 9.3798

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 11
nop 3
dl 0
loc 17
ccs 6
cts 11
cp 0.5455
crap 9.3798
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TgBotApi\BotApiBase\Method\Traits;
6
7
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
8
use TgBotApi\BotApiBase\Exception\BadArgumentException;
9
10
/**
11
 * Trait FillFromArrayTrait.
12
 */
13
trait FillFromArrayTrait
14
{
15
    /**
16
     * @param array                       $data
17
     * @param array                       $forbidden
18
     * @param NameConverterInterface|null $converter
19
     *
20
     * @throws BadArgumentException
21
     */
22 2
    public function fill(array $data, array $forbidden = [], NameConverterInterface $converter = null)
23
    {
24 2
        foreach ($forbidden as $item) {
25
            if (isset($data[$item])) {
26
                throw new BadArgumentException(
27
                    \sprintf('Argument %s is forbidden in %s constructor', $item, static::class)
28
                );
29
            }
30
        }
31 2
        foreach ($data as $key => $value) {
32 2
            if ($converter) {
33
                $key = $converter->denormalize($key);
34
            }
35 2
            if (!\property_exists($this, $key)) {
36
                throw new BadArgumentException(\sprintf('Argument %s not found in %s', $key, static::class));
37
            }
38 2
            $this->{$key} = $value;
39
        }
40 2
    }
41
}
42