protected function getCommandWithArrayParamAndFilters()
 {
     $operation = new Operation(array('httpMethod' => 'POST', 'parameters' => array('foo' => new Parameter(array('type' => 'string', 'location' => 'query', 'sentAs' => 'Foo', 'required' => true, 'default' => 'bar', 'filters' => array('strtoupper'))), 'arr' => new Parameter(array('type' => 'array', 'location' => 'query', 'sentAs' => 'Arr', 'required' => true, 'default' => array(123, 456, 789), 'filters' => array(array('method' => 'implode', 'args' => array(',', '@value'))))))));
     $command = new OperationCommand(array(), $operation);
     $command->setClient(new MockClient());
     return $command;
 }
 public static function fromCommand(OperationCommand $command)
 {
     $response = json_decode($command->getResponse()->getBody(true), true);
     $class = get_called_class();
     // Cannot do new self() with abstract class
     return new $class($response);
 }
Exemplo n.º 3
0
 /**
  * The fromCommand method is used by Guzzle to create an instance of this class. It "injects"
  * the response data, which is then parsed to populate this and the associated models.
  *
  * @param Guzzle\Service\Command\OperationCommand
  * @return array
  */
 public static function fromCommand(\Guzzle\Service\Command\OperationCommand $command)
 {
     // Grab the results, which will be in JSON format
     //krumo($command->getResponse());
     $result = $command->getResponse()->json();
     // Start building an array, since a call to the API can include several different sites
     $websites = array();
     // Now iterate through the results
     foreach ($result as $key => $row) {
         // "Target" is a stripped-down version of the URL provided
         $url = $row['target'];
         // Create a new Website model
         $website = new Website($url);
         // The 0 axis holds information about trustworthiness
         if (isset($row[0])) {
             $website->setTrustworthiness($row[0][0], $row[0][1]);
         }
         // The 4 axis contins information pertaining to child satefy
         if (isset($row[4])) {
             $website->setChildSafety($row[4][0], $row[4][1]);
         }
         // NOTE: the axes in indexes 1 and 2 are deprecated.
         // If any categories are specified, set them
         if (isset($row['categories'])) {
             $website->setCategories($row['categories']);
         }
         // If provided, set the blacklist information; we'll discard the timestamp information
         if (isset($row['blacklists'])) {
             $website->setBlacklists(array_keys($row['blacklists']));
         }
         // Add the website
         $websites[$key] = $website;
     }
     return $websites;
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public static function fromCommand(OperationCommand $command)
 {
     $data = $command->getResponse()->json();
     $brandAddress = self::hydrateModelProperties(new Address(), $data['agentBrand']['address']);
     $brand = self::hydrateModelProperties(new Brand(), $data['agentBrand'], array(), array('address' => $brandAddress));
     $address = self::hydrateModelProperties(new Address(), $data['address']);
     return self::hydrateModelProperties(new self(), $data, array(), array('address' => $address, 'agentBrand' => $brand));
 }
 public function testAlreadyOverriddenUserAgent()
 {
     $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14';
     $command = new OperationCommand();
     $command->set('ua', 'some_value');
     $this->dispatchCommand($command);
     $this->assertSame('some_value', $command->get('ua'));
 }
Exemplo n.º 6
0
 /**
  * {@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;
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public static function fromCommand(OperationCommand $command)
 {
     $items = new Collection();
     foreach ($command->getResponse()->json() as $key => $item) {
         $items[$key] = self::hydrateModelProperties(new self(), $item, array('address1' => 'addressLineOne', 'address2' => 'addressLineTwo', 'address3' => 'addressLineThree', 'address4' => 'addressLineFour', 'address5' => 'addressLineFive'));
     }
     return $items;
 }
 public function testAlreadyOverriddenUserAgent()
 {
     $_SERVER['REMOTE_ADDR'] = '1.2.3.4';
     $command = new OperationCommand();
     $command->set('uip', '4.3.2.1');
     $this->dispatchCommand($command);
     $this->assertSame('4.3.2.1', $command->get('uip'));
 }
Exemplo n.º 9
0
 public static function fromCommand(OperationCommand $command)
 {
     $xml = $command->getResponse()->xml();
     if (!isset($xml->AuthInfo)) {
         throw new AuthenticationException((string) $xml, $xml->attributes()['errorcode']);
     }
     $authToken = (string) $xml->AuthInfo->attributes()['authToken'];
     return $authToken;
 }
 public function testAddsContentTypeWhenExpectsIsSetOnCommand()
 {
     $op = new OperationCommand(array(), new Operation());
     $op['command.expects'] = 'application/json';
     $op->setClient(new Client());
     $request = $op->prepare();
     $request->setResponse(new Response(200, null, '{"Baz":"Bar"}'), true);
     $this->assertEquals(array('Baz' => 'Bar'), $op->execute());
 }
 /**
  * Create a response model object from a completed command.
  *
  * @param OperationCommand $command That serialized the request
  * @return \Illuminate\Support\Collection
  */
 public static function fromCommand(OperationCommand $command)
 {
     // Initialize the collection
     $collection = new self($command->getResponse()->json());
     // Set the Usergrid API client on the collection
     $collection->setApiClient($command->getClient()->getApiClient());
     // Return the collection
     return $collection;
 }
Exemplo n.º 12
0
 public static function fromCommand(OperationCommand $command)
 {
     $response = $command->getResponse();
     $info = $response->getInfo();
     if ($info['content_type'] === 'image/png' || $info['content_type'] === 'image/jpg') {
         return new static(['logo' => $info['url']]);
     }
     throw new BadResponseException('Content type invalid. API returned ' . $info['content_type']);
 }
Exemplo n.º 13
0
 /**
  * @covers Desk\Command\PreValidator::castPrimitivesToArrays
  * @dataProvider dataCastPrimitivesToArrays
  *
  * @param array $param    Parameter description/structure
  * @param mixed $value    Input value for the parameter
  * @param mixed $expected Expected output value
  */
 public function testCastPrimitivesToArrays(array $param, $value, $expected)
 {
     $command = new OperationCommand(array('foo' => $value), new Operation(array('parameters' => array('foo' => $param))));
     $command->setClient(new Client());
     $event = new Event(array('command' => $command));
     $preValidator = $this->mock('castPrimitivesToArrays');
     $preValidator->castPrimitivesToArrays($event);
     $this->assertSame($expected, $command->get('foo'));
 }
Exemplo n.º 14
0
 public function testAllowsRawResponses()
 {
     $description = new ServiceDescription(array('operations' => array('foo' => array('responseClass' => 'bar', 'responseType' => 'model')), 'models' => array('bar' => array())));
     $op = new OperationCommand(array(OperationCommand::RESPONSE_PROCESSING => OperationCommand::TYPE_RAW), $description->getOperation('foo'));
     $op->setClient(new Client());
     $request = $op->prepare();
     $response = new Response(200, array('Content-Type' => 'application/xml'), '<Foo><Baz>Bar</Baz></Foo>');
     $request->setResponse($response, true);
     $this->assertSame($response, $op->execute());
 }
 public function testSkipsUnkownModels()
 {
     $parser = new OperationResponseParser();
     $operation = $this->getDescription()->getOperation('test');
     $operation->setResponseClass('Baz')->setResponseType('model');
     $op = new OperationCommand(array(), $operation);
     $op->setResponseParser($parser)->setClient(new Client());
     $op->prepare()->setResponse(new Response(201), true);
     $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $op->execute());
 }
