Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 6cf3967

Browse files
author
Andrey Helldar
committed
Fixed object unpacking error
1 parent b340cc1 commit 6cf3967

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

src/Wrappers/Resolver.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function get()
6262
*/
6363
protected function success()
6464
{
65-
$data = $this->data;
65+
$data = $this->resolveData($this->data);
6666

6767
if ($this->wrap) {
6868
$data = is_array($data) && Arr::exists($data, 'data') ? $data : compact('data');
@@ -77,11 +77,20 @@ protected function success()
7777

7878
protected function failed(): array
7979
{
80-
return $this->mergeWith($this->data);
80+
return $this->mergeWith($this->resolveData($this->data));
8181
}
8282

8383
protected function mergeWith(array $data): array
8484
{
8585
return $this->allow_with ? array_merge($data, $this->with) : $data;
8686
}
87+
88+
protected function resolveData($data)
89+
{
90+
if (! is_array($data) && ! is_object($data)) {
91+
return $data;
92+
}
93+
94+
return Arr::toArray($data);
95+
}
8796
}

src/Wrappers/Wrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function split(): void
113113
$with = $this->getWith();
114114

115115
if (is_array($data) || is_object($data)) {
116-
$array = Arr::wrap($data);
116+
$array = Arr::toArray($data);
117117

118118
if ($this->wrap || ! empty($with) || $this->isError($array)) {
119119
$this->setData($this->unpackData($array));

tests/Fixtures/Entities/Arrayable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Entities;
4+
5+
use Illuminate\Contracts\Support\Arrayable as Contract;
6+
7+
class Arrayable implements Contract
8+
{
9+
public function foo(): string
10+
{
11+
return 'foo';
12+
}
13+
14+
public function toArray()
15+
{
16+
return [
17+
'values' => [
18+
'value' => $this->foo(),
19+
],
20+
];
21+
}
22+
}

tests/Symfony/Parsers/Main/NoWithDataTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Symfony\Parsers\Main;
44

55
use Symfony\Component\HttpFoundation\JsonResponse;
6+
use Tests\Fixtures\Entities\Arrayable;
67
use Tests\Symfony\TestCase;
78

89
final class NoWithDataTest extends TestCase
@@ -30,6 +31,8 @@ public function testResponse()
3031
$this->assertTrue($this->response('foo', 300)->instance() instanceof JsonResponse);
3132
$this->assertTrue($this->response([], 300)->instance() instanceof JsonResponse);
3233
$this->assertTrue($this->response(0, 300)->instance() instanceof JsonResponse);
34+
35+
$this->assertTrue($this->response(new Arrayable())->instance() instanceof JsonResponse);
3336
}
3437

3538
public function testJson()
@@ -53,6 +56,8 @@ public function testJson()
5356
$this->assertJson($this->response('foo', 300)->getRaw());
5457
$this->assertJson($this->response([], 300)->getRaw());
5558
$this->assertJson($this->response(0, 300)->getRaw());
59+
60+
$this->assertJson($this->response(new Arrayable())->getRaw());
5661
}
5762

5863
public function testStructure()
@@ -91,6 +96,8 @@ public function testStructure()
9196
$this->assertSame(['data' => 0], $this->response(0, 200)->getJson());
9297
$this->assertSame(['data' => 0], $this->response(0, 204)->getJson());
9398
$this->assertSame(['data' => 0], $this->response(0, 300)->getJson());
99+
100+
$this->assertSame(['data' => ['values' => ['value' => 'foo']]], $this->response(new Arrayable())->getJson());
94101
}
95102

96103
public function testStatusCode()
@@ -114,5 +121,7 @@ public function testStatusCode()
114121
$this->assertSame(300, $this->response('foo', 300)->getStatusCode());
115122
$this->assertSame(300, $this->response([], 300)->getStatusCode());
116123
$this->assertSame(300, $this->response(0, 300)->getStatusCode());
124+
125+
$this->assertSame(200, $this->response(new Arrayable())->getStatusCode());
117126
}
118127
}

tests/Symfony/Parsers/Main/NoWithNoDataTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Symfony\Parsers\Main;
44

55
use Symfony\Component\HttpFoundation\JsonResponse;
6+
use Tests\Fixtures\Entities\Arrayable;
67
use Tests\Symfony\TestCase;
78

89
final class NoWithNoDataTest extends TestCase
@@ -32,6 +33,8 @@ public function testResponse()
3233
$this->assertTrue($this->response('foo', 300)->instance() instanceof JsonResponse);
3334
$this->assertTrue($this->response([], 300)->instance() instanceof JsonResponse);
3435
$this->assertTrue($this->response(0, 300)->instance() instanceof JsonResponse);
36+
37+
$this->assertTrue($this->response(new Arrayable())->instance() instanceof JsonResponse);
3538
}
3639

3740
public function testJson()
@@ -55,6 +58,8 @@ public function testJson()
5558
$this->assertJson($this->response('foo', 300)->getRaw());
5659
$this->assertJson($this->response([], 300)->getRaw());
5760
$this->assertJson($this->response(0, 300)->getRaw());
61+
62+
$this->assertJson($this->response(new Arrayable())->getRaw());
5863
}
5964

