EnumMapper::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * File was created 30.09.2015 07:58
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property;
7
8
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsEnum;
9
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
10
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
11
use PeekAndPoke\Types\Enumerated;
12
13
/**
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
class EnumMapper extends AbstractPropertyMapper
17
{
18
    /** @var AsEnum */
19
    private $options;
20
21
    /**
22
     * C'tor.
23
     *
24
     * @param AsEnum $options
25
     */
26 23
    public function __construct(AsEnum $options)
27
    {
28 23
        $this->options = $options;
29 23
    }
30
31
    /**
32
     * @return AsEnum
33
     */
34 1
    public function getOptions()
35
    {
36 1
        return $this->options;
37
    }
38
39
    /**
40
     * @param Slumberer  $slumberer
41
     * @param Enumerated $value
42
     *
43
     * @return string|null
44
     */
45 12
    public function slumber(Slumberer $slumberer, $value)
46
    {
47 12
        if (!$value instanceof Enumerated) {
0 ignored issues
show
introduced by
$value is always a sub-type of PeekAndPoke\Types\Enumerated.
Loading history...
48 8
            return null;
49
        }
50
51 4
        return (string)$value->getValue();
52
    }
53
54
    /**
55
     * @param Awaker $awaker
56
     * @param mixed  $value
57
     *
58
     * @return Enumerated|null
59
     */
60 10
    public function awake(Awaker $awaker, $value)
61
    {
62
        /** @var Enumerated $enumClass */
63 10
        $enumClass = $this->options->getValue();
64 10
        return $enumClass::from($value);
65
    }
66
}
67