Exemplo n.º 16
0
 /**
  * Create a response model object from a completed command
  *
  * @param OperationCommand $command That serialized the request
  *
  * @return CurrencyCollection
  */
 public static function fromCommand(OperationCommand $command)
 {
     $response = $command->getResponse();
     $json = $response->json();
     $currencies = array();
     foreach ($json as $currency) {
         $currencies[] = new Currency($currency['code'], $currency['name'], $currency['rate']);
     }
     return new self($currencies);
 }
Exemplo n.º 17
0
 public static function fromCommand(OperationCommand $command)
 {
     $xml = $command->getResponse()->xml();
     $styles = $xml->StyleList->style;
     $output = array();
     foreach ($styles as $style) {
         $output[(int) $style->attributes()['guid']] = trim((string) $style);
     }
     return $output;
 }
Exemplo n.º 18
0
 public static function fromCommand(OperationCommand $command)
 {
     $xml = $command->getResponse()->xml();
     $makes = $xml->MakeList->make;
     $output = array();
     foreach ($makes as $make) {
         $output[] = trim((string) $make);
     }
     return $output;
 }
Exemplo n.º 19
0
 public static function fromCommand(OperationCommand $command)
 {
     $xml = $command->getResponse()->xml();
     $years = $xml->YearList->year;
     $output = array();
     foreach ($years as $year) {
         $output[] = (int) $year;
     }
     return $output;
 }
 public function testForceSet()
 {
     $plugin = new DataSetter(array('test_key' => 'test_value'), true);
     $command = new OperationCommand();
     $command->set('test_key', 'already_stated_value');
     $event = new Event(array('command' => $command));
     $plugin->register($this->client);
     $this->client->getEventDispatcher()->dispatch('command.before_prepare', $event);
     $this->assertSame('test_value', $command->get('test_key'));
 }
Exemplo n.º 21
0
 public static function fromCommand(OperationCommand $command)
 {
     $json = $command->getResponse()->json();
     // support collections
     if (array_key_exists(0, $json)) {
         return array_map(function (array $properties) {
             return new static($properties);
         }, $json);
     }
     return new static($json);
 }
Exemplo n.º 22
0
 /**
  * {@inheritdoc}
  */
 public static function fromCommand(OperationCommand $command)
 {
     $instance = new self();
     $response = $command->getResponse();
     $body = $response->getBody();
     if ($body instanceof Stream) {
         $contentDispositionParts = self::parseContentDisposition($response->getContentDisposition());
         $instance->setStream($body)->setSize($response->getBody()->getSize())->setMimeType($response->getContentType())->setFileExtension($contentDispositionParts['extension'])->setFileName($contentDispositionParts['filename'])->setData($response->getBody(true));
     }
     return $instance;
 }
Exemplo n.º 23
0
 /**
  * Create a response model object from a completed command
  *
  * @param OperationCommand $command That serialized the request
  *
  * @return self
  */
 public static function fromCommand(OperationCommand $command)
 {
     $body = (string) $command->getResponse()->getBody();
     $lines = str_getcsv($body, "\n");
     $metadata = array();
     foreach ($lines as $line) {
         list($key, $value) = str_getcsv($line);
         $metadata[$key] = $value;
     }
     return new self($metadata);
 }
 /**
  * FromCommand
  *
  * @return ResponseSearchModel
  * @author James Pudney
  */
 public static function fromCommand(OperationCommand $command)
 {
     $response = $command->getResponse();
     $json = $response->json();
     $columns = self::getColumnNames($json['response']['metaData']['columns']);
     $results = $json['response']['results'];
     // reformat the results
     $results = self::formatSearchResults($results, $columns);
     // return the model
     return new self($results, $columns);
 }
Exemplo n.º 25
0
 /**
  * Constructor
  *
  * @param OperationCommand $command
  */
 public function __construct(OperationCommand $command)
 {
     $this->setClient($command->getClient());
     $this->setCommandName($command->getName());
     $data = $command->getResponse()->json();
     foreach ($data as $key => $value) {
         $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
         if (method_exists($this, $setter)) {
             $this->{$setter}($value);
         }
     }
 }
 /**
  * {@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 response model object from a completed command
  *
  * @param OperationCommand $command That serialized the request
  *
  * @return self
  */
 public static function fromCommand(OperationCommand $command)
 {
     $lines = explode("\n", $command->getResponse()->getBody(true));
     $head = str_getcsv(array_shift($lines), ';');
     foreach ($head as $key => $headItem) {
         $head[$key] = self::strtocamelcase($headItem);
     }
     $array = array();
     foreach ($lines as $line) {
         $array[] = array_combine($head, str_getcsv($line, ';'));
     }
     return $array;
 }
Exemplo n.º 28
0
 public static function fromCommand(OperationCommand $command)
 {
     $response = $command->getResponse()->json();
     // If 'ok' is not set or not true, something went wrong
     if (!isset($response['response']) || empty($response['ok']) || $response['ok'] != 1) {
         throw new \Exception('response not ok');
     }
     // If response is not an array return true if 1, otherwise false
     if (!is_array($response['response'])) {
         return $response['response'] == 1 ? true : false;
     }
     return $response['response'];
 }
Exemplo n.º 29
0
 public static function fromCommand(OperationCommand $command)
 {
     $response = $command->getResponse()->json();
     if (self::isListing($response)) {
         $things = array();
         foreach ($response['data']['children'] as $input) {
             $things[] = self::thingFactory()->createThing($input);
         }
         return $things;
     } else {
         $thing = self::thingFactory()->createThing($response);
         return $thing;
     }
 }
Exemplo n.º 30
0
 /**
  * {@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'));
     }
 }