Skip to content

Commit d9892b2

Browse files
committed
Make ErrorBag Json Serializable
1 parent 2757c79 commit d9892b2

File tree

4 files changed

+78
-19
lines changed

4 files changed

+78
-19
lines changed

src/Contracts/ErrorBagInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public function get( $key );
4444
* @return mixed
4545
*/
4646
public function isEmpty();
47+
48+
/**
49+
* @return array
50+
*/
51+
public function toArray();
4752
}

src/ErrorBag.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @package azi
1212
*/
13-
class ErrorBag implements ArrayAccess, ErrorBagInterface
13+
class ErrorBag implements ArrayAccess, ErrorBagInterface, \JsonSerializable
1414
{
1515
/**
1616
* @var ErrorMessages[]
@@ -92,14 +92,6 @@ public function hasErrors()
9292
return !empty($this->errors);
9393
}
9494

95-
/**
96-
* @return array
97-
*/
98-
public function getErrors()
99-
{
100-
return $this->errors;
101-
}
102-
10395
/**
10496
* @param $key
10597
* @return mixed
@@ -129,4 +121,40 @@ public function isEmpty()
129121
{
130122
return empty($this->errors);
131123
}
124+
125+
/**
126+
* Specify data which should be serialized to JSON
127+
*
128+
* @return array|\stdClass data to be json serialized
129+
*/
130+
function jsonSerialize()
131+
{
132+
return $this->getErrors();
133+
}
134+
135+
/**
136+
* @return array
137+
*/
138+
public function getErrors()
139+
{
140+
return $this->errors;
141+
}
142+
143+
/**
144+
* @return string
145+
*/
146+
public function toJson()
147+
{
148+
return json_encode($this->getErrors());
149+
}
150+
151+
/**
152+
* @return array
153+
*/
154+
public function toArray()
155+
{
156+
return array_map(function ( ErrorMessages $error ) {
157+
return $error->toArray();
158+
}, $this->getErrors());
159+
}
132160
}

src/ErrorMessages.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @package azi
99
*/
10-
class ErrorMessages implements \ArrayAccess
10+
class ErrorMessages implements \ArrayAccess, \JsonSerializable
1111
{
1212
/**
1313
* @var array
@@ -120,14 +120,6 @@ public function offsetUnset( $offset )
120120
unset($this->messages);
121121
}
122122

123-
/**
124-
* @return array
125-
*/
126-
public function toArray()
127-
{
128-
return $this->messages;
129-
}
130-
131123
/**
132124
* Returns the first message as string
133125
*
@@ -145,4 +137,22 @@ public function first()
145137
{
146138
return reset($this->messages);
147139
}
140+
141+
/**
142+
* Specify data which should be serialized to JSON
143+
*
144+
* @return array|\stdClass data to be json serialized
145+
*/
146+
function jsonSerialize()
147+
{
148+
return $this->toArray();
149+
}
150+
151+
/**
152+
* @return array
153+
*/
154+
public function toArray()
155+
{
156+
return $this->messages;
157+
}
148158
}
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* @package azi
1010
*/
11-
class ValidatorTest extends TestCase
11+
class EnvalidTest extends TestCase
1212
{
1313
public function testItAcceptsRules()
1414
{
@@ -76,5 +76,21 @@ public function testItValidatesEmailFields()
7676
$this->assertTrue($this->validator->passed());
7777
}
7878

79+
public function testTheErrorBagIsJsonSerializable()
80+
{
81+
$this->validator->validate([
82+
'name' => null
83+
], [
84+
'name' => 'required'
85+
]);
7986

87+
$errors = $this->validator->getErrors();
88+
89+
$errorsJson = json_encode($errors);
90+
$this->assertJson($errorsJson);
91+
92+
$errors = json_decode($errorsJson);
93+
$expected = (object) ['name' => ['Name is required']];
94+
$this->assertEquals($expected, $errors);
95+
}
8096
}

0 commit comments

Comments
 (0)