Completed
Push — develop ( f65419...2fe783 )
by
unknown
06:34
created

GeoSelectSimple::getDefaultHydratorStrategy()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 35
Code Lines 17

Duplication

Lines 8
Ratio 22.86 %

Importance

Changes 0
Metric Value
dl 8
loc 35
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @author    Carsten Bleek <[email protected]>
7
 * @copyright 2013-2017 Cross Solution (http://cross-solution.de)
8
 * @version   GIT: $Id$
9
 * @license   https://yawik.org/LICENSE.txt MIT
10
 */
11
12
namespace Geo\Form;
13
14
15
use Core\Form\Hydrator\HydratorStrategyProviderInterface;
16
use Core\Form\Hydrator\HydratorStrategyProviderTrait;
17
use Jobs\Entity\Location;
18
use Zend\Form\Element\Select;
19
use Zend\Hydrator\Strategy\ClosureStrategy;
20
21
/**
22
 * Class GeoSelectSimple
23
 *
24
 * This fieldset can be used, if you want to use a select field with a certain amount of locations.
25
 *
26
 * Example: You can configure the location field to hold the 3 locations "Stuttgart", "München" and "Frankfurt" by
27
 * configuring the location field the following way:
28
 *
29
 *   'l' => [
30
 *       'options' => [
31
 *           'value_options' => [
32
 *               '{"city":"Stuttgart","region":"Baden-Württemberg","coordinates":{"type":"Point","coordinates":[9.17702,48.78232]}}' => 'Stuttgart',
33
 *               '{"city":"München","region":"Bayern","coordinates":{"type":"Point","coordinates":[11.57549,48.13743]}}' => 'München',
34
 *               '{"city":"Frankfurt","region":"Hessen","coordinates":{"type":"Point","coordinates":[8.68212,50.11092]}}' => 'Frankfurt',
35
 *           ]
36
 *       ],
37
 *       'attributes' => [
38
 *            'class' => '',
39
 *            'data-searchbox' => '-1',
40
 *            'data-placeholder' => 'please select',
41
 *            'data-allowclear' => 'true',
42
 *        ],
43
 *       'type' => 'SimpleLocationSelect',
44
 *       'enabled' => true
45
 *   ],
46
 *
47
 * @package Geo\Form
48
 */
49
class GeoSelectSimple extends Select implements HydratorStrategyProviderInterface
50
{
51
    use HydratorStrategyProviderTrait;
52
53
    private function getDefaultHydratorStrategy()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
54
    {
55
        return new ClosureStrategy(
56
            /* extract */
57
            function($value) {
58
                if ($value instanceOf Location) {
59
                    return $value->toString();
60
                }
61
62
                if (0 === strpos($value, '{')) {
63
                    return $value;
64
                }
65
                if ($value){
66
                    foreach ($this->getValueOptions() as $optValue => $opt) {
67
                        if (false !== strpos($value, $opt)) {
68
                            return $optValue;
69
                        }
70
                    }
71
                }
72
73
                return null;
74
75
            },
76
77
            /* hydrate */
78 View Code Duplication
            function ($value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
                if (empty($value) || 0 !== strpos($value, '{')) {
80
                    return null;
81
                }
82
83
                $location = new Location();
84
                return $location->fromString($value);
85
            }
86
        );
87
    }
88
}