Passed
Push — master ( 414f0a...c0888e )
by Pierre
03:03
created

ValueAsKey::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
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 ValueAsKey implements Factory
14
{
15
    /**
16
     * @var ValueTransformer
17
     */
18
    private $transformer;
19
20 5
    public function __construct(ValueTransformer $transformer)
21
    {
22 5
        $this->transformer = $transformer;
23 5
    }
24
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @throw InvalidArgumentException if there is some problem with the config.
29
     */
30 2
    public function create(string $name, array $config): Dictionary
31
    {
32 2
        if (!isset($config['content'])) {
33 1
            throw new InvalidArgumentException(sprintf('The key content for dictionary %s must be set.', $name));
34
        }
35
36 1
        $content = $config['content'];
37 1
        $values  = [];
38
39 1
        foreach ($content as $value) {
40 1
            $builtValue          = $this->transformer->transform($value);
41 1
            $values[$builtValue] = $builtValue;
42
        }
43
44 1
        return new Simple($name, $values);
45
    }
46
47 1
    public function supports(array $config): bool
48
    {
49 1
        return (isset($config['type'])) ? 'value_as_key' === $config['type'] : false;
50
    }
51
}
52