Completed
Branch v1.x-dev (59bcf7)
by Benjamin
05:08
created

KeyToCollectionMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 11
c 2
b 0
f 0
dl 0
loc 33
ccs 0
cts 13
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 11 3
A reverseTransform() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
namespace Obblm\Core\DataTransformer;
4
5
use Obblm\Core\Contracts\PositionInterface;
6
use Symfony\Component\Form\DataTransformerInterface;
7
8
class KeyToCollectionMapper implements DataTransformerInterface
9
{
10
    private $collection;
11
12
    public function __construct($collection)
13
    {
14
        $this->collection = $collection;
15
    }
16
17
    /**
18
     * @param string|null $value
19
     * @return PositionInterface|void
20
     */
21
    public function transform($value)
22
    {
23
        if ($value === null) {
24
            return;
25
        }
26
27
        if (!isset($this->collection[$value])) {
28
            return;
29
        }
30
31
        return $this->collection[$value];
32
    }
33
34
    public function reverseTransform($viewData)
35
    {
36
        if ($viewData === null) {
37
            return;
38
        }
39
40
        return $viewData->getKey();
41
    }
42
}
43