|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Response; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\ParameterRefBuilder; |
|
12
|
|
|
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException; |
|
13
|
|
|
use Nocarrier\Hal; |
|
14
|
|
|
use Ramsey\VndError\VndError; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author John Kleijn <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class VndValidationErrorFactory |
|
21
|
|
|
{ |
|
22
|
|
|
const DEFAULT_MESSAGE = 'Input Validation Failure'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ParameterRefBuilder |
|
26
|
|
|
*/ |
|
27
|
|
|
private $refBuilder; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param ParameterRefBuilder $refBuilder |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(ParameterRefBuilder $refBuilder) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->refBuilder = $refBuilder; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* @param InvalidParametersException $exception |
|
40
|
|
|
* @param string|null $logRef |
|
41
|
|
|
* |
|
42
|
|
|
* @return VndError |
|
43
|
|
|
*/ |
|
44
|
|
|
public function create(Request $request, InvalidParametersException $exception, $logRef = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$error = new VndError(self::DEFAULT_MESSAGE, $logRef); |
|
47
|
|
|
$error->addLink('about', $this->refBuilder->buildDocumentLink($request), ['title' => 'Api Specification']); |
|
48
|
|
|
$error->setUri($request->getUri()); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($exception->getValidationErrors() as $errorSpec) { |
|
51
|
|
|
// For older versions, try to extract the property name from the message |
|
52
|
|
|
if (!$errorSpec['property']) { |
|
53
|
|
|
$errorSpec['property'] = preg_replace('/the property (.*) is required/', '\\1', $errorSpec['message']); |
|
54
|
|
|
} |
|
55
|
|
|
$normalizedPropertyName = preg_replace('/\[\d+\]/', '', $errorSpec['property']); |
|
56
|
|
|
$data = [ |
|
57
|
|
|
'message' => $errorSpec['message'], |
|
58
|
|
|
'path' => $this->refBuilder->createParameterSchemaPointer($request, $normalizedPropertyName) |
|
59
|
|
|
]; |
|
60
|
|
|
$parameterDefinitionUri = $this->refBuilder->buildSpecificationLink($request, $normalizedPropertyName); |
|
61
|
|
|
|
|
62
|
|
|
$validationError = new Hal($parameterDefinitionUri, $data); |
|
63
|
|
|
$error->addResource( |
|
64
|
|
|
'errors', |
|
65
|
|
|
$validationError |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $error; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|