public function testUsers() { $response = ObjectResponse::factory(new Client(), 'account', ['_embedded' => ['users' => [['user' => ['name' => 'Kees'], 'right' => ['metadata' => 'meta']], ['user' => ['name' => 'Jan'], 'right' => ['metadata' => 'meta']]]]]); $users = $response->get('users'); $this->assertEquals(2, count($users)); $jan = array_pop($users); $this->assertEquals('Jan', $jan->get('name')); }
/** * Handle the response. Returns either an array or a PaginatedCollection. * * @param ResponseInterface $response * @return ObjectResponse|PaginatedCollection * @throws Exception\HttpException */ protected function handleResponse(ResponseInterface $response, array $requestOptions = []) { $body = json_decode($response->getBody()->getContents(), true) ?: []; if (!preg_match('~[23][0-9]{2}~', $response->getStatusCode())) { throw new Exception\HttpException($response->getStatusCode(), $body); } if (isset($body['pages'], $body['_embedded']) && ctype_digit($body['pages'])) { return new PaginatedCollection($this, $body); } if (isset($requestOptions['type'])) { return ObjectResponse::factory($this, $requestOptions['type'], $body); } return new ObjectResponse($body, $this->getOptions()); }
public function testFactory() { $this->assertInstanceOf('\\eResults\\Unity\\Api\\Response\\ObjectResponse', ObjectResponse::factory(new Client(), null)); $this->assertInstanceOf('\\eResults\\Unity\\Api\\Response\\AccountResponse', ObjectResponse::factory(new Client(), 'account', ['_embedded' => ['users' => []]])); $this->assertInstanceOf('\\eResults\\Unity\\Api\\Response\\UserResponse', ObjectResponse::factory(new Client(), 'user')); }