|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author [email protected] |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Jobs\Form; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Entity\EntityInterface; |
|
14
|
|
|
use Core\Entity\EntityTrait; |
|
15
|
|
|
use Core\Entity\Hydrator\MappingEntityHydrator; |
|
16
|
|
|
use Core\Form\HydratorStrategyAwareTrait; |
|
17
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
18
|
|
|
use Jobs\Entity\Location; |
|
19
|
|
|
use Jobs\Form\Hydrator\Strategy\LocationStrategy; |
|
20
|
|
|
use Zend\Form\Exception; |
|
21
|
|
|
use Zend\Form\Fieldset; |
|
22
|
|
|
use Core\Entity\Hydrator\EntityHydrator; |
|
23
|
|
|
use Zend\Form\FieldsetInterface; |
|
24
|
|
|
use Core\Form\CustomizableFieldsetInterface; |
|
25
|
|
|
use Core\Form\CustomizableFieldsetTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Defines the formular fields of the base formular of a job opening. |
|
29
|
|
|
*/ |
|
30
|
|
|
class BaseFieldset extends Fieldset implements CustomizableFieldsetInterface |
|
31
|
|
|
{ |
|
32
|
|
|
use CustomizableFieldsetTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* name of the used geo location Engine |
|
36
|
|
|
* |
|
37
|
|
|
* @var string $locationEngineType |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $locationEngineType; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $options |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(array $options = []) |
|
45
|
|
|
{ |
|
46
|
|
|
parent::__construct(); |
|
47
|
|
|
if (array_key_exists('location_engine_type', $options)) { |
|
48
|
|
|
$this->locationEngineType = $options['location_engine_type']; |
|
49
|
|
|
} |
|
50
|
|
|
$this->setOptions($options); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $locationEngineType |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setLocationEngineType($locationEngineType) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->locationEngineType = $locationEngineType; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return \Zend\Hydrator\HydratorInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getHydrator() |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->hydrator) { |
|
67
|
|
|
$hydrator = new MappingEntityHydrator([ |
|
68
|
|
|
'locations' => 'geoLocation' |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
$geoLocationStrategy = $this->get('geoLocation')->getHydratorStrategy(); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$locationsStrategy = new \Zend\Hydrator\Strategy\ClosureStrategy( |
|
74
|
|
|
/* extract */ |
|
75
|
|
|
function ($value) use ($geoLocationStrategy) |
|
76
|
|
|
{ |
|
77
|
|
|
return $geoLocationStrategy->extract($value->first()); |
|
78
|
|
|
}, |
|
79
|
|
|
|
|
80
|
|
|
/* hydrate */ |
|
81
|
|
|
function ($value) use ($geoLocationStrategy) |
|
82
|
|
|
{ |
|
83
|
|
|
return new ArrayCollection([$geoLocationStrategy->hydrate($value)]); |
|
84
|
|
|
} |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
$hydrator->addStrategy('locations', $locationsStrategy); |
|
88
|
|
|
|
|
89
|
|
|
/* |
|
|
|
|
|
|
90
|
|
|
$datetimeStrategy = new Hydrator\DatetimeStrategy(); |
|
91
|
|
|
$datetimeStrategy->setHydrateFormat(Hydrator\DatetimeStrategy::FORMAT_MYSQLDATE); |
|
92
|
|
|
$hydrator->addStrategy('datePublishStart', $datetimeStrategy); |
|
93
|
|
|
*/ |
|
94
|
|
|
|
|
95
|
|
|
$this->setHydrator($hydrator); |
|
96
|
|
|
} |
|
97
|
|
|
return $this->hydrator; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function init() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->setAttribute('id', 'job-fieldset'); |
|
103
|
|
|
|
|
104
|
|
|
$this->setName('jobBase'); |
|
105
|
|
|
|
|
106
|
|
|
$this->add( |
|
107
|
|
|
[ |
|
108
|
|
|
'type' => 'Text', |
|
109
|
|
|
'name' => 'title', |
|
110
|
|
|
'options' => [ |
|
111
|
|
|
'label' => /*@translate*/ 'Job title', |
|
112
|
|
|
'description' => /*@translate*/ 'Please enter the job title' |
|
113
|
|
|
], |
|
114
|
|
|
] |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
$this->add( |
|
119
|
|
|
[ |
|
120
|
|
|
'type' => 'LocationSelect', |
|
121
|
|
|
'name' => 'geoLocation', |
|
122
|
|
|
'options' => [ |
|
123
|
|
|
'label' => /*@translate*/ 'Location', |
|
124
|
|
|
'description' => /*@translate*/ 'Please enter the location of the job', |
|
125
|
|
|
'location_entity' => Location::class, |
|
126
|
|
|
'summary_value' => function() { |
|
127
|
|
|
$loc = $this->object->getLocations()->first(); |
|
128
|
|
|
if (!$loc) { return ''; } |
|
129
|
|
|
|
|
130
|
|
|
$value = $loc->getPostalCode() . ' ' . $loc->getCity() . ', ' . $loc->getRegion(); |
|
131
|
|
|
$value = trim($value, ' ,'); |
|
132
|
|
|
|
|
133
|
|
|
return $value; |
|
134
|
|
|
}, |
|
135
|
|
|
], |
|
136
|
|
|
'attributes' => [ |
|
137
|
|
|
'data-width' => '100%', |
|
138
|
|
|
] |
|
139
|
|
|
] |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: