Completed
Pull Request — master (#1)
by Karsten
03:23
created

LineStringMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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\LineString;
9
use PeekAndPoke\Component\Slumber\Annotation\Slumber\GeoJson\AsLineString;
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 LineStringMapper extends AbstractPropertyMapper
18
{
19
    /** @var AsLineString */
20
    private $options;
21
22
    public function __construct(AsLineString $options)
23
    {
24
        $this->options = $options;
25
    }
26
27
    /**
28
     * @return AsLineString
29
     */
30
    public function getOptions()
31
    {
32
        return $this->options;
33
    }
34
35
    /**
36
     * @param Slumberer  $slumberer
37
     * @param LineString $value
38
     *
39
     * @return mixed
40
     */
41
    public function slumber(Slumberer $slumberer, $value)
42
    {
43
        if (! $value instanceof LineString) {
44
            return null;
45
        }
46
47
        return [
48
            'type'        => 'LineString',
49
            'coordinates' => $value->getCoordinates(),
50
        ];
51
    }
52
53
    /**
54
     * @param Awaker $awaker
55
     * @param mixed  $value
56
     *
57
     * @return LineString
58
     */
59
    public function awake(Awaker $awaker, $value)
60
    {
61
        if (($value instanceof \ArrayAccess || is_array($value))
62
            && $value['type'] === 'Point'
63
            && isset($value['type'], $value['coordinates'])
64
        ) {
65
            $coords = $value['coordinates'];
66
67
            return new LineString($coords);
68
        }
69
70
        return null;
71
    }
72
}
73