validate(['api_key' => ['required', 'string']]); $response = Http::withHeaders([ 'X-Api-Key' => $request->string('api_key')->toString(), ])->get('https://www.mailwizz.nl/api/lists'); if ($response->failed()) { return response()->json(['message' => __('Invalid API key or connection failed.')], 422); } $json = $response->json(); if (is_array($json) && ($json['status'] ?? null) === 'error') { return response()->json(['message' => __('Invalid API key or connection failed.')], 422); } return response()->json([ 'lists' => $this->normalizeListsPayload(is_array($json) ? $json : []), ]); } public function fields(Request $request): JsonResponse { $request->validate([ 'api_key' => ['required', 'string'], 'list_uid' => ['required', 'string'], ]); $listUid = $request->string('list_uid')->toString(); $response = Http::withHeaders([ 'X-Api-Key' => $request->string('api_key')->toString(), ])->get("https://www.mailwizz.nl/api/lists/{$listUid}/fields"); if ($response->failed()) { return response()->json(['message' => __('Failed to fetch list fields.')], 422); } $json = $response->json(); if (is_array($json) && ($json['status'] ?? null) === 'error') { return response()->json(['message' => __('Failed to fetch list fields.')], 422); } return response()->json([ 'fields' => $this->normalizeFieldsPayload(is_array($json) ? $json : []), ]); } /** * @return list */ private function normalizeListsPayload(array $json): array { $out = []; $records = data_get($json, 'data.records'); if (! is_array($records)) { return $out; } foreach ($records as $row) { if (! is_array($row)) { continue; } $uid = data_get($row, 'general.list_uid') ?? data_get($row, 'list_uid'); $name = data_get($row, 'general.name') ?? data_get($row, 'name'); if (is_string($uid) && $uid !== '' && is_string($name) && $name !== '') { $out[] = ['list_uid' => $uid, 'name' => $name]; } } return $out; } /** * @return list}> */ private function normalizeFieldsPayload(array $json): array { $out = []; $records = data_get($json, 'data.records'); if (! is_array($records)) { return $out; } foreach ($records as $row) { if (! is_array($row)) { continue; } $tag = data_get($row, 'tag'); $label = data_get($row, 'label'); $typeId = data_get($row, 'type.identifier'); if (! is_string($tag) || $tag === '' || ! is_string($label)) { continue; } $typeIdentifier = is_string($typeId) ? $typeId : ''; $rawOptions = data_get($row, 'options'); $options = $this->normalizeFieldOptions($rawOptions); $out[] = [ 'tag' => $tag, 'label' => $label, 'type_identifier' => $typeIdentifier, 'options' => $options, ]; } return $out; } /** * @return array */ private function normalizeFieldOptions(mixed $rawOptions): array { if ($rawOptions === null || $rawOptions === '') { return []; } if (is_string($rawOptions)) { $decoded = json_decode($rawOptions, true); if (is_array($decoded)) { $rawOptions = $decoded; } else { return []; } } if (! is_array($rawOptions)) { return []; } $out = []; foreach ($rawOptions as $key => $value) { if (is_string($key) || is_int($key)) { $k = (string) $key; $out[$k] = is_scalar($value) ? (string) $value : ''; } } return $out; } }