Пример #1
0
 /**
  * Handler for generating arguments list for POST methods.
  *
  * @see Lead\Resource\Controller::args()
  *
  * @param  object $request The request instance.
  * @param  array  $options An options array.
  * @return array
  */
 protected function _post($request, $options)
 {
     $payload = Payload::parse($request->body());
     if (!($collection = $payload->export())) {
         throw new ResourceException("No data provided for `{$this->name()}` resource(s), nothing to process.", 422);
     }
     $list = [];
     foreach ($collection as $data) {
         $list[] = [$options['binding']::create($data), $payload];
     }
     return $list;
 }
Пример #2
0
            expect($payload->meta())->toBe(['count' => 10]);
        });
        it("serializes parsed JSON-API payload", function () {
            $json = file_get_contents($this->fixture . 'collection.json');
            $payload = Payload::parse($json);
            expect($payload->serialize())->toEqual(json_decode($json, true));
            $json = file_get_contents($this->fixture . 'item.json');
            $payload = Payload::parse($json);
            expect($payload->serialize())->toEqual(json_decode($json, true));
        });
    });
    describe("::parse()", function () {
        it("parses JSON-API payload", function () {
            $json = file_get_contents($this->fixture . 'collection.json');
            $payload = Payload::parse($json);
            expect($payload->jsonapi())->toBe(['version' => '1.0']);
            expect($payload->meta())->toBe(['count' => 13]);
            expect($payload->links())->toBe(['self' => 'http://example.com/articles', 'next' => 'http://example.com/articles?page[offset]=2', 'last' => 'http://example.com/articles?page[offset]=10']);
            expect($payload->included())->toBe([['type' => 'people', 'id' => '9', 'attributes' => ['firstName' => 'Dan', 'lastName' => 'Gebhardt', 'twitter' => 'dgeb'], 'links' => ['self' => 'http://example.com/people/9']], ['type' => 'comments', 'id' => '5', 'attributes' => ['body' => 'First!'], 'relationships' => ['author' => ['data' => ['type' => 'people', 'id' => '2']]], 'links' => ['self' => 'http://example.com/comments/5']], ['type' => 'comments', 'id' => '12', 'attributes' => ['body' => 'I like XML better'], 'relationships' => ['author' => ['data' => ['type' => 'people', 'id' => '9']]], 'links' => ['self' => 'http://example.com/comments/12']]]);
        });
        it("parses JSON-API errors payload", function () {
            $json = file_get_contents($this->fixture . 'errors.json');
            $payload = Payload::parse($json);
            expect($payload->errors())->toBe([['code' => '123', 'source' => ['pointer' => '/data/attributes/firstName'], 'title' => 'Value is too short', 'detail' => 'First name must contain at least three characters.']]);
        });
        it("parses decoded JSON-API payload", function () {
            $payload = Payload::parse(['data' => [['id' => 1, 'attributes' => ['name' => 'value']]]]);
            expect($payload->export())->toBe([['id' => 1, 'name' => 'value']]);
        });
    });
});