Skip to content

Commit 52a64b1

Browse files
author
Daniel Opitz
committed
Added addErrorMessage method
1 parent c25a842 commit 52a64b1

File tree

4 files changed

+94
-20
lines changed

4 files changed

+94
-20
lines changed
Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
*
88
* Represents a status and a message.
99
*/
10-
class ValidationMessage
10+
class ErrorMessage
1111
{
1212
/**
13-
* @var string
13+
* @var string|null
1414
*/
1515
protected $field;
1616

@@ -27,15 +27,11 @@ class ValidationMessage
2727
/**
2828
* Constructor.
2929
*
30-
* @param string $field The field name
3130
* @param string $message The Message
32-
* @param string|null $code The error code (optional)
3331
*/
34-
public function __construct(string $field, string $message, string $code = null)
32+
public function __construct(string $message)
3533
{
36-
$this->field = $field;
3734
$this->message = $message;
38-
$this->code = $code;
3935
}
4036

4137
/**
@@ -48,16 +44,44 @@ public function getMessage(): string
4844
return $this->message;
4945
}
5046

47+
/**
48+
* Set the field name.
49+
*
50+
* @param string $field The field name
51+
*
52+
* @return $this self
53+
*/
54+
public function setField(string $field)
55+
{
56+
$this->field = $field;
57+
58+
return $this;
59+
}
60+
5161
/**
5262
* Returns the field name.
5363
*
54-
* @return string The field name
64+
* @return string|null The field name
5565
*/
56-
public function getField(): string
66+
public function getField()
5767
{
5868
return $this->field;
5969
}
6070

71+
/**
72+
* Set the field name.
73+
*
74+
* @param mixed $code The error code
75+
*
76+
* @return $this self
77+
*/
78+
public function setCode($code)
79+
{
80+
$this->code = $code;
81+
82+
return $this;
83+
}
84+
6185
/**
6286
* Returns the validation status code.
6387
*
@@ -76,10 +100,14 @@ public function getCode(): ?string
76100
public function toArray(): array
77101
{
78102
$result = [
79-
'field' => $this->getField(),
80103
'message' => $this->getMessage(),
81104
];
82105

106+
$field = $this->getField();
107+
if ($field !== null) {
108+
$result['field'] = $field;
109+
}
110+
83111
$code = $this->getCode();
84112
if ($code !== null) {
85113
$result['code'] = $code;

src/Validation/ValidationResult.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ValidationResult
3131
/**
3232
* Get all errors.
3333
*
34-
* @return ValidationMessage[] Errors
34+
* @return ErrorMessage[] Errors
3535
*/
3636
public function getErrors(): array
3737
{
@@ -41,7 +41,7 @@ public function getErrors(): array
4141
/**
4242
* Get first error.
4343
*
44-
* @return ValidationMessage|null Error message
44+
* @return ErrorMessage|null Error message
4545
*/
4646
public function getFirstError()
4747
{
@@ -133,7 +133,26 @@ public function clear()
133133
public function addError(string $field, string $message, string $code = null)
134134
{
135135
$this->setSuccess(false);
136-
$this->errors[] = new ValidationMessage($field, $message, $code);
136+
137+
$message = new ErrorMessage($message);
138+
$message->setField($field)->setCode($code);
139+
140+
$this->errors[] = $message;
141+
142+
return $this;
143+
}
144+
145+
/**
146+
* Add a message object.
147+
*
148+
* @param ErrorMessage $errorMessage The message object
149+
*
150+
* @return $this
151+
*/
152+
public function addErrorMessage(ErrorMessage $errorMessage)
153+
{
154+
$this->setSuccess(false);
155+
$this->errors[] = $errorMessage;
137156

138157
return $this;
139158
}

tests/ValidationMessageTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
namespace Odan\Validation\Test;
44

5-
use Odan\Validation\ValidationMessage;
5+
use Odan\Validation\ErrorMessage;
66
use PHPUnit\Framework\TestCase;
77

88
/**
99
* Tests.
1010
*
11-
* @coversDefaultClass \Odan\Validation\ValidationMessage
11+
* @coversDefaultClass \Odan\Validation\ErrorMessage
1212
*/
1313
class ValidationMessageTest extends TestCase
1414
{
1515
public function testConstruct()
1616
{
17-
$message = new ValidationMessage('', '');
18-
$this->assertInstanceOf(ValidationMessage::class, $message);
17+
$message = new ErrorMessage('');
18+
$this->assertInstanceOf(ErrorMessage::class, $message);
1919
}
2020

2121
public function testWithField()
2222
{
23-
$message = new ValidationMessage('email', 'required');
23+
$message = new ErrorMessage('required');
24+
$message->setField('email');
25+
2426
$this->assertEquals([
2527
'field' => 'email',
2628
'message' => 'required',
@@ -29,7 +31,10 @@ public function testWithField()
2931

3032
public function testWithFieldAndCode()
3133
{
32-
$message = new ValidationMessage('email', 'invalid', '5000');
34+
$message = new ErrorMessage('invalid');
35+
$message->setField('email');
36+
$message->setCode('5000');
37+
3338
$this->assertEquals([
3439
'field' => 'email',
3540
'message' => 'invalid',

tests/ValidationResultTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Odan\Validation\Test;
44

5+
use Odan\Validation\ErrorMessage;
56
use Odan\Validation\ValidationResult;
67
use PHPUnit\Framework\TestCase;
78

@@ -93,6 +94,27 @@ public function testErrorsWithMessage()
9394
$this->assertEquals($expected, $val->getFirstError()->toArray());
9495
}
9596

97+
/**
98+
* Tests addError and success functions.
99+
* Tests addError function with null for the second parameter.
100+
*
101+
* @return void
102+
*/
103+
public function testAddErrorMessage()
104+
{
105+
$val = new ValidationResult();
106+
$message = new ErrorMessage('required');
107+
$message->setField('email')->setCode('5000');
108+
109+
$val->addErrorMessage($message);
110+
$result = $val->isFailed();
111+
$this->assertTrue($result);
112+
113+
$expected = ['field' => 'email', 'message' => 'required', 'code' => '5000'];
114+
115+
$this->assertEquals($expected, $val->getFirstError()->toArray());
116+
}
117+
96118
/**
97119
* Tests success function.
98120
* Tests for no errors.
@@ -130,7 +152,7 @@ public function testClear()
130152
{
131153
$val = new ValidationResult();
132154
$val->setMessage('Errors');
133-
$val->addError('error', 'error');
155+
$val->addError('email', 'required');
134156
$val->clear();
135157
$result = $val->isFailed();
136158
$this->assertFalse($result);

0 commit comments

Comments
 (0)