Skip to content

Commit 32bcbbc

Browse files
author
Daniel Opitz
committed
Added ValidationException
1 parent 52a64b1 commit 32bcbbc

File tree

9 files changed

+271
-50
lines changed

9 files changed

+271
-50
lines changed

src/Validation/ServiceResult.php

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,81 @@
22

33
namespace Odan\Validation;
44

5+
use JsonSerializable;
6+
57
/**
6-
* ServiceResult.
8+
* Encapsulates the state of model binding to a property of an action-method argument, or to the argument itself.
79
*/
8-
class ServiceResult extends ValidationResult
10+
class ServiceResult implements JsonSerializable
911
{
1012
/**
11-
* @var mixed|null
13+
* @var bool Success
1214
*/
13-
protected $result;
15+
protected $success = true;
16+
17+
/**
18+
* @var string|null
19+
*/
20+
protected $message = null;
1421

1522
/**
1623
* @var mixed|null Code
1724
*/
1825
protected $code = null;
1926

27+
/**
28+
* @var mixed|null
29+
*/
30+
protected $result;
31+
32+
/**
33+
* Get message.
34+
*
35+
* @return string|null
36+
*/
37+
public function getMessage(): ?string
38+
{
39+
return $this->message;
40+
}
41+
42+
/**
43+
* Set the default success message.
44+
*
45+
* @param string $message The default success message
46+
*
47+
* @return $this
48+
*/
49+
public function setMessage(string $message)
50+
{
51+
$this->message = $message;
52+
53+
return $this;
54+
}
55+
56+
/**
57+
* Set the success status.
58+
*
59+
* @param bool $success The success status
60+
*
61+
* @return $this self
62+
*/
63+
public function setSuccess(bool $success)
64+
{
65+
$this->success = $success;
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* Returns the success of the validation.
72+
*
73+
* @return bool true if validation was successful; otherwise, false
74+
*/
75+
public function isSuccess(): bool
76+
{
77+
return $this->success;
78+
}
79+
2080
/**
2181
* Get the code.
2282
*
@@ -42,7 +102,7 @@ public function setCode($code)
42102
}
43103

44104
/**
45-
* Get the result.
105+
* Returns the value that was being bound during model binding.
46106
*
47107
* @return mixed The result data
48108
*/
@@ -52,7 +112,7 @@ public function getResult()
52112
}
53113

54114
/**
55-
* Set the result.
115+
* Set the result data.
56116
*
57117
* @param mixed $result The result data
58118
*
@@ -72,7 +132,14 @@ public function setResult($result)
72132
*/
73133
public function toArray(): array
74134
{
75-
$data = parent::toArray();
135+
$data = [
136+
'success' => $this->isSuccess(),
137+
];
138+
139+
$message = $this->getMessage();
140+
if ($message !== null) {
141+
$data['message'] = $message;
142+
}
76143

77144
$code = $this->getCode();
78145
if ($code !== null) {
@@ -86,4 +153,14 @@ public function toArray(): array
86153

87154
return $data;
88155
}
156+
157+
/**
158+
* Serializes the object to a value that can be serialized natively by json_encode().
159+
*
160+
* @return array|mixed
161+
*/
162+
public function jsonSerialize()
163+
{
164+
return $this->toArray();
165+
}
89166
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Represents a status and a message.
99
*/
10-
class ErrorMessage
10+
class ValidationError
1111
{
1212
/**
1313
* @var string|null
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Odan\Validation;
4+
5+
use DomainException;
6+
use Throwable;
7+
8+
/**
9+
* Class ValidationException.
10+
*/
11+
class ValidationException extends DomainException
12+
{
13+
/**
14+
* @var ValidationResult
15+
*/
16+
protected $validationResult;
17+
18+
/**
19+
* Construct the exception.
20+
*
21+
* @param ValidationResult $validationResult The validation result object
22+
* @param string $message [optional] The Exception message to throw
23+
* @param int $code [optional] The Exception code
24+
* @param Throwable|null $previous [optional] The previous throwable used for the exception chaining
25+
*/
26+
public function __construct(ValidationResult $validationResult, string $message = '', int $code = 0, Throwable $previous = null)
27+
{
28+
parent::__construct($message, $code, $previous);
29+
30+
$this->validationResult = $validationResult;
31+
}
32+
33+
/**
34+
* @return ValidationResult
35+
*/
36+
public function getValidation(): ValidationResult
37+
{
38+
return $this->validationResult;
39+
}
40+
}

src/Validation/ValidationResult.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Odan\Validation;
44

5+
use JsonSerializable;
6+
57
/**
68
* Validation Result.
79
*
@@ -11,13 +13,8 @@
1113
*
1214
* https://martinfowler.com/articles/replaceThrowWithNotification.html
1315
*/
14-
class ValidationResult
16+
class ValidationResult implements JsonSerializable
1517
{
16-
/**
17-
* @var bool Success
18-
*/
19-
protected $success = true;
20-
2118
/**
2219
* @var array
2320
*/
@@ -31,7 +28,7 @@ class ValidationResult
3128
/**
3229
* Get all errors.
3330
*
34-
* @return ErrorMessage[] Errors
31+
* @return ValidationError[] Errors
3532
*/
3633
public function getErrors(): array
3734
{
@@ -41,7 +38,7 @@ public function getErrors(): array
4138
/**
4239
* Get first error.
4340
*
44-
* @return ErrorMessage|null Error message
41+
* @return ValidationError|null Error message
4542
*/
4643
public function getFirstError()
4744
{
@@ -72,28 +69,14 @@ public function setMessage(string $message)
7269
return $this;
7370
}
7471

75-
/**
76-
* Set the success status.
77-
*
78-
* @param bool $success The success status
79-
*
80-
* @return $this self
81-
*/
82-
public function setSuccess(bool $success)
83-
{
84-
$this->success = $success;
85-
86-
return $this;
87-
}
88-
8972
/**
9073
* Returns the success of the validation.
9174
*
9275
* @return bool true if validation was successful; otherwise, false
9376
*/
9477
public function isSuccess(): bool
9578
{
96-
return $this->success;
79+
return empty($this->errors);
9780
}
9881

9982
/**
@@ -115,7 +98,6 @@ public function clear()
11598
{
11699
$this->message = null;
117100
$this->errors = [];
118-
$this->success = true;
119101

120102
return $this;
121103
}
@@ -132,9 +114,7 @@ public function clear()
132114
*/
133115
public function addError(string $field, string $message, string $code = null)
134116
{
135-
$this->setSuccess(false);
136-
137-
$message = new ErrorMessage($message);
117+
$message = new ValidationError($message);
138118
$message->setField($field)->setCode($code);
139119

140120
$this->errors[] = $message;
@@ -143,16 +123,15 @@ public function addError(string $field, string $message, string $code = null)
143123
}
144124

145125
/**
146-
* Add a message object.
126+
* Add a validation error object.
147127
*
148-
* @param ErrorMessage $errorMessage The message object
128+
* @param ValidationError $validationError The error object
149129
*
150130
* @return $this
151131
*/
152-
public function addErrorMessage(ErrorMessage $errorMessage)
132+
public function addValidationError(ValidationError $validationError)
153133
{
154-
$this->setSuccess(false);
155-
$this->errors[] = $errorMessage;
134+
$this->errors[] = $validationError;
156135

157136
return $this;
158137
}
@@ -182,4 +161,14 @@ public function toArray(): array
182161

183162
return $result;
184163
}
164+
165+
/**
166+
* Serializes the object to a value that can be serialized natively by json_encode().
167+
*
168+
* @return array|mixed
169+
*/
170+
public function jsonSerialize()
171+
{
172+
return $this->toArray();
173+
}
185174
}

tests/ServiceResultTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Odan\Validation\ServiceResult;
66

77
/**
8-
* ValidationResult tests.
8+
* Tests.
99
*
1010
* @coversDefaultClass \Odan\Validation\ServiceResultTest
1111
*/

tests/TestService.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Odan\Validation\Test;
4+
5+
use Odan\Validation\ServiceResult;
6+
use Odan\Validation\ValidationException;
7+
use Odan\Validation\ValidationResult;
8+
9+
/**
10+
* Test service.
11+
*/
12+
class TestService
13+
{
14+
/**
15+
* Process.
16+
*
17+
* @param int $id ID
18+
*
19+
* @throws ValidationException
20+
*
21+
* @return ServiceResult Result data
22+
*/
23+
public function process(int $id): ServiceResult
24+
{
25+
$validation = new ValidationResult();
26+
27+
if ($id < 1) {
28+
$validation->addError('id', 'invalid');
29+
}
30+
31+
if ($validation->isFailed()) {
32+
$validation->setMessage('Please check your input');
33+
34+
throw new ValidationException($validation);
35+
}
36+
37+
$result = new ServiceResult();
38+
$result->setSuccess(true);
39+
$result->setMessage('Successfully');
40+
$result->setCode(0);
41+
$result->setResult(['foo' => 'value']);
42+
43+
return $result;
44+
}
45+
}

0 commit comments

Comments
 (0)