1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the ContentFieldValidationException ValueObjectVisitor class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor; |
12
|
|
|
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Translation; |
14
|
|
|
use eZ\Publish\Core\REST\Common\Output\Generator; |
15
|
|
|
use eZ\Publish\Core\REST\Common\Output\Visitor; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ContentFieldValidationException value object visitor. |
19
|
|
|
*/ |
20
|
|
|
class ContentFieldValidationException extends BadRequestException |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Visit struct returned by controllers. |
24
|
|
|
* |
25
|
|
|
* @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor |
26
|
|
|
* @param \eZ\Publish\Core\REST\Common\Output\Generator $generator |
27
|
|
|
* @param \eZ\Publish\Core\REST\Server\Exceptions\ContentFieldValidationException $data |
28
|
|
|
*/ |
29
|
|
|
public function visit(Visitor $visitor, Generator $generator, $data) |
30
|
|
|
{ |
31
|
|
|
$generator->startObjectElement('ErrorMessage'); |
32
|
|
|
|
33
|
|
|
$statusCode = $this->getStatus(); |
34
|
|
|
$visitor->setStatus($statusCode); |
35
|
|
|
$visitor->setHeader('Content-Type', $generator->getMediaType('ErrorMessage')); |
36
|
|
|
|
37
|
|
|
$generator->startValueElement('errorCode', $statusCode); |
38
|
|
|
$generator->endValueElement('errorCode'); |
39
|
|
|
|
40
|
|
|
$generator->startValueElement('errorMessage', $this->httpStatusCodes[$statusCode]); |
41
|
|
|
$generator->endValueElement('errorMessage'); |
42
|
|
|
|
43
|
|
|
$generator->startValueElement('errorDescription', $data->getMessage()); |
44
|
|
|
$generator->endValueElement('errorDescription'); |
45
|
|
|
|
46
|
|
|
$errorDetails = ['fields' => []]; |
47
|
|
|
foreach ($data->getFieldErrors() as $fieldTypeId => $translations) { |
48
|
|
|
foreach ($translations as $languageCode => $validationErrors) { |
|
|
|
|
49
|
|
|
if (!is_array($validationErrors)) { |
50
|
|
|
$validationErrors = [$validationErrors]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($validationErrors as $validationError) { |
54
|
|
|
$translation = $validationError->getTranslatableMessage(); |
55
|
|
|
$errorDetails['fields'][$fieldTypeId][] = [ |
56
|
|
|
'type' => $validationError->getTarget(), |
57
|
|
|
'message' => $this->translator->trans( |
58
|
|
|
$this->translationToString($translation), |
59
|
|
|
$translation->values, |
60
|
|
|
'repository_exceptions' |
61
|
|
|
), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$generator->startValueElement('errorDetails', $errorDetails); |
|
|
|
|
67
|
|
|
$generator->endValueElement('errorDetails'); |
68
|
|
|
|
69
|
|
View Code Duplication |
if ($this->debug) { |
|
|
|
|
70
|
|
|
$generator->startValueElement('trace', $data->getTraceAsString()); |
71
|
|
|
$generator->endValueElement('trace'); |
72
|
|
|
|
73
|
|
|
$generator->startValueElement('file', $data->getFile()); |
74
|
|
|
$generator->endValueElement('file'); |
75
|
|
|
|
76
|
|
|
$generator->startValueElement('line', $data->getLine()); |
77
|
|
|
$generator->endValueElement('line'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$generator->endObjectElement('ErrorMessage'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Convert a Translation object to a string, detecting singular/plural as needed. |
85
|
|
|
* |
86
|
|
|
* @param Translation $translation The Translation object |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
private function translationToString(Translation $translation) |
90
|
|
|
{ |
91
|
|
|
if ($translation instanceof Translation\Plural) { |
92
|
|
|
if (current($translation->values) === 1) { |
|
|
|
|
93
|
|
|
return $translation->singular; |
|
|
|
|
94
|
|
|
} else { |
95
|
|
|
return $translation->plural; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
return $translation->message; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|