Skip to content

Commit a6ced4e

Browse files
committed
Empty model now become {} after json_encode
- `->toArray()` method has been removed in favor of PHP function `iterator_to_array`
1 parent 8a372ef commit a6ced4e

3 files changed

Lines changed: 43 additions & 21 deletions

File tree

src/Struct.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Respect\Validation\Exceptions\NestedValidationException;
1111
use Respect\Validation\Rules\AllOf;
1212
use Respect\Validation\Rules\Key as ValidationKey;
13-
use Respect\Validation\Rules\KeySet;
1413

1514
abstract class Struct implements \Iterator, \Countable, \JsonSerializable
1615
{
@@ -120,7 +119,7 @@ public function get(string $key, $default = null)
120119
*/
121120
public function set(string $key, $value)
122121
{
123-
$data = $this->toArray();
122+
$data = iterator_to_array($this);
124123
$data[$key] = $value;
125124
return new static($data);
126125
}
@@ -133,35 +132,24 @@ public function set(string $key, $value)
133132
*/
134133
public function delete(string $key)
135134
{
136-
$data = $this->toArray();
135+
$data = iterator_to_array($this);
137136
unset($data[$key]);
138137
return new static($data);
139138
}
140139

141140
/**
142-
* @return array
143-
*/
144-
public function toArray(): array
145-
{
146-
return array_reduce($this->__defined_props, function ($carry, $prop) {
147-
$carry[$prop] = $this->{$prop};
148-
return $carry;
149-
}, []);
150-
}
151-
152-
/**
153-
* @return array
141+
* @return object
154142
*/
155143
public function jsonSerialize()
156144
{
157-
$data = [];
145+
$data = new \stdClass();
158146
foreach (static::getSchema()->getProps() as $prop) {
159147
if (!$this->has($prop->getName())) {
160148
continue;
161149
}
162150

163151
$value = $this->get($prop->getName());
164-
$data[$prop->getName()] = static::jsonSerializeValue($value);
152+
$data->{$prop->getName()} = static::jsonSerializeValue($value);
165153
}
166154

167155
return $data;

tests/Fixture/CanBeEmptyModel.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Acelot\Struct\Tests\Fixture;
4+
5+
use Acelot\Struct\Schema;
6+
use Acelot\Struct\Schema\Prop;
7+
use Acelot\Struct\Struct;
8+
9+
/**
10+
* @property-read mixed $a
11+
* @property-read mixed $b
12+
* @property-read mixed $c
13+
*/
14+
class CanBeEmptyModel extends Struct
15+
{
16+
public static function getSchema(): Schema
17+
{
18+
return new Schema(
19+
Prop::create('a')
20+
->notRequired(),
21+
22+
Prop::create('b')
23+
->notRequired(),
24+
25+
Prop::create('c')
26+
->notRequired()
27+
);
28+
}
29+
}

tests/Unit/StructTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Acelot\Struct\Exception\UndefinedPropertyException;
66
use Acelot\Struct\Exception\ValidationException;
77
use Acelot\Struct\Schema;
8+
use Acelot\Struct\Tests\Fixture\CanBeEmptyModel;
89
use Acelot\Struct\Tests\Fixture\CreateUserModel;
910
use PHPUnit\Framework\Error\Notice;
1011
use PHPUnit\Framework\TestCase;
@@ -31,7 +32,7 @@ public function testConstructAndOtherMethods()
3132
'login' => 'superhacker',
3233
'password' => 'correcthorsebatterystaple',
3334
'birthday' => new \DateTimeImmutable('1988-08-08')
34-
], $model->toArray());
35+
], iterator_to_array($model));
3536

3637
$this->assertTrue(property_exists($model, 'login'));
3738
$this->assertTrue($model->has('login'));
@@ -133,8 +134,12 @@ public function testJsonSerialize()
133134
'birthday' => new \DateTimeImmutable('1988-08-08', new \DateTimeZone('UTC'))
134135
]);
135136

136-
$json = json_encode($model);
137-
$this->assertEquals('{"login":"superhacker","password":"correcthorsebatterystaple","birthday":"1988-08-08T00:00:00.000+00:00"}',
138-
$json);
137+
$this->assertEquals(
138+
'{"login":"superhacker","password":"correcthorsebatterystaple","birthday":"1988-08-08T00:00:00.000+00:00"}',
139+
json_encode($model)
140+
);
141+
142+
$emptyModel = new CanBeEmptyModel([]);
143+
$this->assertEquals('{}', json_encode($emptyModel));
139144
}
140145
}

0 commit comments

Comments
 (0)