Completed
Push — master ( 88792f...ba8ac8 )
by Karsten
08:30 queued 06:31
created

PointMapper::slumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 3
cts 6
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2.5
1
<?php
2
/**
3
 * File was created 03.03.2016 13:36
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property\GeoJson;
7
8
use PeekAndPoke\Component\GeoJson\Point;
9
use PeekAndPoke\Component\Slumber\Annotation\Slumber\GeoJson\AsPoint;
10
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
11
use PeekAndPoke\Component\Slumber\Core\Codec\Property\AbstractPropertyMapper;
12
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
13
14
/**
15
 * @author Karsten J. Gerber <[email protected]>
16
 */
17
class PointMapper extends AbstractPropertyMapper
18
{
19
    /** @var AsPoint */
20
    private $options;
21
22
    /**
23
     * @param AsPoint $options
24
     */
25 4
    public function __construct(AsPoint $options)
26
    {
27 4
        $this->options = $options;
28 4
    }
29
30
    /**
31
     * @return AsPoint
32
     */
33
    public function getOptions()
34
    {
35
        return $this->options;
36
    }
37
38
    /**
39
     * @param Slumberer $slumberer
40
     * @param Point     $value
41
     *
42
     * @return mixed
43
     */
44 2
    public function slumber(Slumberer $slumberer, $value)
45
    {
46 2
        if (! $value instanceof Point) {
47 2
            return null;
48
        }
49
50
        return [
51
            'type'        => 'Point',
52
            'coordinates' => [
53
                $value->getLng(),
54
                $value->getLat(),
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * @param Awaker $awaker
61
     * @param mixed  $value
62
     *
63
     * @return Point
64
     */
65
    public function awake(Awaker $awaker, $value)
66
    {
67
        if (($value instanceof \ArrayAccess || \is_array($value))
68
            && $value['type'] === 'Point'
69
            && isset($value['type'], $value['coordinates'])
70
            && \count($value['coordinates']) === 2
71
        ) {
72
            $coords = $value['coordinates'];
73
74
            return new Point($coords[1], $coords[0]);
75
        }
76
77
        return null;
78
    }
79
}
80