Skip to content

Commit e1a6c37

Browse files
committed
Merge branch 'feature/validation-exception'
2 parents b090bca + ab94d9d commit e1a6c37

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"prefer-stable": true,
1414
"require": {
1515
"php": "^7.2",
16-
"symfony/http-kernel": "^4.4|^5.0"
16+
"symfony/http-kernel": "^4.4|^5.0",
17+
"ext-json": "*"
18+
},
19+
"suggest": {
20+
"symfony/validator": "for ValidationException"
1721
},
1822
"autoload": {
1923
"psr-4": {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
4+
namespace HalloVerden\HttpExceptions\Utility;
5+
6+
7+
use HalloVerden\HttpExceptions\BadRequestException;
8+
use Symfony\Component\Validator\ConstraintViolation;
9+
use Symfony\Component\Validator\ConstraintViolationInterface;
10+
use Symfony\Component\Validator\ConstraintViolationListInterface;
11+
12+
/**
13+
* Class ValidationException
14+
*
15+
* @package HalloVerden\RequestEntityBundle\Exception
16+
*/
17+
class ValidationException extends BadRequestException {
18+
19+
/**
20+
* ValidationException constructor.
21+
*
22+
* @param ConstraintViolationListInterface $violationList
23+
* @param string $message
24+
* @param \Exception|null $previous
25+
* @param int $code
26+
*/
27+
public function __construct(ConstraintViolationListInterface $violationList, $message = "VALIDATION_ERROR", \Exception $previous = null, $code = 0) {
28+
$v = [];
29+
30+
foreach ($violationList as $violation) {
31+
/* @var $violation ConstraintViolationInterface*/
32+
$d = new ValidationExceptionViolation($violation->getInvalidValue(), $violation->getMessage(), $violation->getPropertyPath());
33+
34+
if ($violation instanceof ConstraintViolation) {
35+
$d->setErrorName($violation->getConstraint()::getErrorName($violation->getCode()));
36+
}
37+
38+
$v[] = $d->toArray();
39+
}
40+
41+
parent::__construct($message, [
42+
"violations" => $v
43+
], $previous, $code);
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function __toString() {
50+
return __CLASS__ . ": [{$this->code}]: {$this->message} -> " . json_encode($this->data) . ".";
51+
}
52+
53+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
4+
namespace HalloVerden\HttpExceptions\Utility;
5+
6+
/**
7+
* Class ValidationExceptionViolation
8+
*
9+
* @package HalloVerden\RequestEntityBundle\Exception
10+
*/
11+
class ValidationExceptionViolation {
12+
13+
/**
14+
* @var string|null
15+
*/
16+
private $errorName;
17+
18+
/**
19+
* @var mixed
20+
*/
21+
private $invalidValue;
22+
23+
/**
24+
* @var string|null
25+
*/
26+
private $message;
27+
28+
/**
29+
* @var string|null
30+
*/
31+
private $propertyPath;
32+
33+
/**
34+
* ValidationExceptionViolation constructor.
35+
*
36+
* @param $invalidValue
37+
* @param string $message
38+
* @param string $propertyPath
39+
*/
40+
public function __construct($invalidValue, string $message, string $propertyPath) {
41+
$this->invalidValue = $invalidValue;
42+
$this->message = $message;
43+
$this->propertyPath = $propertyPath;
44+
}
45+
46+
/**
47+
* @return string|null
48+
*/
49+
public function getErrorName(): ?string {
50+
return $this->errorName;
51+
}
52+
53+
/**
54+
* @param string|null $errorName
55+
*/
56+
public function setErrorName(?string $errorName): void {
57+
$this->errorName = $errorName;
58+
}
59+
60+
/**
61+
* @return mixed
62+
*/
63+
public function getInvalidValue() {
64+
return $this->invalidValue;
65+
}
66+
67+
/**
68+
* @param mixed $invalidValue
69+
*/
70+
public function setInvalidValue($invalidValue): void {
71+
$this->invalidValue = $invalidValue;
72+
}
73+
74+
/**
75+
* @return string|null
76+
*/
77+
public function getMessage(): ?string {
78+
return $this->message;
79+
}
80+
81+
/**
82+
* @param string|null $message
83+
*/
84+
public function setMessage(?string $message): void {
85+
$this->message = $message;
86+
}
87+
88+
/**
89+
* @return string|null
90+
*/
91+
public function getPropertyPath(): ?string {
92+
return $this->propertyPath;
93+
}
94+
95+
/**
96+
* @return array
97+
*/
98+
public function toArray(): array {
99+
$a = [
100+
'invalidValue' => $this->getInvalidValue(),
101+
'message' => $this->getMessage(),
102+
'propertyPath' => $this->getPropertyPath()
103+
];
104+
105+
if ($this->getErrorName()) {
106+
$a['errorName'] = $this->getErrorName();
107+
}
108+
109+
return $a;
110+
}
111+
}

0 commit comments

Comments
 (0)