/** * Retrieve the cached results of the api $request. * * @param RequestInterface $request A request for which the response may be cached. * * @return ResponseInterface|null */ public function get(RequestInterface $request) { $id = $request->getUrl(); $cache = Arrays::get($this->cache, $id); if ($cache === null) { return null; } if ($cache['expires'] >= time()) { return $cache['response']; } unset($this->cache[$id]); return null; }
/** * Returns a collection containing all Comics which match the given criteria. * * @param Api\Client $client The API Client. * @param array $criteria The criteria for searching. * * @return Api\Collection */ public static final function findAll(Api\Client $client, array $criteria = []) { $filters = ['format' => [['in', ['comic', 'hardcover', 'trade paperback', 'magazine', 'digest', 'graphic novel', 'digital comic', 'infinite comic']]], 'formatType' => [['in', ['comic', 'collection']]], 'noVariants' => [['bool'], ['boolToString']], 'dateDescriptor' => [['in', ['lastWeek', 'thisWeek', 'nextWeek', 'thisMonth']]], 'fromDate' => [['date', true]], 'toDate' => [['date', true]], 'hasDigitalIssue' => [['bool'], ['boolToString']], 'modifiedSince' => [['date', true], ['formatDate']], 'creators' => [['ofScalars', [['uint']]], ['implode', ',']], 'characters' => [['ofScalars', [['uint']]], ['implode', ',']], 'series' => [['ofScalars', [['uint']]], ['implode', ',']], 'events' => [['ofScalars', [['uint']]], ['implode', ',']], 'stories' => [['ofScalars', [['uint']]], ['implode', ',']], 'sharedAppearances' => [['ofScalars', [['uint']]], ['implode', ',']], 'collaborators' => [['ofScalars', [['uint']]], ['implode', ',']], 'orderBy' => [['in', ['focDate', 'onsaleDate', 'title', 'issueNumber', 'modified', '-focDate', '-onsaleDate', '-title', '-issueNumber', '-modified']]]]; list($success, $filteredCriteria, $error) = Api\Filterer::filter($filters, $criteria); Util::ensure(true, $success, $error); $toDate = Util\Arrays::get($filteredCriteria, 'toDate'); $fromDate = Util\Arrays::get($filteredCriteria, 'fromDate'); if ($toDate !== null && $fromDate !== null) { unset($filteredCriteria['toDate'], $filteredCriteria['fromDate']); $filteredCriteria['dateRange'] = "{$fromDate->format('c')},{$toDate->format('c')}"; } return new Api\Collection($client, self::API_RESOURCE, $filteredCriteria); }
/** * Helper method to derive the filter to use for the given resource array * * @param mixed $results the results array from the API * * @return callable The filter to use */ private static function deriveResourceFilter($results) { $default = function () { return []; }; if (!is_array($results) || !isset($results[0]['resourceURI'])) { return $default; } $pattern = '^' . preg_quote(Client::BASE_URL) . '(?P<resource>[a-z]*)/\\d+$'; $matches = []; preg_match("#{$pattern}#", $results[0]['resourceURI'], $matches); return Util\Arrays::get($matches, 'resource', $default); }
/** * Execute the specified request against the Marvel API. * * @param RequestInterface $request The request to send. * * @return ResponseInterface * * @throws \Exception Throws on error. */ public function send(RequestInterface $request) { $curlHeaders = ['Expect:']; //stops curl automatically putting in expect 100. foreach ($request->getHeaders() as $key => $value) { $curlHeaders[] = "{$key}: {$value}"; } $curlOptions = [CURLOPT_URL => $request->getUrl(), CURLOPT_RETURNTRANSFER => true, CURLOPT_VERBOSE => false, CURLOPT_HEADER => true, CURLOPT_FORBID_REUSE => true, CURLOPT_HTTPHEADER => $curlHeaders, CURLOPT_ENCODING => 'gzip,deflate']; if (strtoupper($request->getMethod()) !== 'GET') { throw new \Exception("Unsupported method '{$request->getMethod()}' given"); } $curl = Util::ensureNot(false, curl_init(), 'Unable to initialize connection'); Util::ensureNot(false, curl_setopt_array($curl, $curlOptions), 'Unable to prepare connection'); $result = Util::ensureNot(false, curl_exec($curl), curl_error($curl)); $headerSize = Util::ensureNot(false, curl_getinfo($curl, CURLINFO_HEADER_SIZE), 'Unable to determine header size'); $httpCode = Util::ensureNot(false, curl_getinfo($curl, CURLINFO_HTTP_CODE), 'Unable to determine response HTTP code'); $headers = Http::parseHeaders(substr($result, 0, $headerSize - 1)); $body = json_decode(substr($result, $headerSize), true); $error = Arrays::get([JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', JSON_ERROR_STATE_MISMATCH => ' Invalid or malformed JSON', JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', JSON_ERROR_SYNTAX => 'Syntax error', JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'], json_last_error()); Util::ensure(null, $error, "Unable to parse response: {$error}"); return new Response($httpCode, $headers, $body ?: []); }