Beispiel #1
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 collection.
  *
  * @param Client   $client   A client connection to the API.
  * @param string   $resource The name of API resource to request.
  * @param array    $filters  A key value pair array of search filters.
  * @param callable $loader   A custom callable to use when iterating over the collection.
  */
 public final function __construct(Client $client, $resource, array $filters = [], callable $loader = null)
 {
     Util::throwIfNotType(['string' => [$resource]], true);
     $this->client = $client;
     $this->resource = $resource;
     $this->filters = $filters;
     $this->loader = $loader;
     $this->rewind();
 }
 /**
  * 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']));
 }
 /**
  * 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);
 }