@@ -61,7 +61,7 @@ public function data($data = null): self
61
61
*/
62
62
public function with (array $ with = []): self
63
63
{
64
- $ this ->with = $ with ;
64
+ $ this ->with = array_merge ( $ this -> with , $ with ) ;
65
65
66
66
return $ this ;
67
67
}
@@ -93,7 +93,7 @@ protected function isError(): bool
93
93
94
94
private function e ($ value = null , $ doubleEncode = true )
95
95
{
96
- if (!is_string ($ value ) || null === $ value ) {
96
+ if (! is_string ($ value ) || null === $ value ) {
97
97
return $ value ;
98
98
}
99
99
@@ -104,6 +104,8 @@ private function e($value = null, $doubleEncode = true)
104
104
105
105
private function getData ()
106
106
{
107
+ $ this ->splitData ();
108
+
107
109
$ data = $ this ->isError ()
108
110
? $ this ->getErrorData ()
109
111
: $ this ->getSuccessData ();
@@ -116,15 +118,15 @@ private function getErrorData(): array
116
118
return [
117
119
'error ' => [
118
120
'code ' => $ this ->status_code ,
119
- 'data ' => $ this ->wrappedData ( ),
121
+ 'data ' => $ this ->e ( $ this -> data ),
120
122
],
121
123
];
122
124
}
123
125
124
- private function getSuccessData ()
126
+ private function getSuccessData (): array
125
127
{
126
128
return [
127
- 'data ' => $ this ->wrappedData ( ),
129
+ 'data ' => $ this ->e ( $ this -> data ),
128
130
];
129
131
}
130
132
@@ -135,11 +137,24 @@ private function mergeDataWith(array $data = []): array
135
137
: $ data ;
136
138
}
137
139
138
- private function wrappedData ()
140
+ private function splitData (): void
139
141
{
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
+ }
143
158
}
144
159
145
160
private function toResponse (Responsable $ content )
0 commit comments