Example #1
0
 /**
  * @param RequestInterface $request   Request that received an error
  * @param CommandInterface $command   Command that created the request
  * @param Operation        $operation Operation that defines the request and errors
  *
  * @return \Closure Returns a closure
  * @throws ErrorResponseException
  */
 protected function getErrorClosure(RequestInterface $request, CommandInterface $command, Operation $operation)
 {
     return function (Event $event) use($request, $command, $operation) {
         $response = $event['response'];
         foreach ($operation->getErrorResponses() as $error) {
             if (!isset($error['class'])) {
                 continue;
             }
             if (isset($error['code']) && $response->getStatusCode() != $error['code']) {
                 continue;
             }
             if (isset($error['reason']) && $response->getReasonPhrase() != $error['reason']) {
                 continue;
             }
             $className = $error['class'];
             $errorClassInterface = __NAMESPACE__ . '\\ErrorResponseExceptionInterface';
             if (!class_exists($className)) {
                 throw new ErrorResponseException("{$className} does not exist");
             } elseif (!in_array($errorClassInterface, class_implements($className))) {
                 throw new ErrorResponseException("{$className} must implement {$errorClassInterface}");
             }
             throw $className::fromCommand($command, $response);
         }
     };
 }
Example #2
0
 /**
  * Create the root XML element to use with a request
  *
  * @param Operation $operation Operation object
  *
  * @return \XMLWriter
  */
 protected function createRootElement(Operation $operation)
 {
     static $defaultRoot = array('name' => 'Request');
     // If no root element was specified, then just wrap the XML in 'Request'
     $root = $operation->getData('xmlRoot') ?: $defaultRoot;
     // Allow the XML declaration to be customized with xmlEncoding
     $encoding = $operation->getData('xmlEncoding');
     $xmlWriter = $this->startDocument($encoding);
     $xmlWriter->startElement($root['name']);
     // Create the wrapping element with no namespaces if no namespaces were present
     if (!empty($root['namespaces'])) {
         // Create the wrapping element with an array of one or more namespaces
         foreach ((array) $root['namespaces'] as $prefix => $uri) {
             $nsLabel = 'xmlns';
             if (!is_numeric($prefix)) {
                 $nsLabel .= ':' . $prefix;
             }
             $xmlWriter->writeAttribute($nsLabel, $uri);
         }
     }
     return $xmlWriter;
 }