1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Request; |
20
|
|
|
|
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
23
|
|
|
use Surfnet\StepupBundle\Exception\BadJsonRequestException; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ParamConverter that converts JSON objects with underscore notation mapped to snake-cased, public properties of |
29
|
|
|
* classes that implement JsonConvertible. |
30
|
|
|
* |
31
|
|
|
* @SuppressWarnings(PHPMD.MissingImport) |
32
|
|
|
* @see JsonConvertible |
33
|
|
|
*/ |
34
|
|
|
class JsonConvertibleParamConverter implements ParamConverterInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var ValidatorInterface |
38
|
|
|
*/ |
39
|
|
|
private $validator; |
40
|
|
|
|
41
|
|
|
public function __construct(ValidatorInterface $validator) |
42
|
|
|
{ |
43
|
|
|
$this->validator = $validator; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @SuppressWarnings(PHPMD.MissingImport) - Unable to import the dynamic class creation at line 63 |
48
|
|
|
*/ |
49
|
|
|
public function apply(Request $request, ParamConverter $configuration) |
50
|
|
|
{ |
51
|
|
|
$name = $configuration->getName(); |
52
|
|
|
$snakeCasedName = $this->camelCaseToSnakeCase($name); |
53
|
|
|
$class = $configuration->getClass(); |
54
|
|
|
|
55
|
|
|
$json = $request->getContent(); |
56
|
|
|
$object = json_decode($json, true); |
57
|
|
|
|
58
|
|
|
if (!isset($object[$snakeCasedName]) || !is_array($object[$snakeCasedName])) { |
59
|
|
|
throw new BadJsonRequestException([sprintf("Missing parameter '%s'", $name)]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$object = $object[$snakeCasedName]; |
63
|
|
|
$convertedObject = new $class; |
64
|
|
|
|
65
|
|
|
$errors = []; |
66
|
|
|
|
67
|
|
|
foreach ($object as $key => $value) { |
68
|
|
|
$properlyCasedKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))); |
69
|
|
|
|
70
|
|
|
if (!property_exists($convertedObject, $properlyCasedKey)) { |
71
|
|
|
$errors[] = sprintf("Unknown property '%s.%s'", $snakeCasedName, $key); |
72
|
|
|
|
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$convertedObject->$properlyCasedKey = $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$violations = $this->validator->validate($convertedObject); |
80
|
|
|
|
81
|
|
|
if (count($errors) + count($violations) > 0) { |
82
|
|
|
throw BadJsonRequestException::createForViolationsAndErrors($violations, $name, $errors); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$request->attributes->set($name, $convertedObject); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function supports(ParamConverter $configuration) |
89
|
|
|
{ |
90
|
|
|
$class = $configuration->getClass(); |
91
|
|
|
|
92
|
|
|
if (!is_string($class)) { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return is_subclass_of($class, 'Surfnet\StepupBundle\Request\JsonConvertible'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $camelCase |
101
|
|
|
* @return string |
102
|
|
|
* @see \Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter |
103
|
|
|
*/ |
104
|
|
|
private function camelCaseToSnakeCase($camelCase) |
105
|
|
|
{ |
106
|
|
|
$snakeCase = ''; |
107
|
|
|
|
108
|
|
|
$len = strlen($camelCase); |
109
|
|
|
for ($i = 0; $i < $len; $i++) { |
110
|
|
|
if (ctype_upper($camelCase[$i])) { |
111
|
|
|
$snakeCase .= '_'.strtolower($camelCase[$i]); |
112
|
|
|
} else { |
113
|
|
|
$snakeCase .= strtolower($camelCase[$i]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $snakeCase; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|