public function testShouldBuildAnErrorResponse()
 {
     $item = new \stdClass();
     $item->firstname = 'john';
     $builder = ResponseBuilder::create('1.2', HttpMethod::GET());
     $builder->addParameter(Parameter::create('id', '42'))->addItem($item)->addError(Error::create('error message 1', 'reason1'))->addError(Error::create('error message 2', 'reason2', 'location2'))->setHttpErrorCode(HttpErrorCode::code402());
     $this->assertSame('{"apiVersion":"1.2","method":"GET","params":{"id":"42"},"code":402,"message":"error message 1","errors":[{"message":"error message 1","reason":"reason1","location":""},{"message":"error message 2","reason":"reason2","location":"location2"}]}', json_encode($builder->build()));
 }
 /**
  * Registers services on the given app.
  *
  * This method should only be used to configure services and parameters.
  * It should not get services.
  */
 public function register(Application $app)
 {
     $app['rest_normalizer.version'] = '0.1.0';
     $app['rest_normalizer.builder'] = $app->protect(function ($object = null) use($app) {
         $version = $app['rest_normalizer.version'];
         $request = $app['request'];
         /** @var $request \Symfony\Component\HttpFoundation\Request */
         $builder = ResponseBuilder::create($version, HttpMethod::valueOf($request->getMethod()), $object);
         foreach ($request->request->all() as $key => $value) {
             $parameter = Parameter::create($key, $value);
             $builder->addParameter($parameter);
         }
         return $builder;
     });
 }
 /**
  * Registers services on the given app.
  *
  * This method should only be used to configure services and parameters.
  * It should not get services.
  */
 public function register(Application $app)
 {
     if ($app->offsetGet('api.version') === false) {
         throw new \Exception('Missing api version');
     }
     $app['rest_normalizer.builder'] = $app->protect(function ($object = null) use($app) {
         $request = $app['request'];
         // @var $request Request
         $builder = ResponseBuilder::create($app['api.version'], HttpMethod::valueOf($request->getMethod()), $object);
         foreach ($request->request->all() as $key => $value) {
             $parameter = Parameter::create($key, $value);
             $builder->addParameter($parameter);
         }
         return $builder;
     });
 }
Ejemplo n.º 4
0
 /**
  * @param \stdClass $stdObj
  * @return ParameterInterface[]
  * @throws ResponseParserException
  */
 private function getParameters(\stdClass $stdObj)
 {
     $params = [];
     if (!isset($stdObj->params)) {
         throw new ResponseParserException('Parameter array expected');
     }
     foreach ($stdObj->params as $id => $values) {
         if (!is_array($values)) {
             $values = [$values];
         }
         $params[] = Parameter::create($id, $values);
     }
     return $params;
 }
 public function testShouldAddParameterCollection()
 {
     $builder = SuccessResponseBuilder::create(new SuccessDataBuilder(), '1.2', HttpMethod::GET());
     $builder->addParameters(new ParameterCollection([Parameter::create('firstName', 'john'), Parameter::create('name', 'doe')]));
     $this->assertSame('{"apiVersion":"1.2","method":"GET","params":{"firstName":"john","name":"doe"},"data":{"items":[],"totalItems":0}}', json_encode($builder->build()));
 }
 public function testShouldAddParameterCollection()
 {
     $builder = ErrorResponseBuilder::create(new ErrorDataBuilder(), '1.2', HttpMethod::GET(), HttpErrorCode::code500());
     $builder->addParameters(new ParameterCollection([Parameter::create('firstName', 'john'), Parameter::create('name', 'doe')]))->addError(Error::create('error message', 'ErrorType'));
     $this->assertSame('{"apiVersion":"1.2","method":"GET","params":{"firstName":"john","name":"doe"},"code":500,"message":"error message","errors":[{"message":"error message","reason":"ErrorType","location":""}]}', json_encode($builder->build()));
 }
Ejemplo n.º 7
0
 public function testValueIsOptional()
 {
     $this->assertSame([], Parameter::create("id")->getValues()->toArray());
 }