Exemplo n.º 1
0
 /**
  * @inheritdoc
  * @throws ResponseBuilderException
  */
 public function build(ResponseInterface $response)
 {
     if ($response->getErrors()->count() <= 0) {
         throw new ResponseBuilderException('One error at least is required in order to build a restful error');
     }
     $data = $this->buildData($response);
     $data->code = $response->getHttpErrorCode()->getValue();
     $data->message = $response->getErrors()->first()->getMessage()->getValue();
     $data->errors = [];
     /** @var ErrorInterface $error */
     foreach ($response->getErrors() as $error) {
         $errorData = new \stdClass();
         $errorData->message = $error->getMessage()->getValue();
         $errorData->reason = $error->getReason()->getValue();
         $errorData->location = $error->getLocation()->getValue();
         $data->errors[] = $errorData;
     }
     return $data;
 }
 /**
  * @param ResponseInterface $response
  * @return \stdClass
  *
  * @throws Crak\Component\RestNormalizer\Exception\ResponseBuilderException
  */
 public function buildData(ResponseInterface $response)
 {
     $data = new \stdClass();
     $data->apiVersion = $response->getApiVersion()->getValue();
     $data->method = $response->getHttpMethod()->getValue();
     $data->params = new \stdClass();
     /** @var Crak\Component\RestNormalizer\ParameterInterface $parameter */
     foreach ($response->getParameters() as $parameter) {
         $values = $parameter->getValues();
         $nbValues = count($values);
         $plainValues = null;
         if ($nbValues === 1) {
             $plainValues = $values->first()->getValue();
         } else {
             if ($nbValues > 1) {
                 foreach ($values as $value) {
                     $plainValues[] = $value->getValue();
                 }
             }
         }
         $data->params->{$parameter->getId()->getValue()} = $plainValues;
     }
     return $data;
 }