Skip to content

Commit e7cfa51

Browse files
committed
refactor: parse json response.
1 parent afaba66 commit e7cfa51

File tree

2 files changed

+129
-196
lines changed

2 files changed

+129
-196
lines changed

src/Rest/Response/JSON.php

+128-195
Original file line numberDiff line numberDiff line change
@@ -19,216 +19,149 @@ public function parse(string $data, array $headers): static | int | bool | strin
1919
/**
2020
* @var mixed
2121
*/
22-
$source = json_decode($data);
22+
$source = json_decode($data, true);
2323
$errors = $this->getJsonErrors();
2424
$this->originalString = $this->string = $data;
25-
if ($errors === null) {
26-
if (!(
27-
$headers['Status'] === 201
28-
|| $headers['Status'] === 200
29-
|| $headers['Status'] === 204
30-
|| $headers['Status'] === 409
31-
|| $headers['Status'] === 422
32-
|| $headers['Status'] === 400
33-
|| $headers['Status'] === 404
34-
)) {
35-
throw new Exception([
36-
'Message' => $errors,
37-
'Response' => $data,
38-
'Headers' => $headers,
39-
]);
40-
}
41-
if (in_array($headers['Status'], [201, 200, 204])) {
42-
switch ($headers['Method']) {
43-
case 'POST':
44-
if (!empty($headers['id']) && $headers['Status'] === 201) {
45-
return (int)$headers['id'];
46-
}
4725

48-
if (!empty($source->fileId)) {
49-
return (int)$source->fileId;
50-
}
51-
if (!empty($source->pendingFile->ref)) {
52-
return $source->pendingFile->ref;
53-
}
54-
/**
55-
* @var string
56-
*/
57-
$wrapper = $headers['X-Parent'];
58-
if (isset($source->$wrapper)) {
59-
/**
60-
* @var mixed
61-
*/
62-
$source = $source->$wrapper;
63-
}
64-
// no break
65-
case 'PUT':
66-
unset($source->STATUS);
67-
$keys = get_object_vars($source);
68-
if ($headers['X-Not-Use-Files'] &&
69-
($count = count($keys)) && (
70-
$count != 1 || !isset($keys['id'])
71-
)
72-
) {
73-
if (
74-
preg_match('!/(\d+)/tags!', $headers['X-Action']) && !$source->tags
75-
) {
76-
return true;
77-
}
78-
/**
79-
* @var \stdClass
80-
*/
81-
$data = static::camelizeObject($source);
82-
if (!empty($data->id)) {
83-
$data->id = (int)$data->id;
84-
}
85-
/** @psalm-suppress InvalidPropertyAssignmentValue */
86-
$this->data = $data;
87-
return $this;
88-
}
89-
if (!empty($source->id)) {
90-
$source->id = (int) $source->id;
91-
}
92-
return $source->id ?? true;
93-
case 'DELETE':
26+
[
27+
'Status' => $status,
28+
'X-Action' => $action,
29+
'X-Parent' => $wrapper,
30+
'Method' => $method
31+
] = $headers;
32+
33+
if (!in_array($status, [200, 201, 204])) {
34+
$errors = $source['MESSAGE'] ?? $source['error'] ?? $errors ?? "Unknown error ($status) status";
35+
}
36+
if ($errors !== null) {
37+
throw new Exception([
38+
'Message' => $errors,
39+
'Response' => $data,
40+
'Headers' => $headers,
41+
]);
42+
}
43+
44+
switch ($method) {
45+
case 'POST':
46+
if (!empty($headers['id']) && $status === 201) {
47+
return (int) $headers['id'];
48+
}
49+
50+
if (!empty($source['fileId'])) {
51+
return (int) $source['fileId'];
52+
}
53+
54+
if (!empty($source['pendingFile']['ref'])) {
55+
return $source['pendingFile']['ref'];
56+
}
57+
58+
if (isset($source[$wrapper])) {
59+
/**
60+
* @var mixed
61+
*/
62+
$source = $source[$wrapper];
63+
}
64+
// no break
65+
case 'PUT':
66+
unset($source['STATUS']);
67+
$keys = array_keys($source);
68+
if ($headers['X-Not-Use-Files'] &&
69+
($count = count($keys)) && (
70+
$count != 1 || !in_array('id', $keys)
71+
)
72+
) {
73+
if (
74+
preg_match('!/(\d+)/tags!', $action) && !empty($source['tags'])
75+
) {
9476
return true;
77+
}
78+
/**
79+
* @var \stdClass
80+
*/
81+
$data = static::camelizeObject($source);
82+
if (!empty($data->id)) {
83+
$data->id = (int) $data->id;
84+
}
85+
/** @psalm-suppress InvalidPropertyAssignmentValue */
86+
$this->data = $data;
87+
return $this;
88+
}
89+
$id = $source['id'] ?? null;
90+
if ($id !== null) {
91+
$id = (int) $id;
92+
}
93+
return $id ?? true;
94+
case 'DELETE':
95+
return true;
96+
97+
default:
98+
/**
99+
* @var array
100+
*/
101+
if (!empty($source['STATUS'])) {
102+
unset($source['STATUS']);
103+
}
95104

96-
default:
97-
/**
98-
* @var object
99-
*/
100-
if (!empty($source->STATUS)) {
101-
unset($source->STATUS);
102-
}
103-
$wrapper = $headers['X-Parent'];
104-
$action = $headers['X-Action'];
105-
$count = count(get_object_vars($source));
106-
if ($count == 1) {
107-
$key = key($source);
108-
$match = $wrapper == $key;
109-
$source = $match ? $source->$wrapper : current($source);
110-
// projects/967489/time/total
111-
if (
112-
$source
113-
&& preg_match('!projects/(\d+)/time/total!', $headers['X-Action'])
114-
) {
115-
$source = current($source);
116-
}
117-
if (
118-
$source &&
119-
preg_match('!messageReplies/(\d+)!', $action)
120-
) {
121-
$source = current($source);
122-
}
123-
// tasklists/2952529/time/total
124-
if ($source && preg_match('!tasklists/(\d+)/time/total!', $headers['X-Action'])) {
125-
$source = current($source);
126-
$source = $source->tasklist;
127-
}
128-
// tasks/43119773/time/total
129-
if ($source && preg_match('!tasks/(\d+)/time/total!', $headers['X-Action'])) {
130-
$source = current($source);
131-
$source = $source->tasklist->task;
132-
}
133-
if ($key === 'project') {
134-
foreach(['files'] as $key) {
135-
if (isset($source->$key)) {
136-
$source = $source->$key;
137-
break;
138-
}
139-
}
140-
} elseif ($key === 'projects' && $action === 'files') {
141-
$data = [];
142-
foreach($source as $project) {
143-
foreach ($project->files as $file) {
144-
$data[] = $file;
145-
}
146-
}
147-
$source = $data;
148-
}
105+
$count = count($source);
106+
if ($count == 1) {
107+
$key = key($source);
108+
$match = $wrapper == $key;
109+
$source = $match ? $source[$wrapper] : current($source);
110+
if ($source) {
111+
if (preg_match('!projects/(\d+)/time/total!', $action)) {
112+
$source = current($source);
149113
} elseif (
150-
isset($source->card)
151-
&& preg_match('!portfolio/cards/(\d+)!', $action)
114+
preg_match('!messageReplies/(\d+)!', $action)
152115
) {
153-
$source = $source->card;
116+
$source = current($source);
117+
} elseif (preg_match('!tasklists/(\d+)/time/total!', $action)) {
118+
$source = current($source);
119+
$source = $source['tasklist'];
120+
} elseif (preg_match('!tasks/(\d+)/time/total!', $action)) {
121+
$source = current($source);
122+
$source = $source['tasklist']['task'];
154123
}
124+
}
155125

156-
/*
157-
if (isset($source->project->files)) {
158-
$source = $source->project->files;
159-
} elseif (!empty($source->project->notebooks)) {
160-
$source = $source->project->notebooks;
161-
} elseif (!empty($source->project->links)) {
162-
$source = $source->project->links;
163-
} elseif (
164-
!empty($source->messageReplies)
165-
&& preg_match('!messageReplies/(\d+)!', $headers['X-Action'])
166-
) {
167-
$source = current($source->messageReplies);
168-
} elseif (
169-
!empty($source->people)
170-
&& preg_match('!projects/(\d+)/people/(\d+)!', $headers['X-Action'])
171-
) {
172-
$source = current($source->people);
173-
} elseif (
174-
!empty($source->project)
175-
&& preg_match('!projects/(\d+)/notebooks!', $headers['X-Action'])
176-
) {
177-
$source = [];
178-
} elseif (
179-
isset($source->cards)
180-
&& preg_match('!portfolio/columns/(\d+)/cards!', $headers['X-Action'])
181-
) {
182-
$source = $source->cards;
183-
} elseif (isset($source->$parent)) {
184-
$source = $source->$parent;
185-
} else {
186-
echo $parent;
187-
}
188-
if ($headers['X-Action'] === 'links' || $headers['X-Action'] === 'notebooks') {
189-
$_source = [];
190-
$wrapper = $headers['X-Action'];
191-
foreach ($source as $project) {
192-
foreach ($project->$wrapper as $object) {
193-
$_source[] = $object;
194-
}
126+
if ($key === 'project') {
127+
foreach(['files'] as $key) {
128+
if (isset($source[$key])) {
129+
$source = $source[$key];
130+
break;
195131
}
196-
$source = $_source;
197-
} elseif (str_contains($headers['X-Action'], 'time_entries') && $source !== null) {
198-
$source = [];
199132
}
200-
*/
201-
202-
$this->headers = $headers;
203-
$this->string = json_encode($source);
204-
if (is_arr_obj($source)) {
205-
/**
206-
* @var \stdClass
207-
*/
208-
$data = static::camelizeObject($source);
209-
if (!empty($data->id)) {
210-
$data->id = (int)$data->id;
133+
} elseif ($key === 'projects' && $action === 'files') {
134+
$data = [];
135+
foreach($source as $project) {
136+
foreach ($project['files'] as $file) {
137+
$data[] = $file;
211138
}
212-
/** @psalm-suppress InvalidPropertyAssignmentValue */
213-
$this->data = $data;
214139
}
140+
$source = $data;
141+
}
142+
} elseif (
143+
isset($source['card'])
144+
&& preg_match('!portfolio/cards/(\d+)!', $action)
145+
) {
146+
$source = $source['card'];
147+
}
215148

216-
return $this;
149+
$this->headers = $headers;
150+
$this->string = json_encode($source);
151+
if (is_arr_obj($source)) {
152+
/**
153+
* @var \stdClass
154+
*/
155+
$data = static::camelizeObject($source);
156+
if (!empty($data->id)) {
157+
$data->id = (int)$data->id;
158+
}
159+
/** @psalm-suppress InvalidPropertyAssignmentValue */
160+
$this->data = $data;
217161
}
218-
} elseif (!empty($source->MESSAGE)) {
219-
$errors = $source->MESSAGE;
220-
} elseif (!empty($source->error)) {
221-
$errors = $source->error;
222-
} else {
223-
$errors = null;
224-
}
225-
}
226162

227-
throw new Exception([
228-
'Message' => $errors,
229-
'Response' => $data,
230-
'Headers' => $headers,
231-
]);
163+
return $this;
164+
}
232165
}
233166

234167
/**
@@ -274,7 +207,7 @@ protected static function camelizeObject(mixed $source)
274207
* @var mixed $value
275208
*/
276209
foreach ($source as $key => $value) {
277-
if (ctype_upper($key)) {
210+
if (ctype_upper((string) $key)) {
278211
$key = strtolower($key);
279212
}
280213
$key = Str::camel($key);

tests/TestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function factory(string $className, Closure | array $callback = null)
6464
$path .= '?' . $request;
6565
}
6666
$isFile = $method === 'POST' && is_array($parameters) && isset($parameters['file']) && $parameters['file'] instanceof CURLFile;
67-
$params = $isFile ? $parameters : json_decode($request);
67+
$params = $isFile ? $parameters : json_decode($request ?? '');
6868
$getRequest = function () use ($params, $parent, $isFile) {
6969
if ($isFile) {
7070
return $params;

0 commit comments

Comments
 (0)