1 | <?php |
||
16 | class EntityParamConverter implements ParamConverterInterface |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var EntityTypeManagerInterface |
||
21 | */ |
||
22 | private $entityTypeManager; |
||
23 | |||
24 | /** |
||
25 | * @param EntityTypeManagerInterface $entityTypeManager |
||
26 | */ |
||
27 | 17 | public function __construct(EntityTypeManagerInterface $entityTypeManager) |
|
31 | |||
32 | /** |
||
33 | * @param Request $request |
||
34 | * @param ParamConverter $configuration |
||
35 | * |
||
36 | * @return bool |
||
37 | */ |
||
38 | 8 | public function apply(Request $request, ParamConverter $configuration) |
|
39 | { |
||
40 | 8 | $param = $configuration->getName(); |
|
41 | 8 | if (!$request->attributes->has($param)) { |
|
42 | 2 | return false; |
|
43 | } |
||
44 | |||
45 | 6 | $value = $request->attributes->get($param); |
|
46 | 6 | if (!$value && $configuration->isOptional()) { |
|
47 | 1 | return false; |
|
48 | } |
||
49 | |||
50 | 5 | $request->attributes->set( |
|
51 | $param, |
||
52 | 5 | $this->getNode($value, $configuration) |
|
53 | ); |
||
54 | |||
55 | 3 | return true; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param string $value |
||
60 | * @param ParamConverter $configuration |
||
61 | * |
||
62 | * @return EntityInterface|null |
||
63 | */ |
||
64 | 5 | protected function getNode($value, ParamConverter $configuration) |
|
71 | |||
72 | /** |
||
73 | * @param ParamConverter $configuration |
||
74 | * @param EntityInterface $node |
||
75 | */ |
||
76 | 5 | protected function assertValidNode( |
|
77 | ParamConverter $configuration, |
||
78 | EntityInterface $node = null |
||
79 | ) { |
||
80 | 5 | if (is_null($node) && $configuration->isOptional()) { |
|
81 | 1 | return; |
|
82 | } |
||
83 | 4 | if (is_null($node)) { |
|
84 | 1 | throw new NotFoundHttpException('entity not found.'); |
|
85 | } |
||
86 | 3 | $options = $configuration->getOptions(); |
|
87 | 3 | if (isset($options['bundle']) && $node->bundle( |
|
88 | 3 | ) !== $options['bundle']) { |
|
89 | 1 | throw new NotFoundHttpException( |
|
90 | 1 | sprintf('%s not found.', $options['bundle']) |
|
91 | ); |
||
92 | } |
||
93 | 2 | } |
|
94 | |||
95 | /** |
||
96 | * @param ParamConverter $configuration |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | 9 | public function supports(ParamConverter $configuration) |
|
114 | } |
||
115 |