1 | <?php |
||
19 | class ExtReferenceConverter implements ExtReferenceConverterInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var RouterInterface |
||
23 | */ |
||
24 | private $router; |
||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $mapping; |
||
29 | |||
30 | /** |
||
31 | * Constructor |
||
32 | * |
||
33 | * @param RouterInterface $router Router |
||
34 | * @param array $mapping colleciton_name => service_id mapping |
||
35 | */ |
||
36 | 10 | public function __construct(RouterInterface $router, array $mapping) |
|
37 | { |
||
38 | 10 | $this->router = $router; |
|
39 | 10 | $this->mapping = $mapping; |
|
40 | 10 | } |
|
41 | |||
42 | /** |
||
43 | * return the extref from URL |
||
44 | * |
||
45 | * @param string $url Extref URL |
||
46 | * @return ExtReference |
||
47 | * @throws \InvalidArgumentException |
||
48 | */ |
||
49 | 6 | public function getExtReference($url) |
|
50 | { |
||
51 | 6 | $path = parse_url($url, PHP_URL_PATH); |
|
52 | 6 | if ($path === false) { |
|
53 | throw new \InvalidArgumentException(sprintf('URL %s', $url)); |
||
54 | } |
||
55 | |||
56 | 6 | $id = null; |
|
57 | 6 | $collection = null; |
|
58 | |||
59 | 6 | foreach ($this->router->getRouteCollection()->all() as $route) { |
|
60 | 6 | list($collection, $id) = $this->getDataFromRoute($route, $path); |
|
61 | 6 | if ($collection !== null && $id !== null) { |
|
62 | 6 | return ExtReference::create($collection, $id); |
|
63 | } |
||
64 | 1 | } |
|
65 | |||
66 | throw new \InvalidArgumentException(sprintf('Could not read URL %s', $url)); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * return the URL from extref |
||
71 | * |
||
72 | * @param ExtReference $extReference Extref |
||
73 | * @return string |
||
74 | * @throws \InvalidArgumentException |
||
75 | */ |
||
76 | 4 | public function getUrl(ExtReference $extReference) |
|
77 | { |
||
78 | 4 | if (!isset($this->mapping[$extReference->getRef()])) { |
|
79 | throw new \InvalidArgumentException( |
||
80 | sprintf( |
||
81 | 'Could not create URL from extref "%s"', |
||
82 | json_encode($extReference) |
||
83 | ) |
||
84 | ); |
||
85 | } |
||
86 | |||
87 | 4 | return $this->router->generate( |
|
88 | 4 | $this->mapping[$extReference->getRef()].'.get', |
|
89 | 4 | ['id' => $extReference->getId()], |
|
90 | 2 | true |
|
|
|||
91 | 2 | ); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * get collection and id from route |
||
96 | * |
||
97 | * @param Route $route route to look at |
||
98 | * @param string $value value of reference as URI |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | 6 | private function getDataFromRoute(Route $route, $value) |
|
120 | } |
||
121 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: