示例#1
0
 /**
  * Find all characters based on the given $criteria.
  *
  * @param Api\Client $client   The API Client.
  * @param array      $criteria The criteria to search with.
  *
  * @return Api\Collection
  */
 public static function findAll(Api\Client $client, array $criteria = [])
 {
     $filters = ['name' => [['string']], 'modifiedSince' => [['date', true], ['formatDate']], 'comics' => [['ofScalars', [['uint']]], ['implode', ',']], 'series' => [['ofScalars', [['uint']]], ['implode', ',']], 'events' => [['ofScalars', [['uint']]], ['implode', ',']], 'stories' => [['ofScalars', [['uint']]], ['implode', ',']], 'orderBy' => [['in', ['name', 'modified', '-name', '-modified']]]];
     list($success, $filteredCriteria, $error) = Api\Filterer::filter($filters, $criteria);
     Util::ensure(true, $success, $error);
     return new Api\Collection($client, 'characters', $filteredCriteria);
 }
示例#2
0
 /**
  * Create a new instance of Request.
  *
  * @param string $url     The url of the request.
  * @param string $method  The http method of the request.
  * @param array  $headers The headers of the request.
  * @param array  $body    The body of the request.
  *
  * @throws \InvalidArgumentException Thrown if $url is not a non-empty string.
  * @throws \InvalidArgumentException Thrown if $method is not a non-empty string.
  */
 public final function __construct($url, $method, array $headers = [], array $body = [])
 {
     Util::throwIfNotType(['string' => [$url, $method]], true);
     $this->url = $url;
     $this->method = $method;
     $this->headers = $headers;
     $this->body = $body;
 }
 /**
  * Create an array of new AbstractEntity based on the given $input arrays
  *
  * @param array[] $inputs The value to be filtered.
  *
  * @return AbstractEntity[]
  */
 public static final function fromArrays(array $inputs)
 {
     Util::throwIfNotType(['array' => $inputs]);
     $entities = [];
     foreach ($inputs as $key => $input) {
         $entities[$key] = new static($input);
     }
     return $entities;
 }
 /**
  * Create a new DataWrapper instance.
  *
  * @param array $input The data for the DataWrapper
  */
 public function __construct(array $input)
 {
     $filters = ['code' => [['int', true]], 'status' => [['string', true]], 'copyright' => [['string', true]], 'attributionText' => [['string', true]], 'attributionHTML' => [['string', true]], 'etag' => [['string', true]], 'data' => ['default' => [], ['array', 0]]];
     list($success, $filteredInput, $error) = Filterer::filter($filters, $input, ['allowUnknowns' => true]);
     Util::ensure(true, $success, $error);
     foreach ($filteredInput as $key => $value) {
         $this->{$key} = $value;
     }
     $this->data = new DataContainer($filteredInput['data']);
 }
 /**
  * Create a new DataContainer instance.
  *
  * @param array $input The data for the DataContainer
  */
 public function __construct(array $input)
 {
     $resourceFilter = self::deriveResourceFilter(Util\Arrays::get($input, 'results', []));
     $filters = ['offset' => ['default' => 0, ['int', true]], 'limit' => ['default' => 0, ['int', true]], 'total' => ['default' => 0, ['int', true]], 'count' => ['default' => 0, ['int', true]], 'results' => [[$resourceFilter]]];
     list($success, $filtered, $error) = Filterer::filter($filters, $input, ['allowUnknowns' => true]);
     Util::ensure(true, $success, $error);
     foreach ($filtered as $key => $value) {
         $this->{$key} = $value;
     }
 }
示例#6
0
 /**
  * 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);
 }
 /**
  * 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 ?: []);
 }
 /**
  * Execute a GET request against the Marvel API for a single resource.
  *
  * @param string  $resource The API resource to search for.
  * @param integer $id       The id of the API resource.
  *
  * @return ResponseInterface
  */
 public final function get($resource, $id)
 {
     Util::throwIfNotType(['string' => [$resource], 'int' => [$id]], true);
     $timestamp = time();
     $query = ['apikey' => $this->publicApiKey, 'ts' => $timestamp, 'hash' => md5($timestamp . $this->privateApiKey . $this->publicApiKey)];
     $url = self::BASE_URL . urlencode($resource) . "/{$id}?" . http_build_query($query);
     return $this->send(new Request($url, 'GET', ['Accept' => 'application/json']));
 }
示例#9
0
 /**
  * Return the current element, @see Iterator::current().
  *
  * @return mixed Returns the element in the results array or a custom type defined by $loader.
  */
 public final function current()
 {
     if ($this->position === -1) {
         $this->next();
     }
     Util::ensure(true, array_key_exists($this->position, $this->results), '\\OutOfBoundsException', ['Collection contains no element at current position']);
     if ($this->loader === null) {
         return $this->results[$this->position];
     }
     return call_user_func_array($this->loader, [$this->results[$this->position]]);
 }
示例#10
0
 /**
  * Parses the response into an array using the field definition.
  *
  * @param string $response The response from NetAcuity.
  * @param array $fields The expected fields in the response.
  *
  * @return array The response where the keys are from $fields and the values are from the $response.
  */
 private function _parseResponse($response, array $fields)
 {
     Util::throwIfNotType(['string' => $response], true);
     Util::throwIfNotType(['string' => $fields], true);
     $responseData = explode(';', $response);
     Util::ensure(count($fields), count($responseData));
     return array_combine($fields, $responseData);
 }