Skip to content

Commit 8ecff2d

Browse files
author
Daniel Opitz
committed
Added setCode and getCode
Removed fluent interface
1 parent d541ba5 commit 8ecff2d

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/Validation/ValidationResult.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class ValidationResult implements JsonSerializable
2525
*/
2626
protected $message = null;
2727

28+
/**
29+
* @var string|null
30+
*/
31+
protected $code = null;
32+
2833
/**
2934
* Get all errors.
3035
*
@@ -60,13 +65,33 @@ public function getMessage(): ?string
6065
*
6166
* @param string $message The default success message
6267
*
63-
* @return $this
68+
* @return void
6469
*/
6570
public function setMessage(string $message)
6671
{
6772
$this->message = $message;
73+
}
74+
75+
/**
76+
* Get the error code.
77+
*
78+
* @return string|null
79+
*/
80+
public function getCode(): ?string
81+
{
82+
return $this->code;
83+
}
6884

69-
return $this;
85+
/**
86+
* Set the error code.
87+
*
88+
* @param string $message The error code
89+
*
90+
* @return void
91+
*/
92+
public function setCode(string $message)
93+
{
94+
$this->code = $message;
7095
}
7196

7297
/**
@@ -92,14 +117,13 @@ public function isFailed(): bool
92117
/**
93118
* Clear errors and message.
94119
*
95-
* @return $this
120+
* @return void
96121
*/
97122
public function clear()
98123
{
124+
$this->code = null;
99125
$this->message = null;
100126
$this->errors = [];
101-
102-
return $this;
103127
}
104128

105129
/**
@@ -110,30 +134,26 @@ public function clear()
110134
* The message SHOULD be limited to a concise single sentence
111135
* @param string|null $code A numeric or alphanumeric value that indicates the error type that occurred. (optional)
112136
*
113-
* @return $this
137+
* @return void
114138
*/
115139
public function addError(string $field, string $message, string $code = null)
116140
{
117141
$message = new ValidationError($message);
118142
$message->setField($field)->setCode($code);
119143

120144
$this->errors[] = $message;
121-
122-
return $this;
123145
}
124146

125147
/**
126148
* Add a validation error object.
127149
*
128150
* @param ValidationError $validationError The error object
129151
*
130-
* @return $this
152+
* @return void
131153
*/
132154
public function addValidationError(ValidationError $validationError)
133155
{
134156
$this->errors[] = $validationError;
135-
136-
return $this;
137157
}
138158

139159
/**
@@ -143,9 +163,12 @@ public function addValidationError(ValidationError $validationError)
143163
*/
144164
public function toArray(): array
145165
{
146-
$result = [
147-
'success' => $this->isSuccess(),
148-
];
166+
$result = [];
167+
168+
$code = $this->getCode();
169+
if ($code !== null) {
170+
$result['code'] = $code;
171+
}
149172

150173
$message = $this->getMessage();
151174
if ($message !== null) {

tests/ValidationExceptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testValidationFailedAction()
3838
$json = $this->withStatus(422)->withJson($validation);
3939
}
4040

41-
$this->assertSame('{"success":false,"message":"Please check your input","errors":[{"message":"invalid","field":"id"}]}', $json);
41+
$this->assertSame('{"message":"Please check your input","errors":[{"message":"invalid","field":"id"}]}', $json);
4242
}
4343

4444
/**

0 commit comments

Comments
 (0)