Example #1
1
public function visit(
CommandInterface $command,
Response $response,
Parameter $param,
&$value,
$context = null
) {
$value[$param->getName()] = $response->getStatusCode();
}
 /**
  * Recursively process a parameter while applying filters
  *
  * @param Parameter $param API parameter being validated
  * @param mixed     $value Value to validate and process. The value may change during this process.
  */
 protected function recursiveProcess(Parameter $param, &$value)
 {
     if ($value === null) {
         return;
     }
     if (is_array($value)) {
         $type = $param->getType();
         if ($type == 'array') {
             foreach ($value as &$item) {
                 $this->recursiveProcess($param->getItems(), $item);
             }
         } elseif ($type == 'object' && !isset($value[0])) {
             // On the above line, we ensure that the array is associative and not numerically indexed
             if ($properties = $param->getProperties()) {
                 foreach ($properties as $property) {
                     $name = $property->getName();
                     $key = $property->getWireName();
                     if (isset($value[$key])) {
                         $this->recursiveProcess($property, $value[$key]);
                         if ($key != $name) {
                             $value[$name] = $value[$key];
                             unset($value[$key]);
                         }
                     }
                 }
             }
         }
     }
     $value = $param->filter($value);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
 {
     if ($value instanceof PostFileInterface) {
         $request->addPostFile($value);
     } else {
         $request->addPostFile($param->getWireName(), $value);
     }
 }
Example #4
0
public function visit(
CommandInterface $command,
Response $response,
Parameter $param,
&$value,
$context = null
) {
$value[$param->getName()] = $param->filter($response->getBody());
}
Example #5
0
 /**
  * Validates an array of tags or aliases (just strings)
  *
  * @param array     $aliases
  * @param Parameter $parameter
  *
  * @return array
  * @throws \Guzzle\Service\Exception\ValidationException
  */
 public static function tagsOrAliases(array $aliases, Parameter $parameter)
 {
     foreach ($aliases as $alias) {
         if (!is_string($alias) || strlen($alias) > 128) {
             throw new ValidationException("Invalid value [{$alias}] in {$parameter->getName()}");
         }
     }
     return $aliases;
 }
Example #6
0
 /**
  * Validates the structure of a link (from the service description)
  *
  * @param \Guzzle\Service\Description\Parameter $structure
  *
  * @throws \Desk\Exception\UnexpectedValueException If it's invalid
  */
 public function validateLinkStructure(Parameter $structure)
 {
     if (!$structure->getData('operation')) {
         throw new UnexpectedValueException("Parameter with 'links' location requires 'operation'");
     }
     if (!$structure->getData('pattern')) {
         throw new UnexpectedValueException("Parameter with 'links' location requires 'pattern'");
     }
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
 {
     $entityBody = EntityBody::factory($value);
     $request->setBody($entityBody);
     $this->addExpectHeader($request, $entityBody, $param->getData('expect_header'));
     // Add the Content-Encoding header if one is set on the EntityBody
     if ($encoding = $entityBody->getContentEncoding()) {
         $request->setHeader('Content-Encoding', $encoding);
     }
 }
 public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
 {
     if (isset($this->data[$command])) {
         $json = $this->data[$command];
     } else {
         $json = array();
     }
     $json[$param->getWireName()] = $this->prepareValue($value, $param);
     $this->data[$command] = $json;
 }
Example #9
0
 /**
  * Add a prefixed array of headers to the request
  *
  * @param RequestInterface $request Request to update
  * @param Parameter        $param   Parameter object
  * @param array            $value   Header array to add
  *
  * @throws InvalidArgumentException
  */
 protected function addPrefixedHeaders(RequestInterface $request, Parameter $param, $value)
 {
     if (!is_array($value)) {
         throw new InvalidArgumentException('An array of mapped headers expected, but received a single value');
     }
     $prefix = $param->getSentAs();
     foreach ($value as $headerName => $headerValue) {
         $request->setHeader($prefix . $headerName, $headerValue);
     }
 }
Example #10
0
 /**
  * Processes model data according to a parameter schema
  *
  * @param Desk\Relationship\Resource\EmbeddedCommand $command
  * @param Guzzle\Service\Description\Parameter       $schema
  * @param array                                      $data
  *
  * @return array
  */
 public function process(EmbeddedCommand $command, Parameter $schema, array $data)
 {
     $result = array();
     $visitors = array();
     $properties = $schema->getProperties();
     foreach ($properties as $property) {
         $location = $property->getLocation();
         if ($location && !isset($visitors[$location])) {
             // add visitor for this location and trigger before()
             $visitor = $this->visitors->getResponseVisitor($location);
             $visitor->before($command, $result);
             $visitors[$location] = $visitor;
         }
     }
     $response = $command->getResponse();
     // Visit additional properties when it is an actual schema
     $additional = $schema->getAdditionalProperties();
     if ($additional instanceof Parameter) {
         // Only visit when a location is specified
         $location = $additional->getLocation();
         if ($location) {
             if (!isset($visitors[$location])) {
                 $visitors[$location] = $this->visitors->getResponseVisitor($location);
                 $visitors[$location]->before($command, $result);
             }
             // Only traverse if an array was parsed from the before() visitors
             if (is_array($result)) {
                 // Find each additional property
                 foreach (array_keys($result) as $key) {
                     // Check if the model actually knows this property. If so, then it is not additional
                     if (!$schema->getProperty($key)) {
                         // Set the name to the key so that we can parse it with each visitor
                         $additional->setName($key);
                         $visitors[$location]->visit($command, $response, $additional, $result);
                     }
                 }
                 // Reset the additionalProperties name to null
                 $additional->setName(null);
             }
         }
     }
     // Apply the parameter value with the location visitor
     foreach ($properties as $property) {
         $location = $property->getLocation();
         if ($location) {
             $visitors[$location]->visit($command, $response, $property, $result);
         }
     }
     // Call the after() method of each found visitor
     foreach ($visitors as $visitor) {
         $visitor->after($command);
     }
     return $result;
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context = null)
 {
     // check if there's a link provided for the param's "wire" name
     $links = $this->get($command, 'links');
     if (!empty($links[$param->getWireName()])) {
         // create a command representing the link
         $linkCommand = $this->builder->createLinkCommand($command, $param, $links[$param->getWireName()]);
         // store the created link command in the results array
         $value[self::ELEMENT][$param->getName()] = $linkCommand;
     }
 }
Example #12
0
 protected function processXmlAttribute(Parameter $property, array &$value)
 {
     $sentAs = $property->getWireName();
     if (isset($value['@attributes'][$sentAs])) {
         $value[$property->getName()] = $value['@attributes'][$sentAs];
         unset($value['@attributes'][$sentAs]);
         if (empty($value['@attributes'])) {
             unset($value['@attributes']);
         }
     }
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context = null)
 {
     // check if there's embedded resource data for the parameter
     $resourceData = $this->get($command, $this->getFieldName());
     if (!empty($resourceData[$param->getWireName()])) {
         // create the resource representing the embedded resource data
         $resource = $this->createResourceFromData($command, $param, $resourceData[$param->getWireName()]);
         // store the created embedded resource in the results array
         $value[$this->getOutputFieldName()][$param->getName()] = $resource;
     }
 }
Example #14
0
 protected function processPrefixedHeaders(Response $response, Parameter $param, &$value)
 {
     if ($prefix = $param->getSentAs()) {
         $container = $param->getName();
         $len = strlen($prefix);
         foreach ($response->getHeaders()->toArray() as $key => $header) {
             if (stripos($key, $prefix) === 0) {
                 $value[$container][substr($key, $len)] = count($header) == 1 ? end($header) : $header;
             }
         }
     }
 }
 /**
  * Custom handling for arrays
  *
  * @param Parameter $param  Parameter for the object
  * @param array     $value  Value that is set for this parameter
  * @param string    $prefix Prefix for the resulting key
  * @param array     $query  Query string array passed by reference
  */
 protected function resolveArray(Parameter $param, array $value, $prefix, array &$query)
 {
     $offset = $param->getData('offset') ?: 1;
     foreach ($value as $index => $v) {
         $index += $offset;
         if (is_array($v) && ($items = $param->getItems())) {
             $this->customResolver($v, $items, $query, $prefix . '.' . $index);
         } else {
             $query[$prefix . '.' . $index] = $param->filter($v);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context = null)
 {
     // check if there's an embedded resource for the parameter's
     // "wire" name
     $resources = $this->get($command, 'embedded');
     if (!empty($resources[$param->getWireName()])) {
         // create a model representing the embedded resource data
         $embeddedModel = $this->builder->createEmbeddedModel($command, $param, $resources[$param->getWireName()]);
         // store the created embedded model in the results array
         $value[self::ELEMENT][$param->getName()] = $embeddedModel;
     }
 }
Example #17
0
 public function testCanAddArrayOfSimpleTypes()
 {
     $request = new EntityEnclosingRequest('POST', 'http://foo.com');
     $visitor = new XmlVisitor();
     $param = new Parameter(array('type' => 'object', 'location' => 'xml', 'name' => 'Out', 'properties' => array('Nodes' => array('required' => true, 'type' => 'array', 'min' => 1, 'items' => array('type' => 'string', 'sentAs' => 'Node')))));
     $param->setParent(new Operation(array('data' => array('xmlRoot' => array('name' => 'Test', 'namespaces' => array('https://foo/'))))));
     $value = array('Nodes' => array('foo', 'baz'));
     $this->assertTrue($this->validator->validate($param, $value));
     $visitor->visit($this->command, $request, $param, $value);
     $visitor->after($this->command, $request);
     $this->assertEquals("<?xml version=\"1.0\"?>\n" . "<Test xmlns=\"https://foo/\"><Out><Nodes><Node>foo</Node><Node>baz</Node></Nodes></Out></Test>\n", (string) $request->getBody());
 }
Example #18
0
 /**
  * Process a prefixed header array
  *
  * @param Response  $response Response that contains the headers
  * @param Parameter $param    Parameter object
  * @param array     $value    Value response array to modify
  */
 protected function processPrefixedHeaders(Response $response, Parameter $param, &$value)
 {
     // Grab prefixed headers that should be placed into an array with the prefix stripped
     if ($prefix = $param->getSentAs()) {
         $container = $param->getName();
         $len = strlen($prefix);
         // Find all matching headers and place them into the containing element
         foreach ($response->getHeaders() as $key => $header) {
             if (stripos($key, $prefix) === 0) {
                 // Account for multi-value headers
                 $value[$container][substr($key, $len)] = count($header) == 1 ? end($header) : $header;
             }
         }
     }
 }
 protected function prepareAdditionalParameters(OperationInterface $operation, CommandInterface $command, RequestInterface $request, Parameter $additional)
 {
     if (!($location = $additional->getLocation())) {
         return;
     }
     $visitor = $this->factory->getRequestVisitor($location);
     $hidden = $command[$command::HIDDEN_PARAMS];
     foreach ($command->toArray() as $key => $value) {
         if ($value !== null && !in_array($key, $hidden) && !$operation->hasParam($key)) {
             $additional->setName($key);
             $visitor->visit($command, $request, $additional, $value);
         }
     }
     return $visitor;
 }
 /**
  * Map nested parameters into the location_key based parameters
  *
  * @param array     $value Value to map
  * @param Parameter $param Parameter that holds information about the current key
  *
  * @return array Returns the mapped array
  */
 protected function resolveRecursively(array $value, Parameter $param)
 {
     foreach ($value as $name => $v) {
         if ($subParam = $param->getProperty($name)) {
             $key = $subParam->getWireName();
             if (is_array($v)) {
                 $value[$key] = $this->resolveRecursively($v, $subParam);
             } elseif ($name != $key) {
                 $value[$key] = $param->filter($v);
                 unset($value[$name]);
             }
         }
     }
     return $value;
 }
Example #21
0
protected function resolveRecursively(array $value, Parameter $param)
{
foreach ($value as $name => &$v) {
switch ($param->getType()) {
case 'object':
if ($subParam = $param->getProperty($name)) {
$key = $subParam->getWireName();
$value[$key] = $this->prepareValue($v, $subParam);
if ($name != $key) {
unset($value[$name]);
}
} elseif ($param->getAdditionalProperties() instanceof Parameter) {
$v = $this->prepareValue($v, $param->getAdditionalProperties());
}
break;
case 'array':
if ($items = $param->getItems()) {
$v = $this->prepareValue($v, $items);
}
break;
}
}

return $param->filter($value);
}
 /**
  * Map nested parameters into the location_key based parameters
  *
  * @param array    $value  Value to map
  * @param Parameter $param  Parameter that holds information about the current key
  * @param array    $query  Built up query string values
  * @param string   $prefix String to prepend to sub query values
  */
 protected function customResolver($value, Parameter $param, array &$query, $prefix = '')
 {
     if ($param->getType() == 'object') {
         foreach ($value as $name => $v) {
             if ($subParam = $param->getProperty($name)) {
                 $key = $prefix . '.' . $subParam->getWireName();
                 if (is_array($v)) {
                     $this->customResolver($v, $subParam, $query, $key);
                 } else {
                     $query[$key] = $v;
                 }
             }
         }
     } elseif ($param->getType() == 'array') {
         $offset = $param->getData('offset') ?: 0;
         foreach ($value as $index => $v) {
             $index += $offset;
             if (is_array($v) && ($items = $param->getItems())) {
                 $this->customResolver($v, $items, $query, $prefix . '.' . $index);
             } else {
                 $query[$prefix . '.' . $index] = $v;
             }
         }
     } else {
         $query[$prefix] = $value;
     }
 }
Example #23
0
 /**
  * Custom handling for arrays
  *
  * @param Parameter $param  Parameter for the object
  * @param array     $value  Value that is set for this parameter
  * @param string    $prefix Prefix for the resulting key
  * @param array     $query  Query string array passed by reference
  */
 protected function resolveArray(Parameter $param, array $value, $prefix, array &$query)
 {
     static $serializeEmpty = array('SetLoadBalancerPoliciesForBackendServer' => 1, 'SetLoadBalancerPoliciesOfListener' => 1, 'UpdateStack' => 1);
     // For BC, serialize empty lists for specific operations
     if (!$value) {
         if (isset($serializeEmpty[$this->fqname])) {
             $query[$prefix] = '';
         }
         return;
     }
     $offset = $param->getData('offset') ?: 1;
     foreach ($value as $index => $v) {
         $index += $offset;
         if (is_array($v) && ($items = $param->getItems())) {
             $this->customResolver($v, $items, $query, $prefix . '.' . $index);
         } else {
             $query[$prefix . '.' . $index] = $param->filter($v);
         }
     }
 }
Example #24
0
 /**
  * Perform transformations on the result array
  *
  * @param Parameter        $model    Model that defines the structure
  * @param CommandInterface $command  Command that performed the operation
  * @param Response         $response Response received
  *
  * @return array Returns the array of result data
  */
 protected function visitResult(Parameter $model, CommandInterface $command, Response $response)
 {
     // Determine what visitors are associated with the model
     $foundVisitors = $result = array();
     foreach ($model->getProperties() as $schema) {
         if ($location = $schema->getLocation()) {
             $foundVisitors[$location] = $this->factory->getResponseVisitor($location);
             $foundVisitors[$location]->before($command, $result);
         }
     }
     foreach ($model->getProperties() as $schema) {
         /** @var $arg Parameter */
         if ($location = $schema->getLocation()) {
             // Apply the parameter value with the location visitor
             $foundVisitors[$location]->visit($command, $response, $schema, $result);
         }
     }
     foreach ($foundVisitors as $visitor) {
         $visitor->after($command);
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
 {
     $filteredValue = $param->filter($value);
     if (null !== $this->serializer && (is_object($filteredValue) || is_array($filteredValue))) {
         switch ($param->getSentAs()) {
             case 'json':
                 $request->setHeader('Content-Type', 'application/json');
                 $contentType = 'json';
                 break;
             case 'yml':
             case 'yaml':
                 $request->setHeader('Content-Type', 'application/yaml');
                 $contentType = 'yml';
                 break;
             default:
                 $request->setHeader('Content-Type', 'application/xml');
                 $contentType = 'xml';
                 break;
         }
         $context = SerializationContext::create();
         if (null !== ($groups = $param->getData('jms_serializer.groups'))) {
             $context->setGroups($groups);
         }
         if (null !== ($version = $param->getData('jms_serializer.version'))) {
             $context->setVersion($version);
         }
         if (null !== ($nulls = $param->getData('jms_serializer.serialize_nulls'))) {
             $context->setSerializeNull($nulls);
         }
         if (true === $param->getData('jms_serializer.max_depth_checks')) {
             $context->enableMaxDepthChecks();
         }
         $value = $this->serializer->serialize($filteredValue, $contentType, $context);
     }
     parent::visit($command, $request, $param, $value);
 }
Example #26
0
 /**
  * Recursively process a parameter while applying filters
  *
  * @param Parameter $param API parameter being validated
  * @param mixed     $value Value to validate and process. The value may change during this process.
  */
 protected function recursiveProcess(Parameter $param, &$value)
 {
     if ($value === null) {
         return;
     }
     if (is_array($value)) {
         $type = $param->getType();
         if ($type == 'array') {
             foreach ($value as &$item) {
                 $this->recursiveProcess($param->getItems(), $item);
             }
         } elseif ($type == 'object' && !isset($value[0])) {
             // On the above line, we ensure that the array is associative and not numerically indexed
             $knownProperties = array();
             if ($properties = $param->getProperties()) {
                 foreach ($properties as $property) {
                     $name = $property->getName();
                     $key = $property->getWireName();
                     $knownProperties[$name] = 1;
                     if (isset($value[$key])) {
                         $this->recursiveProcess($property, $value[$key]);
                         if ($key != $name) {
                             $value[$name] = $value[$key];
                             unset($value[$key]);
                         }
                     }
                 }
             }
             // Remove any unknown and potentially unsafe properties
             if ($param->getAdditionalProperties() === false) {
                 $value = array_intersect_key($value, $knownProperties);
             } elseif (($additional = $param->getAdditionalProperties()) !== true) {
                 // Validate and filter additional properties
                 foreach ($value as &$v) {
                     $this->recursiveProcess($additional, $v);
                 }
             }
         }
     }
     $value = $param->filter($value);
 }
Example #27
0
protected function recursiveProcess(Parameter $param, &$value)
{
if ($value === null) {
return;
}

if (is_array($value)) {
$type = $param->getType();
if ($type == 'array') {
foreach ($value as &$item) {
$this->recursiveProcess($param->getItems(), $item);
}
} elseif ($type == 'object' && !isset($value[0])) {

 $knownProperties = array();
if ($properties = $param->getProperties()) {
foreach ($properties as $property) {
$name = $property->getName();
$key = $property->getWireName();
$knownProperties[$name] = 1;
if (isset($value[$key])) {
$this->recursiveProcess($property, $value[$key]);
if ($key != $name) {
$value[$name] = $value[$key];
unset($value[$key]);
}
}
}
}


 if ($param->getAdditionalProperties() === false) {
$value = array_intersect_key($value, $knownProperties);
} elseif (($additional = $param->getAdditionalProperties()) !== true) {

 foreach ($value as &$v) {
$this->recursiveProcess($additional, $v);
}
}
}
}

$value = $param->filter($value);
}
Example #28
0
 /**
  * Add a parameter to the command
  *
  * @param Parameter $param Parameter to add
  *
  * @return self
  */
 public function addParam(Parameter $param)
 {
     $this->parameters[$param->getName()] = $param;
     $param->setParent($this);
     return $this;
 }
Example #29
0
 public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
 {
     $request->getQuery()->set($param->getWireName(), $this->prepareValue($value, $param));
 }
Example #30
0
 /**
  * Add an object to the XML
  */
 protected function addXmlObject(\XMLWriter $xmlWriter, Parameter $param, &$value)
 {
     $noAttributes = array();
     // add values which have attributes
     foreach ($value as $name => $v) {
         if ($property = $param->getProperty($name)) {
             if ($property->getData('xmlAttribute')) {
                 $this->addXml($xmlWriter, $property, $v);
             } else {
                 $noAttributes[] = array('value' => $v, 'property' => $property);
             }
         }
     }
     // now add values with no attributes
     foreach ($noAttributes as $element) {
         $this->addXml($xmlWriter, $element['property'], $element['value']);
     }
 }