Passed
Push — draft ( 8ec7b9...aebf0a )
by Nikolay
03:09
created

FillFromArrayTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 26
ccs 6
cts 11
cp 0.5455
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A fill() 0 17 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Greenplugin\TelegramBot\Method\Traits;
6
7
use Greenplugin\TelegramBot\Exception\BadArgumentException;
8
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
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