1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Everlution\Navigation\Factory\Build\Hydrator\Item; |
6
|
|
|
|
7
|
|
|
use Everlution\Navigation\Factory\Build\ItemConfig; |
8
|
|
|
use Everlution\Navigation\Item; |
9
|
|
|
use Everlution\Navigation\Item\Route; |
10
|
|
|
use Everlution\Navigation\Factory\Build\Hydrator\Item as HydratorItem; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RouteConfig. |
14
|
|
|
* @author Ivan Barlog <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class RouteConfig extends HydratorItem implements ItemConfig |
17
|
|
|
{ |
18
|
|
|
const OPTION_ROUTE = 'route'; |
19
|
|
|
const OPTION_ROUTE_PARAMS = 'params'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $className |
23
|
|
|
* @param array $arguments |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
protected function getObject(string $className, array $arguments) |
27
|
|
|
{ |
28
|
|
|
return new $className( |
29
|
|
|
$arguments[self::OPTION_LABEL], |
30
|
|
|
$arguments[self::OPTION_ROUTE], |
31
|
|
|
$arguments[self::OPTION_ROUTE_PARAMS] |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Route|Item $object |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
protected function getArray($object): array |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
self::OPTION_CLASS => get_class($object), |
43
|
|
|
self::OPTION_LABEL => $object->getLabel(), |
44
|
|
|
self::OPTION_ROUTE => $object->getRoute(), |
|
|
|
|
45
|
|
|
self::OPTION_ROUTE_PARAMS => $object->getRouteParameters(), |
|
|
|
|
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function config() |
50
|
|
|
{ |
51
|
|
|
$this->resolver->setRequired([self::OPTION_ROUTE, self::OPTION_ROUTE_PARAMS]); |
52
|
|
|
$this->resolver->setAllowedTypes(self::OPTION_ROUTE, 'string'); |
53
|
|
|
$this->resolver->setAllowedTypes(self::OPTION_ROUTE_PARAMS, 'array'); |
54
|
|
|
$this->resolver->setDefault(self::OPTION_ROUTE_PARAMS, []); |
55
|
|
|
$this->supportedClasses[] = Route::class; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
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: