public function testOwnsStructure() { $param = new Parameter(array('type' => 'object')); $model = new Model(array('foo' => 'bar'), $param); $this->assertSame($param, $model->getStructure()); $this->assertEquals('bar', $model->get('foo')); $this->assertEquals('bar', $model['foo']); }
public function make(Response $response) { $upload = new static(); $data = $response->toArray(); $data['uploaded_at'] = new DateTime(); $upload->setData($data); return $upload; }
/** * {@inheritdoc} */ protected function determineNextToken(Model $result) { $this->nextToken = null; if ($result->get($this->get('more_key'))) { $jobs = $result->get($this->get('result_key')) ?: array(); $numJobs = count($jobs); $this->nextToken = $numJobs ? $jobs[$numJobs - 1]['JobId'] : null; } }
/** * {@inheritdoc} */ protected function handleResults(Model $result) { // Get the list of uploads $uploads = $result->get('Uploads') ?: array(); // If there are prefixes and we want them, merge them in if ($this->get('return_prefixes') && $result->hasKey('CommonPrefixes')) { $uploads = array_merge($uploads, $result->get('CommonPrefixes')); } return $uploads; }
/** * {@inheritdoc} */ protected function handleResults(Model $result) { $items = array(); if ($responses = $result->get('Responses')) { foreach ($responses as $table) { foreach ($table['Items'] as $item) { $items[] = $item; } } } return $items; }
/** * {@inheritdoc} */ protected function handleResults(Model $result) { // Get the results $buckets = $result->get('Buckets') ?: array(); // If only the names_only set, change arrays to a string if ($this->get('names_only')) { foreach ($buckets as &$bucket) { $bucket = $bucket['Name']; } } return $buckets; }
/** * {@inheritdoc} */ protected function handleResults(Model $result) { // Get the list of object versions $versions = $result->get('Versions') ?: array(); $deleteMarkers = $result->get('DeleteMarkers') ?: array(); $versions = array_merge($versions, $deleteMarkers); // If there are prefixes and we want them, merge them in if ($this->get('return_prefixes') && $result->hasKey('CommonPrefixes')) { $versions = array_merge($versions, $result->get('CommonPrefixes')); } return $versions; }
/** * {@inheritdoc} */ protected function handleResults(Model $result) { $instances = array(); // Invert the structure so that instances are yielded and the reservation is exposed as a property foreach ($result->get('Reservations') as $reservation) { foreach ($reservation['Instances'] as $instance) { $instance['Reservation'] = $reservation; unset($instance['Reservation']['Instances']); $instances[] = $instance; } } return $instances; }
/** * get the public hostname or IP of the instance * @throws Exception - if no public hostname or IP can be found * @return string - hostname or IP */ public function getHost() { $host = $this->description['PublicDnsName']; if (!$host) { $data = new Model($this->description); $publicIps = $data->getPath('NetworkInterfaces/*/PrivateIpAddresses/*/Association/PublicIp'); if (count($publicIps) > 0) { $host = $publicIps[0]; } } if (!$host) { throw new \Exception(sprintf('No public hostname or IP can be found for this instance: %s', $this->id)); } return $host; }
/** * Hydrate common values from an Guzzle model to an event * * @param WorkflowEvent $event * @param Model $model */ protected function hydrateWorkflowEvent(WorkflowEvent $event, Model $model) { $event->setEventId($model->get('startedEventId')); $event->setExecutionId($model->get('workflowExecution')['workflowId']); $event->setRunId($model->get('workflowExecution')['runId']); $event->setToken($model->get('taskToken')); $event->setWorkflowName($model->get('workflowType')['name']); $event->setWorkflowVersion($model->get('workflowType')['version']); }
/** * {@inheritdod} * * Overridden to provide special treatment to relationship data */ public function __construct(array $data = array(), Parameter $structure = null) { if (isset($data['_links'])) { $this->links = $data['_links']; unset($data['_links']); } if (isset($data['_embedded'])) { $this->embedded = $data['_embedded']; unset($data['_embedded']); } parent::__construct($data, $structure); }
/** * {@inheritdoc} */ protected function handleResults(Model $result) { // Get the list of objects and record the last key $objects = $result->get('Contents') ?: array(); $numObjects = count($objects); $lastKey = $numObjects ? $objects[$numObjects - 1]['Key'] : false; if ($lastKey && !$result->hasKey($this->get('token_key'))) { $result->set($this->get('token_key'), $lastKey); } // Closure for getting the name of an object or prefix $getName = function ($object) { return isset($object['Key']) ? $object['Key'] : $object['Prefix']; }; // If common prefixes returned (i.e. a delimiter was set) and they need to be returned, there is more to do if ($this->get('return_prefixes') && $result->hasKey('CommonPrefixes')) { // Collect and format the prefixes to include with the objects $objects = array_merge($objects, $result->get('CommonPrefixes')); // Sort the objects and prefixes to maintain alphabetical order, but only if some of each were returned if ($this->get('sort_results') && $lastKey && $objects) { usort($objects, function ($object1, $object2) use($getName) { return strcmp($getName($object1), $getName($object2)); }); } } // If only the names are desired, iterate through the results and convert the arrays to the object/prefix names if ($this->get('names_only')) { $objects = array_map($getName, $objects); } return $objects; }
public function testReceiveMessageWaitTimeSecondsOption() { $options = ['ReceiveMessageWaitTimeSeconds' => 20]; $adapter = new SqsAdapter($this->client, 'foo', $options); $url = $this->stubCreateQueue('foo', $options); $timeout = $this->stubQueueVisibilityTimeout($url); $this->stubCreateDequeueMessage('foo', 0, 'a'); $this->stubCreateDequeueMessage('bar', 1, 'b'); $this->stubCreateDequeueMessage('baz', 2, 'c'); $this->model->shouldReceive('get')->once()->with('Messages')->andReturn([['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'], ['Body' => 'bar', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 1, 'ReceiptHandle' => 'b'], ['Body' => 'baz', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 2, 'ReceiptHandle' => 'c']]); $this->client->shouldReceive('receiveMessage')->once()->with(['QueueUrl' => $url, 'AttributeNames' => ['All'], 'MaxNumberOfMessages' => 3, 'VisibilityTimeout' => $timeout, 'WaitTimeSeconds' => 20])->andReturn($this->model); $iterator = $adapter->dequeue($this->factory, 3); assertThat($iterator, is(anInstanceOf('Generator'))); assertThat(iterator_to_array($iterator), is(equalTo($this->messages))); }
/** * {@inheritdoc} */ protected function determineNextToken(Model $result) { $this->nextToken = null; // If the value of "more key" is true or there is no "more key" to check, then try to get the next token $moreKey = $this->get('more_key'); if ($moreKey === null || $result->getPath($moreKey)) { // Get the token key to check if ($tokenKey = $this->get('token_key')) { // Get the next token's value. Works with multi-value tokens $getToken = function ($key) use($result) { return $result->getPath((string) $key); }; $this->nextToken = is_array($tokenKey) ? array_map($getToken, $tokenKey) : $getToken($tokenKey); } } }
/** * @param \Guzzle\Service\Resource\Model $resources * @return mixed */ private function getHostFromDescribedInstances($resources) { $instances = $resources->getPath('Reservations/*/Instances'); if (empty($instances)) { return; } $instanceToUse = null; foreach ($instances as $index => $instance) { if (!empty($instance['Tags'])) { foreach ($instance['Tags'] as $tag) { if (!empty($this->testSuite) && $tag['Key'] === 'TestSuite' && $tag['Value'] === $this->testSuite) { $instanceToUse = $instance; } } } } if (empty($instanceToUse)) { $instanceToUse = array_shift($instances); } $host = $instanceToUse['PublicDnsName']; return $host; }
/** * Check to see if the path of the output key is satisfied by the value * * @param Model $model Result model * @param string $key Key to check * @param string $checkValue Compare the key to the value * @param bool $all Set to true to ensure all value match or false to only match one * * @return bool */ protected function checkPath(Model $model, $key = null, $checkValue = array(), $all = true) { // If no key is set, then just assume true because the request succeeded if (!$key) { return true; } if (!($result = $model->getPath($key))) { return false; } $total = $matches = 0; foreach ((array) $result as $value) { $total++; foreach ((array) $checkValue as $check) { if ($value == $check) { $matches++; break; } } } // When matching all values, ensure that the match count matches the total count if ($all && $total != $matches) { return false; } return $matches > 0; }
/** * Add history from a Guzzle object to the workflow * * @param DecisionEvent $event * @param Model $model */ private function addHistory(DecisionEvent $event, Model $model) { $history = $event->getHistory(); $items = $model->get('events'); foreach ($items as $item) { $this->parseHistoryItem($history, $item); } }
/** * Creates a credentials object from the credential data return by an STS operation * * @param Model $result The result of an STS operation * * @return Credentials * @throws InvalidArgumentException if the result does not contain credential data */ public function createCredentials(Model $result) { if (!$result->hasKey('Credentials')) { throw new InvalidArgumentException('The modeled result provided contained no credentials.'); } return new Credentials($result->getPath('Credentials/AccessKeyId'), $result->getPath('Credentials/SecretAccessKey'), $result->getPath('Credentials/SessionToken'), $result->getPath('Credentials/Expiration')); }
/** * Extracts the value from the result using Collection::getPath. Also adds some additional logic for keys that need * to access n-1 indexes (e.g., ImportExport, Kinesis). The n-1 logic only works for the known cases. We will switch * to a jmespath implementation in the future to cover all cases * * @param Model $result * @param string $key * * @return mixed|null */ protected function getValueFromResult(Model $result, $key) { // Special handling for keys that need to access n-1 indexes if (strpos($key, '#') !== false) { $keyParts = explode('#', $key, 2); $items = $result->getPath(trim($keyParts[0], '/')); if ($items && is_array($items)) { $index = count($items) - 1; $key = strtr($key, array('#' => $index)); } } // Get the value return $result->getPath($key); }
/** * {@inheritdoc} */ protected function determineNextToken(Model $result) { $this->nextToken = $result->get('Marker'); }
public function fromModel(Model $model) { if ($model->hasKey('domainInfo')) { $domainInfo = $model->get('domainInfo'); $this->fromArray($domainInfo); if (array_key_exists('name', $domainInfo)) { $this->setName($domainInfo['name']); } if (array_key_exists('status', $domainInfo)) { $this->setStatus($domainInfo['status']); } if (array_key_exists('description', $domainInfo)) { $this->setDescription($domainInfo['description']); } } if ($model->hasKey('configuration')) { $configuration = $model->get('configuration'); if (array_key_exists('workflowExecutionRetentionPeriodInDays', $configuration)) { $this->setWorkflowExecutionRetentionPeriodInDays($configuration['workflowExecutionRetentionPeriodInDays']); } } }
public function testFalseyKeysStillDescend() { $model = new Model(array('0' => array('a' => 'jar'), 1 => 'other')); $this->assertEquals('jar', $model->getPath('0/a')); $this->assertEquals('other', $model->getPath('1')); }
/** * {@inheritdoc} */ protected function handleResults(Model $result) { $this->scannedCount += (int) $result->get('ScannedCount'); return parent::handleResults($result); }