, last_error: string} The rows with an * empty `last_error` when the read is confirmed; otherwise no rows and the * reason the read was refused. */ public static function confirm($result, string $operation): array { if (!is_array($result)) { return self::refused($operation . ' returned an invalid result envelope.'); } if (array_key_exists('last_error', $result) && !is_string($result['last_error'])) { return self::refused($operation . ' returned malformed error metadata.'); } $lastError = array_key_exists('last_error', $result) ? $result['last_error'] : ''; if ($lastError !== '') { return self::refused($lastError); } if (array_key_exists('timed_out', $result) && !is_bool($result['timed_out'])) { return self::refused($operation . ' returned malformed timeout metadata.'); } if (($result['timed_out'] ?? false) === true) { return self::refused($operation . ' timed out.'); } if (!array_key_exists('query_succeeded', $result) || !is_bool($result['query_succeeded'])) { return self::refused($operation . ' returned unknown query status.'); } if ($result['query_succeeded'] !== true) { return self::refused($operation . ' failed without a database error.'); } if (!array_key_exists('rows', $result) || !is_array($result['rows'])) { return self::refused($operation . ' returned malformed rows.'); } foreach ($result['rows'] as $row) { if (!is_array($row)) { return self::refused($operation . ' returned malformed row data.'); } } return array('rows' => $result['rows'], 'last_error' => ''); } /** * @param string $reason * @return array{rows: array, last_error: string} */ private static function refused(string $reason): array { return array('rows' => array(), 'last_error' => $reason); } }