6065
public function testStructure()
@@ -93,6 +98,8 @@ public function testStructure()
9398
$this->assertSame(0, $this->response(0, 200)->getJson());
9499
$this->assertSame(0, $this->response(0, 204)->getJson());
95100
$this->assertSame(0, $this->response(0, 300)->getJson());
101+
102+
$this->assertSame(['values' => ['value' => 'foo']], $this->response(new Arrayable())->getJson());
96103
}
97104

98105
public function testStatusCode()
@@ -116,5 +123,7 @@ public function testStatusCode()
116123
$this->assertSame(300, $this->response('foo', 300)->getStatusCode());
117124
$this->assertSame(300, $this->response([], 300)->getStatusCode());
118125
$this->assertSame(300, $this->response(0, 300)->getStatusCode());
126+
127+
$this->assertSame(200, $this->response(new Arrayable())->getStatusCode());
119128
}
120129
}

tests/Symfony/Parsers/Main/WithDataTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\HttpFoundation\JsonResponse;
66
use Tests\Fixtures\Contracts\Parserable;
7+
use Tests\Fixtures\Entities\Arrayable;
78
use Tests\Symfony\TestCase;
89

910
class WithDataTest extends TestCase implements Parserable
@@ -29,6 +30,8 @@ public function testResponse()
2930
$this->assertTrue($this->response('foo', 300)->instance() instanceof JsonResponse);
3031
$this->assertTrue($this->response([], 300)->instance() instanceof JsonResponse);
3132
$this->assertTrue($this->response(0, 300)->instance() instanceof JsonResponse);
33+
34+
$this->assertTrue($this->response(new Arrayable())->instance() instanceof JsonResponse);
3235
}
3336

3437
public function testJson()
@@ -52,6 +55,8 @@ public function testJson()
5255
$this->assertJson($this->response('foo', 300)->getRaw());
5356
$this->assertJson($this->response([], 300)->getRaw());
5457
$this->assertJson($this->response(0, 300)->getRaw());
58+
59+
$this->assertJson($this->response(new Arrayable())->getRaw());
5560
}
5661

5762
public function testStructure()
@@ -95,6 +100,8 @@ public function testStructure()
95100
$this->assertSame(['data' => 0], $this->response(0, 200)->getJson());
96101
$this->assertSame(['data' => 0], $this->response(0, 204)->getJson());
97102
$this->assertSame(['data' => 0], $this->response(0, 300)->getJson());
103+
104+
$this->assertSame(['data' => ['values' => ['value' => 'foo']]], $this->response(new Arrayable())->getJson());
98105
}
99106

100107
public function testStatusCode()
@@ -118,5 +125,7 @@ public function testStatusCode()
118125
$this->assertSame(300, $this->response('foo', 300)->getStatusCode());
119126
$this->assertSame(300, $this->response([], 300)->getStatusCode());
120127
$this->assertSame(300, $this->response(0, 300)->getStatusCode());
128+
129+
$this->assertSame(200, $this->response(new Arrayable())->getStatusCode());
121130
}
122131
}

tests/Symfony/Parsers/Main/WithNoDataTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\HttpFoundation\JsonResponse;
66
use Tests\Fixtures\Contracts\Parserable;
7+
use Tests\Fixtures\Entities\Arrayable;
78
use Tests\Symfony\TestCase;
89

910
final class WithNoDataTest extends TestCase implements Parserable
@@ -31,6 +32,8 @@ public function testResponse()
3132
$this->assertTrue($this->response('foo', 300)->instance() instanceof JsonResponse);
3233
$this->assertTrue($this->response([], 300)->instance() instanceof JsonResponse);
3334
$this->assertTrue($this->response(0, 300)->instance() instanceof JsonResponse);
35+
36+
$this->assertTrue($this->response(new Arrayable())->instance() instanceof JsonResponse);
3437
}
3538

3639
public function testJson()
@@ -54,6 +57,8 @@ public function testJson()
5457
$this->assertJson($this->response('foo', 300)->getRaw());
5558
$this->assertJson($this->response([], 300)->getRaw());
5659
$this->assertJson($this->response(0, 300)->getRaw());
60+
61+
$this->assertJson($this->response(new Arrayable())->getRaw());
5762
}
5863

5964
public function testStructure()
@@ -92,6 +97,8 @@ public function testStructure()
9297
$this->assertSame(0, $this->response(0, 200)->getJson());
9398
$this->assertSame(0, $this->response(0, 204)->getJson());
9499
$this->assertSame(0, $this->response(0, 300)->getJson());
100+
101+
$this->assertSame(['values' => ['value' => 'foo']], $this->response(new Arrayable())->getJson());
95102
}
96103

97104
public function testStatusCode()
@@ -115,5 +122,7 @@ public function testStatusCode()
115122
$this->assertSame(300, $this->response('foo', 300)->getStatusCode());
116123
$this->assertSame(300, $this->response([], 300)->getStatusCode());
117124
$this->assertSame(300, $this->response(0, 300)->getStatusCode());
125+
126+
$this->assertSame(200, $this->response(new Arrayable())->getStatusCode());
118127
}
119128
}

0 commit comments

Comments
 (0)