/** * Create a new Response based on a raw response message * * @param string $message Response message * * @return Response * @throws InvalidArgumentException if an empty $message is provided */ public static function fromMessage($message) { if (!$message) { throw new InvalidArgumentException('No response message provided'); } // Normalize line endings $message = preg_replace("/([^\r])(\n)\\b/", "\$1\r\n", $message); $protocol = $code = $status = ''; $parts = explode("\r\n\r\n", $message, 2); $headers = new Collection(); foreach (array_values(array_filter(explode("\r\n", $parts[0]))) as $i => $line) { // Remove newlines from headers $line = implode(' ', explode("\n", $line)); if ($i === 0) { // Check the status line list($protocol, $code, $status) = array_map('trim', explode(' ', $line, 3)); } else { if (strpos($line, ':')) { // Add a header list($key, $value) = array_map('trim', explode(':', $line, 2)); // Headers are case insensitive $headers->add($key, $value); } } } $body = null; if (isset($parts[1]) && $parts[1] != "\n") { $body = EntityBody::factory(trim($parts[1])); // Always set the appropriate Content-Length if Content-Legnth $headers['Content-Length'] = $body->getSize(); } $response = new static(trim($code), $headers, $body); $response->setProtocol($protocol)->setStatus($code, $status); return $response; }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { $activities = new Collection(); foreach ($command->getResponse()->json() as $key => $activity) { $activities->add($key, self::hydrateModelProperties(new self(), $activity)); } return $activities; }
/** * @covers Guzzle\Common\Collection::getAll */ public function testCanGetAllValuesByArray() { $this->coll->add('foo', 'bar'); $this->coll->add('tEsT', 'value'); $this->coll->add('tesTing', 'v2'); $this->coll->add('key', 'v3'); $this->assertNull($this->coll->get('test')); $this->assertEquals(array('foo' => 'bar', 'tEsT' => 'value', 'tesTing' => 'v2'), $this->coll->getAll(array('foo', 'tesTing', 'tEsT'))); }
public function __construct($apiKey, ParametersEscaper $parametersEscaper, $environment) { $this->apiKey = $apiKey; $this->parametersEscaper = $parametersEscaper; $this->environment = $environment; $headers = new Collection(); $headers->add('X-HTTP-Method-Override', 'GET'); $this->client = new Client(); $this->client->setDefaultHeaders($headers); }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { $data = $command->getResponse()->json(); $records = new Collection(); if (isset($data['records']) && is_array($data['records'])) { foreach ($data['records'] as $key => $object) { $records->add($key, self::hydrateModelProperties(new ReferencingApplicationFindResult(), $object, array('applicationUuid' => 'referencingApplicationUuId'))); } } $instance = self::hydrateModelProperties(new self(), $data, array(), array('records' => $records)); return $instance; }
/** * Create a Cookie by parsing a Cookie HTTP header * * @param string $cookieString Cookie HTTP header * * @return Cookie */ public static function factory($cookieString) { $data = new Collection(); if ($cookieString) { foreach (explode(';', $cookieString) as $kvp) { $parts = explode('=', $kvp, 2); $key = urldecode(trim($parts[0])); $value = isset($parts[1]) ? trim($parts[1]) : ''; $data->add($key, urldecode($value)); } } return new static($data->getAll()); }
public function getParamsToSign(RequestInterface $request, $timestamp, $nonce) { $params = new Collection(array('oauth_consumer_key' => $this->config['consumer_key'], 'oauth_nonce' => $nonce, 'oauth_signature_method' => $this->config['signature_method'], 'oauth_timestamp' => $timestamp, 'oauth_version' => $this->config['version'])); // Filter out oauth_token during temp token step, as in request_token. if ($this->config['token'] !== false) { $params->add('oauth_token', $this->config['token']); } // Add call back uri if (isset($this->config['callback_uri']) && !empty($this->config['callback_uri'])) { $params->add('oauth_callback', $this->config['callback_uri']); } // Add query string parameters $params->merge($request->getQuery()); // Add POST fields to signing string if (!$this->config->get('disable_post_params') && $request instanceof EntityEnclosingRequestInterface && false !== strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded')) { $params->merge($request->getPostFields()); } // Sort params $params = $params->getAll(); ksort($params); return $params; }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { $data = $command->getResponse()->json(); // Collection of notifications if (self::isResponseDataIndexedArray($data)) { $notifications = new Collection(); foreach ($data as $key => $documentData) { $notifications->add($key, self::hydrateModelProperties(new self(), $documentData, array('applicationId' => 'referencingApplicationUuId'))); } return $notifications; } else { return self::hydrateModelProperties(new self(), $data, array('applicationId' => 'referencingApplicationUuId')); } }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { $data = $command->getResponse()->json(); // Collection of notes if (self::isResponseDataIndexedArray($data)) { $notes = new Collection(); foreach ($data as $key => $noteData) { $notes->add($key, self::hydrateModelProperties(new self(), $noteData)); } return $notes; } else { return self::hydrateModelProperties(new self(), $data); } }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { $data = $command->getResponse()->json(); // Indexed array of agent users if (self::isResponseDataIndexedArray($data)) { $users = new Collection(); foreach ($data as $key => $userData) { $users->add($key, self::hydrateModelProperties(new self(), $userData, array('agentUserId' => 'agentUserUuId', 'fullName' => 'name'))); } return $users; } else { return self::hydrateModelProperties(new self(), $data, array('agentUserId' => 'agentUserUuId', 'fullName' => 'name')); } }
/** * @param Parameter[] $parameters * @param Collection $collection */ public function addParameters(array $parameters, Collection $collection) { foreach ($parameters as $parameter) { $value = ''; $localParams = $parameter->getLocalParams(); if (!empty($localParams)) { if (!isset($localParameterSerializer)) { $localParameterSerializer = new LocalParameterSerializer(); } $value = $localParameterSerializer->serialize($localParams); } $value .= $parameter->getValue(); $collection->add($parameter->getKey(), $value); } }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { // If there is no content then assume an empty collection if (204 == $command->getResponse()->getStatusCode()) { return new Collection(); } $data = $command->getResponse()->json(); // Collection of products if (self::isResponseDataIndexedArray($data)) { $products = new Collection(); foreach ($data as $key => $productData) { $products->add($key, self::hydrateModelProperties(new self(), $productData, array('productId' => 'id'))); } return $products; } else { return self::hydrateModelProperties(new self(), $data, array('productId' => 'id')); } }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { $data = $command->getResponse()->json(); // Indexed array of branches if (self::isResponseDataIndexedArray($data)) { $branches = new Collection(); foreach ($data as $key => $branchData) { $branches->add($key, self::hydrateModelProperties(new self(), $branchData, array('agentBranchId' => 'agentBranchUuId'))); } return $branches; } else { $address = self::hydrateModelProperties(new Address(), $data['address']); $correspondenceAddress = self::hydrateModelProperties(new Address(), $data['correspondenceAddress']); $brandAddress = self::hydrateModelProperties(new Address(), $data['agentBrand']['address']); $brand = self::hydrateModelProperties(new Brand(), $data['agentBrand'], array(), array('address' => $brandAddress)); return self::hydrateModelProperties(new self(), $data, array(), array('address' => $address, 'correspondenceAddress' => $correspondenceAddress, 'agentBrand' => $brand)); } }
/** * @deprecated Use $message->getHeader()->parseParams() * @codeCoverageIgnore */ public function getTokenizedHeader($header, $token = ';') { Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader()->parseParams()'); if ($this->hasHeader($header)) { $data = new Collection(); foreach ($this->getHeader($header)->parseParams() as $values) { foreach ($values as $key => $value) { if ($value === '') { $data->set($data->count(), $key); } else { $data->add($key, $value); } } } return $data; } }
/** * {@inheritdoc} */ public function parseMessage($message) { if (!$message) { return false; } $headers = new Collection(); $scheme = $host = $body = $method = $user = $pass = $query = $port = $version = $protocol = ''; $path = '/'; // Inspired by https://github.com/kriswallsmith/Buzz/blob/message-interfaces/lib/Buzz/Message/Parser/Parser.php#L16 $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE); for ($i = 0, $c = count($lines); $i < $c; $i += 2) { $line = $lines[$i]; // If two line breaks were encountered, then this is the body if (empty($line)) { $body = implode('', array_slice($lines, $i + 2)); break; } // Parse message headers if (!$method) { list($method, $path, $proto) = explode(' ', $line); $method = strtoupper($method); list($protocol, $version) = explode('/', strtoupper($proto)); $scheme = 'http'; } else { if (strpos($line, ':')) { list($key, $value) = explode(':', $line, 2); $key = trim($key); // Normalize standard HTTP headers if (in_array(strtolower($key), static::$requestHeaders)) { $key = str_replace(' ', '-', ucwords(str_replace('-', ' ', $key))); } // Headers are case insensitive $headers->add($key, trim($value)); } } } // Check for the Host header if (isset($headers['Host'])) { $host = $headers['Host']; } if (strpos($host, ':')) { list($host, $port) = array_map('trim', explode(':', $host)); if ($port == 443) { $scheme = 'https'; } } else { $port = ''; } // Check for basic authorization $auth = isset($headers['Authorization']) ? $headers['Authorization'] : ''; if ($auth) { list($type, $data) = explode(' ', $auth); if (strtolower($type) == 'basic') { $data = base64_decode($data); list($user, $pass) = explode(':', $data); } } // Check if a query is present $qpos = strpos($path, '?'); if ($qpos) { $query = substr($path, $qpos); $path = substr($path, 0, $qpos); } return array('method' => $method, 'protocol' => $protocol, 'protocol_version' => $version, 'parts' => array('scheme' => $scheme, 'host' => $host, 'port' => $port, 'user' => $user, 'pass' => $pass, 'path' => $path, 'query' => $query), 'headers' => $headers->getAll(), 'body' => $body); }
/** * {@inheritdoc} */ public function getCookies() { $cookieData = new Collection(); if ($cookies = $this->getHeader('Cookie')) { foreach ($cookies as $cookie) { $parts = explode('=', $cookie, 2); $cookieData->add($parts[0], isset($parts[1]) ? $parts[1] : ''); } } return $cookieData->getAll(); }
public function testImplementsCount() { $data = new Collection(); $this->assertEquals(0, $data->count()); $data->add('key', 'value'); $this->assertEquals(1, count($data)); $data->add('key', 'value2'); $this->assertEquals(1, count($data)); $data->add('key_2', 'value3'); $this->assertEquals(2, count($data)); }
/** * {@inheritdoc} */ public static function fromCommand(OperationCommand $command) { $data = $command->getResponse()->json(); if (self::isResponseDataIndexedArray($data)) { $applications = new Collection(); foreach ($data as $key => $applicationData) { $applications->add($key, self::hydrate($applicationData)); } return $applications; } else { return self::hydrate($data); } }