Completed
Push — develop ( 27be84...959dbb )
by
unknown
09:17
created

GeoSelectHydratorStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Collections\Collection;
15
use Geo\Service\AbstractClient;
16
use Core\Entity\AbstractLocation as Location;
17
use Zend\Hydrator\Strategy\StrategyInterface;
18
19
/**
20
 * Strategy to hydrate/extract a location entity.
21
 * 
22
 * @author Mathias Gelhausen <[email protected]>
23
 * @since 0.29
24
 * @since 0.29.2 - add support for multiple locations.
25
 * @todo write test 
26
 */
27
class GeoSelectHydratorStrategy implements StrategyInterface
28
{
29
    protected $geoClient;
30
31
    protected $locationEntityPrototype;
32
33
    public function __construct(AbstractClient $client)
34
    {
35
        $this->geoClient = $client;
36
    }
37
38
    /**
39
     * @param mixed $locationEntityPrototype
40
     *
41
     * @return self
42
     */
43
    public function setLocationEntityPrototype($locationEntityPrototype)
44
    {
45
        $this->locationEntityPrototype =
46
            is_string($locationEntityPrototype)
47
            ? new $locationEntityPrototype
48
            : $locationEntityPrototype;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56
    public function getLocationEntity()
57
    {
58
        return clone $this->locationEntityPrototype;
59
    }
60
61
62
63
    /**
64
     * Converts the given value so that it can be extracted by the hydrator.
65
     *
66
     * @param mixed  $value  The original value.
67
     *
68
     * @return mixed Returns the value that should be extracted.
69
     */
70
    public function extract($value, $object = null)
71
    {
72
        if ($value instanceOf Collection || is_array($value)) {
73
            $values = [];
74
            foreach ($value as $collItem) {
75
                $values[] = $this->extract($collItem, $object);
76
            }
77
            return $values;
78
        }
79
80
        if ($value instanceOf Location) {
81
            return $value->toString();
82
        }
83
84
        if (0 === strpos($value, '{')) {
85
            return $value;
86
        }
87
        if ($value){
88
            $data = $this->geoClient->queryOne($value);
89
            return $data['id'];
90
        }else{
91
            return;
92
        }
93
    }
94
95
    /**
96
     * Converts the given value so that it can be hydrated by the hydrator.
97
     *
98
     * @param mixed $value The original value.
99
     * @param array $data  (optional) The original data for context.
100
     *
101
     * @return mixed Returns the value that should be hydrated.
102
     */
103
    public function hydrate($value, $data = [])
104
    {
105
        if (is_array($value)) {
106
            $coll = new ArrayCollection();
107
            foreach ($value as $v) {
108
                $coll->add($this->hydrate($v, $data));
109
            }
110
            return $coll;
111
        }
112
113
        if (empty($value) || 0 !== strpos($value, '{')) {
114
            return null;
115
        }
116
117
        $location = $this->getLocationEntity();
118
        return $location->fromString($value);
119
    }
120
}