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

Commit 5400f45

Browse files
author
Andrey Helldar
authored
Fixed data splitting
1 parent 3138a7a commit 5400f45

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/Services/ResponseService.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function data($data = null): self
6161
*/
6262
public function with(array $with = []): self
6363
{
64-
$this->with = $with;
64+
$this->with = array_merge($this->with, $with);
6565

6666
return $this;
6767
}
@@ -93,7 +93,7 @@ protected function isError(): bool
9393

9494
private function e($value = null, $doubleEncode = true)
9595
{
96-
if (!is_string($value) || null === $value) {
96+
if (! is_string($value) || null === $value) {
9797
return $value;
9898
}
9999

@@ -104,6 +104,8 @@ private function e($value = null, $doubleEncode = true)
104104

105105
private function getData()
106106
{
107+
$this->splitData();
108+
107109
$data = $this->isError()
108110
? $this->getErrorData()
109111
: $this->getSuccessData();
@@ -116,15 +118,15 @@ private function getErrorData(): array
116118
return [
117119
'error' => [
118120
'code' => $this->status_code,
119-
'data' => $this->wrappedData(),
121+
'data' => $this->e($this->data),
120122
],
121123
];
122124
}
123125

124-
private function getSuccessData()
126+
private function getSuccessData(): array
125127
{
126128
return [
127-
'data' => $this->wrappedData(),
129+
'data' => $this->e($this->data),
128130
];
129131
}
130132

@@ -135,11 +137,24 @@ private function mergeDataWith(array $data = []): array
135137
: $data;
136138
}
137139

138-
private function wrappedData()
140+
private function splitData(): void
139141
{
140-
return is_array($this->data) || is_object($this->data)
141-
? $this->data->data ?? $this->data['data'] ?? $this->data
142-
: $this->e($this->data);
142+
if (! is_array($this->data) && ! is_object($this->data)) {
143+
return;
144+
}
145+
146+
$data = is_object($this->data)
147+
? get_object_vars($this->data)
148+
: $this->data;
149+
150+
if (isset($data['data'])) {
151+
$with = array_filter($data, function ($key) {
152+
return $key !== 'data';
153+
}, ARRAY_FILTER_USE_KEY);
154+
155+
$this->data = $data['data'];
156+
$this->with($with);
157+
}
143158
}
144159

145160
private function toResponse(Responsable $content)

0 commit comments

Comments
 (0)