1 | <?php |
||
12 | class RestRequestParser |
||
13 | { |
||
14 | /** |
||
15 | * @var MetadataFactory |
||
16 | */ |
||
17 | private $ddrRestMetadataFactory; |
||
18 | |||
19 | /** |
||
20 | * @var PropertyAccessor |
||
21 | */ |
||
22 | private $propertyAccessor; |
||
23 | |||
24 | 6 | public function __construct(MetadataFactory $ddrRestMetadataFactory, PropertyAccessor $propertyAccessor) |
|
25 | { |
||
26 | 6 | $this->ddrRestMetadataFactory = $ddrRestMetadataFactory; |
|
27 | 6 | $this->propertyAccessor = $propertyAccessor; |
|
28 | 6 | } |
|
29 | |||
30 | /** |
||
31 | * @param Request $request |
||
32 | * @param string $entityClass |
||
33 | * @param object|null $entity |
||
34 | * |
||
35 | * @return object |
||
36 | */ |
||
37 | 6 | public function parseEntity( |
|
38 | Request $request, |
||
39 | $entityClass, |
||
40 | $entity = null |
||
41 | ) { |
||
42 | 6 | $method = $request->getMethod(); |
|
43 | 6 | $format = $request->getRequestFormat(); |
|
44 | |||
45 | 6 | if ('json' !== $format) { |
|
46 | throw new \RuntimeException(sprintf('Unsupported format "%s"', $format)); |
||
47 | } |
||
48 | |||
49 | 6 | $data = $this->getRequestContent($request); |
|
50 | |||
51 | 6 | if (null === $entity) { |
|
52 | 4 | $entity = new $entityClass; |
|
53 | } |
||
54 | |||
55 | 6 | $this->updateObject($entity, $method, $data); |
|
56 | |||
57 | 6 | return $entity; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param Request $request |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | 6 | protected function getRequestContent(Request $request) |
|
66 | { |
||
67 | 6 | $content = $request->getContent(); |
|
68 | 6 | if ('' !== $content) { |
|
69 | 6 | return json_decode($content, true); |
|
70 | } |
||
71 | |||
72 | return $request->request->all(); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param object $object Access by reference. |
||
77 | * @param string $method |
||
78 | * @param array $data |
||
79 | */ |
||
80 | 6 | protected function updateObject( |
|
81 | &$object, |
||
82 | $method, |
||
83 | $data |
||
84 | ) { |
||
85 | 6 | $classMetadata = $this->ddrRestMetadataFactory->getMetadataForClass(ClassUtils::getClass($object)); |
|
86 | |||
87 | 6 | foreach ($data as $key => $value) { |
|
88 | 4 | if (!array_key_exists($key, $classMetadata->propertyMetadata)) { |
|
89 | continue; |
||
90 | //throw new \RuntimeException(sprintf('No field %s for Class %s', $key, get_class($object))); |
||
91 | } |
||
92 | /** @var PropertyMetadata $propertyMetadata */ |
||
93 | 4 | $propertyMetadata = $classMetadata->propertyMetadata[$key]; |
|
94 | 4 | if ($this->isUpdateable($method, $propertyMetadata)) { |
|
95 | 4 | $this->updateProperty($object, $method, $key, $value, $propertyMetadata); |
|
96 | } |
||
97 | } |
||
98 | 6 | } |
|
99 | |||
100 | /** |
||
101 | * @param object $object Access by reference. |
||
102 | * @param string $method |
||
103 | * @param string $propertyName |
||
104 | * @param mixed $value |
||
105 | * @param PropertyMetadata $propertyMetadata |
||
106 | */ |
||
107 | 4 | protected function updateProperty( |
|
138 | |||
139 | 2 | protected function updatePropertyObject( |
|
140 | &$object, |
||
141 | string $method, |
||
154 | |||
155 | /** |
||
156 | * @param string $method |
||
157 | * @param PropertyMetadata $propertyMetadata |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 4 | protected function isUpdateable( |
|
162 | $method, |
||
163 | PropertyMetadata $propertyMetadata |
||
164 | ) { |
||
165 | 4 | if (Request::METHOD_PUT === $method || Request::METHOD_PATCH === $method) { |
|
166 | 2 | return $propertyMetadata->isPuttable(); |
|
167 | } |
||
168 | 2 | if (Request:: METHOD_POST === $method) { |
|
169 | 2 | return $propertyMetadata->isPostable(); |
|
170 | } |
||
171 | |||
172 | return false; |
||
173 | } |
||
174 | |||
175 | 4 | private function convert(?string $type, $value) |
|
190 | } |
||
191 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.