1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Jobs\Factory\Form; |
12
|
|
|
|
13
|
|
|
use Interop\Container\ContainerInterface; |
14
|
|
|
use Jobs\Form\OrganizationSelect; |
15
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
16
|
|
|
use Zend\ServiceManager\FactoryInterface; |
17
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Factory for the ActiveOrganization select box |
21
|
|
|
* |
22
|
|
|
* This creates an {@link \Jobs\Form\OrganizationSelect} with all organizations that are |
23
|
|
|
* currently associated to at least one "active" job entity. |
24
|
|
|
* |
25
|
|
|
* @author Mathias Gelhausen <[email protected]> |
26
|
|
|
* @since 0.23 |
27
|
|
|
* @since 0.30 - refactored to implement lazy loading organization list. |
28
|
|
|
*/ |
29
|
|
|
class ActiveOrganizationSelectFactory implements FactoryInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Create an object |
33
|
|
|
* |
34
|
|
|
* @param ContainerInterface $container |
35
|
|
|
* @param string $requestedName |
36
|
|
|
* @param null|array $options |
37
|
|
|
* |
38
|
|
|
* @return object |
39
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
40
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
41
|
|
|
* creating a service. |
42
|
|
|
* @throws ContainerException if any other error occurs |
43
|
|
|
*/ |
44
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
45
|
|
|
{ |
46
|
|
|
/* @var \Zend\Http\PhpEnvironment\Request $request */ |
47
|
|
|
$request = $container->get('Request'); |
48
|
|
|
$query = $request->getQuery(); |
49
|
|
|
$initialId = $query->get('companyId'); |
50
|
|
|
|
51
|
|
|
$organizations = []; |
52
|
|
|
|
53
|
|
|
if ($initialId) { |
54
|
|
|
/* @var $serviceLocator \Zend\ServiceManager\AbstractPluginManager |
55
|
|
|
* @var $repository \Organizations\Repository\Organization */ |
56
|
|
|
$repositories = $container->get('repositories'); |
57
|
|
|
$repository = $repositories->get('Organizations'); |
58
|
|
|
$organization = $repository->find($initialId); |
59
|
|
|
$organizations[] = $organization; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$select = new OrganizationSelect(); |
63
|
|
|
|
64
|
|
|
$select->setSelectableOrganizations($organizations); |
65
|
|
|
$select->setAttribute('data-ajax', '?ajax=jobs.admin.activeorganizations'); |
66
|
|
|
|
67
|
|
|
return $select; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Creates the organization select box. |
73
|
|
|
* |
74
|
|
|
* @param ServiceLocatorInterface|AbstractPluginManager $serviceLocator |
75
|
|
|
*/ |
76
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
77
|
|
|
{ |
78
|
|
|
return $this($serviceLocator->getServiceLocator(), OrganizationSelect::class); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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: