Completed
Push — develop ( 515643...e2e58e )
by
unknown
08:08
created

GeoSelectHydratorStrategy   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setLocationEntityPrototype() 0 9 2
A getLocationEntity() 0 4 1
A extract() 0 13 3
A hydrate() 0 9 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Geo\Form;
12
13
use Geo\Service\AbstractClient;
14
use Jobs\Entity\Location;
15
use Zend\Hydrator\Strategy\StrategyInterface;
16
17
/**
18
 * ${CARET}
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @todo write test 
22
 */
23
class GeoSelectHydratorStrategy implements StrategyInterface
24
{
25
    protected $geoClient;
26
27
    protected $locationEntityPrototype;
28
29
    public function __construct(AbstractClient $client)
30
    {
31
        $this->geoClient = $client;
32
    }
33
34
    /**
35
     * @param mixed $locationEntityPrototype
36
     *
37
     * @return self
38
     */
39
    public function setLocationEntityPrototype($locationEntityPrototype)
40
    {
41
        $this->locationEntityPrototype =
42
            is_string($locationEntityPrototype)
43
            ? new $locationEntityPrototype
44
            : $locationEntityPrototype;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function getLocationEntity()
53
    {
54
        return clone $this->locationEntityPrototype;
55
    }
56
57
58
59
    /**
60
     * Converts the given value so that it can be extracted by the hydrator.
61
     *
62
     * @param mixed  $value  The original value.
63
     *
64
     * @return mixed Returns the value that should be extracted.
65
     */
66
    public function extract($value, $object = null)
67
    {
68
        if ($value instanceOf Location) {
69
            return $value->toString();
70
        }
71
72
        if (0 === strpos($value, '{')) {
73
            return $value;
74
        }
75
76
        $data = $this->geoClient->queryOne($value);
77
        return $data['id'];
78
    }
79
80
    /**
81
     * Converts the given value so that it can be hydrated by the hydrator.
82
     *
83
     * @param mixed $value The original value.
84
     * @param array $data  (optional) The original data for context.
85
     *
86
     * @return mixed Returns the value that should be hydrated.
87
     */
88
    public function hydrate($value, $data = [])
89
    {
90
        if (empty($value) || 0 !== strpos($value, '{')) {
91
            return null;
92
        }
93
94
        $location = $this->getLocationEntity();
95
        return $location->fromString($value);
96
    }
97
}