Value::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 1
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Dictionary\Factory;
6
7
use InvalidArgumentException;
8
use Knp\DictionaryBundle\Dictionary;
9
use Knp\DictionaryBundle\Dictionary\Factory;
10
use Knp\DictionaryBundle\Dictionary\Simple;
11
use Knp\DictionaryBundle\ValueTransformer;
12
13
final class Value implements Factory
14
{
15
    public function __construct(private readonly ValueTransformer $valueTransformer) {}
16
17
    /**
18
     * {@inheritdoc}
19
     *
20 5
     * @throw InvalidArgumentException Not able to create a dictionary with the given name
21
     */
22 5
    public function create(string $name, array $config): Dictionary
23 5
    {
24
        if (!isset($config['content'])) {
25
            throw new \InvalidArgumentException(\sprintf(
26
                'The key content for dictionary %s must be set.',
27
                $name
28
            ));
29
        }
30 2
31
        $content = $config['content'];
32 2
        $values  = [];
33 1
34 1
        foreach ($content as $value) {
35
            $values[] = $this->valueTransformer->transform($value);
36
        }
37
38
        return new Simple($name, $values);
39 1
    }
40 1
41
    public function supports(array $config): bool
42 1
    {
43 1
        return isset($config['type']) && 'value' === $config['type'];
44
    }
45
}
46