diff --git a/src/Bookboon.php b/src/Bookboon.php index 7282cc2..d6bcb62 100644 --- a/src/Bookboon.php +++ b/src/Bookboon.php @@ -20,9 +20,11 @@ */ use Bookboon\Api\Cache\Cache; +use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Client\Client; use Bookboon\Api\Client\Headers; use Bookboon\Api\Client\OauthClient; +use Psr\Http\Message\ResponseInterface; class Bookboon { @@ -62,7 +64,7 @@ public static function create($appId, $appSecret, array $scopes, array $headers * @param array $variables * @param $httpMethod * @param bool $shouldCache - * @return array + * @return BookboonResponse */ public function rawRequest($url, array $variables = [], $httpMethod = Client::HTTP_GET, $shouldCache = true) { diff --git a/src/Client/BasicAuthClient.php b/src/Client/BasicAuthClient.php index ea6643e..38d54c5 100644 --- a/src/Client/BasicAuthClient.php +++ b/src/Client/BasicAuthClient.php @@ -49,7 +49,7 @@ public function __construct($apiId, $apiSecret, Headers $headers, Cache $cache = * @oaram string $contentType * * @param string $contentType - * @return array + * @return BookboonResponse * @throws ApiGeneralException * @throws ApiTimeoutException */ @@ -93,7 +93,14 @@ protected function executeQuery($url, $type = self::HTTP_GET, $variables = array curl_close($http); - return $this->handleResponse(substr($response, $headersSize), substr($response, 0, $headersSize), $httpStatus, $url); + $responseArray = $this->handleResponse( + substr($response, $headersSize), + substr($response, 0, $headersSize), + $httpStatus, + $url + ); + + return new BookboonResponse($responseArray, $headers); } /** diff --git a/src/Client/BookboonResponse.php b/src/Client/BookboonResponse.php new file mode 100644 index 0000000..6d065b5 --- /dev/null +++ b/src/Client/BookboonResponse.php @@ -0,0 +1,66 @@ +returnArray = $returnArray; + $this->headers = $headers; + } + + /** + * @return array + */ + public function getHeaders() + { + return $this->headers; + } + + /** + * @return array + */ + public function getReturnArray() + { + return $this->returnArray; + } + + /** + * @return array + */ + public function getEntityStore() + { + return $this->entityStore; + } + + /** + * @param $entityStore + */ + public function setEntityStore(EntityStore $entityStore) + { + $this->entityStore = $entityStore; + } +} diff --git a/src/Client/OauthClient.php b/src/Client/OauthClient.php index b29f7f0..d4ba54b 100644 --- a/src/Client/OauthClient.php +++ b/src/Client/OauthClient.php @@ -117,7 +117,14 @@ protected function executeQuery($url, $type = Client::HTTP_GET, $variables = arr $response = $e->getResponse(); } - return $this->handleResponse($response->getBody()->getContents(), $response->getHeaders(), $response->getStatusCode(), $url); + $responseArray = $this->handleResponse( + $response->getBody()->getContents(), + $response->getHeaders(), + $response->getStatusCode(), + $url + ); + + return new BookboonResponse($responseArray, $response->getHeaders()); } /** diff --git a/src/Entity/Author.php b/src/Entity/Author.php index d44c358..fe12dc2 100644 --- a/src/Entity/Author.php +++ b/src/Entity/Author.php @@ -3,16 +3,15 @@ namespace Bookboon\Api\Entity; use Bookboon\Api\Bookboon; +use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Exception\BadUUIDException; class Author extends Entity { /** - * Get Author. - * * @param Bookboon $bookboon - * @param string $authorId - * @return Author + * @param $authorId + * @return BookboonResponse * @throws BadUUIDException */ public static function get(Bookboon $bookboon, $authorId) @@ -21,7 +20,17 @@ public static function get(Bookboon $bookboon, $authorId) throw new BadUUIDException("UUID Not Formatted Correctly"); } - return new static($bookboon->rawRequest("/authors/$authorId")); + $bResponse = $bookboon->rawRequest("/authors/$authorId"); + + $bResponse->setEntityStore( + new EntityStore( + [ + new static($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** @@ -29,7 +38,7 @@ public static function get(Bookboon $bookboon, $authorId) * * @param Bookboon $bookboon * @param string $bookId - * @return Author[] + * @return BookboonResponse * @throws BadUUIDException */ public function getByBookId(Bookboon $bookboon, $bookId) @@ -38,9 +47,17 @@ public function getByBookId(Bookboon $bookboon, $bookId) throw new BadUUIDException("UUID Not Formatted Correctly"); } - $authors = $bookboon->rawRequest("/books/$bookId/authors"); + $bResponse = $bookboon->rawRequest("/books/$bookId/authors"); + + $bResponse->setEntityStore( + new EntityStore( + [ + static::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); - return static::getEntitiesFromArray($authors); + return $bResponse; } protected function isValid(array $array) diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 6c1fa40..d3753ce 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -3,6 +3,7 @@ namespace Bookboon\Api\Entity; use Bookboon\Api\Bookboon; +use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Client\Client; class Book extends Entity @@ -21,16 +22,24 @@ class Book extends Entity const FORMAT_M3U = 'm3u'; /** - * Get Book object. - * * @param Bookboon $bookboon - * @param string|array $bookId uuid for book - * @param bool $extendedMetadata bool include reviews and similar books - * @return Book + * @param $bookId + * @param bool $extendedMetadata + * @return BookboonResponse */ public static function get(Bookboon $bookboon, $bookId, $extendedMetadata = false) { - return static::objectTransformer($bookboon->rawRequest("/books/$bookId", array('extendedMetadata' => $extendedMetadata ? 'true' : 'false'))); + $bResponse = $bookboon->rawRequest("/books/$bookId", array('extendedMetadata' => $extendedMetadata ? 'true' : 'false')); + + $bResponse->setEntityStore( + new EntityStore( + [ + static::objectTransformer($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** @@ -39,7 +48,7 @@ public static function get(Bookboon $bookboon, $bookId, $extendedMetadata = fals * @param Bookboon $bookboon * @param string[] $bookIds * @param bool $extendedMetadata - * @return Book[] + * @return BookboonResponse */ public static function getMultiple(Bookboon $bookboon, array $bookIds, $extendedMetadata = false) { @@ -48,7 +57,17 @@ public static function getMultiple(Bookboon $bookboon, array $bookIds, $extended 'extendedMetadata' => $extendedMetadata ? 'true' : 'false' ); - return static::getEntitiesFromArray($bookboon->rawRequest("/books", $variables)); + $bResponse = $bookboon->rawRequest("/books", $variables); + + $bResponse->setEntityStore( + new EntityStore( + [ + static::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** @@ -56,7 +75,7 @@ public static function getMultiple(Bookboon $bookboon, array $bookIds, $extended * * @param Bookboon $bookboon * @param bool $extendedMetadata - * @return Book[] + * @return BookboonResponse */ public static function getAll(Bookboon $bookboon, $extendedMetadata = false) { @@ -65,7 +84,17 @@ public static function getAll(Bookboon $bookboon, $extendedMetadata = false) 'extendedMetadata' => $extendedMetadata ? 'true' : 'false' ); - return static::getEntitiesFromArray($bookboon->rawRequest("/books", $variables)); + $bResponse = $bookboon->rawRequest("/books", $variables); + + $bResponse->setEntityStore( + new EntityStore( + [ + static::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** @@ -105,9 +134,10 @@ public static function getEntitiesFromArray(array $array) public static function getDownloadUrl(Bookboon $bookboon, $bookId, array $variables, $format = self::FORMAT_PDF) { $variables['format'] = $format; - $download = $bookboon->rawRequest("/books/$bookId/download", $variables, Client::HTTP_POST); - return $download['url']; + $bResponse = $bookboon->rawRequest("/books/$bookId/download", $variables, Client::HTTP_POST); + + return $bResponse->getReturnArray()['url']; } /** @@ -117,16 +147,21 @@ public static function getDownloadUrl(Bookboon $bookboon, $bookId, array $variab * @param $query string to search for * @param int $limit results to return per page * @param int $offset offset of results - * @return Book[] + * @return BookboonResponse */ public static function search(Bookboon $bookboon, $query, $limit = 10, $offset = 0) { - $search = $bookboon->rawRequest('/search', array('q' => $query, 'limit' => $limit, 'offset' => $offset)); - if (count($search) === 0) { - return array(); - } + $bResponse = $bookboon->rawRequest('/search', array('q' => $query, 'limit' => $limit, 'offset' => $offset)); - return Book::getEntitiesFromArray($search); + $bResponse->setEntityStore( + new EntityStore( + [ + Book::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** @@ -136,13 +171,21 @@ public static function search(Bookboon $bookboon, $query, $limit = 10, $offset = * @param string $bookType * @param array $bookIds array of book ids to base recommendations on, can be empty * @param int $limit - * @return Book[] + * @return BookboonResponse */ public static function recommendations(Bookboon $bookboon, array $bookIds = array(), $limit = 5, $bookType = 'pdf') { - $recommendations = $bookboon->rawRequest('/recommendations', array('limit' => $limit, 'book' => $bookIds, 'bookType' => $bookType)); + $bResponse = $bookboon->rawRequest('/recommendations', array('limit' => $limit, 'book' => $bookIds, 'bookType' => $bookType)); + + $bResponse->setEntityStore( + new EntityStore( + [ + Book::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); - return Book::getEntitiesFromArray($recommendations); + return $bResponse; } protected function isValid(array $array) diff --git a/src/Entity/Category.php b/src/Entity/Category.php index 8a2d4f4..bf60b12 100644 --- a/src/Entity/Category.php +++ b/src/Entity/Category.php @@ -3,6 +3,7 @@ namespace Bookboon\Api\Entity; use Bookboon\Api\Bookboon; +use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Client\Client; use Bookboon\Api\Exception\BadUUIDException; @@ -16,11 +17,21 @@ class Category extends Entity * * @param Bookboon $bookboon * @param string $categoryId - * @return Category|bool + * @return BookboonResponse */ public static function get(Bookboon $bookboon, $categoryId) { - return new static($bookboon->rawRequest("/categories/$categoryId")); + $bResponse = $bookboon->rawRequest("/categories/$categoryId"); + + $bResponse->setEntityStore( + new EntityStore( + [ + new static($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** @@ -29,17 +40,27 @@ public static function get(Bookboon $bookboon, $categoryId) * @param Bookboon $bookboon * @param array $blacklistedCategoryIds * @param int $depth level of recursion (default 2 maximum, 0 no recursion) - * @return Category[] + * @return BookboonResponse */ public static function getTree(Bookboon $bookboon, array $blacklistedCategoryIds = array(), $depth = 2) { - $categories = $bookboon->rawRequest('/categories', array('depth' => $depth)); + $bResponse = $bookboon->rawRequest('/categories', array('depth' => $depth)); + + $categories = $bResponse->getReturnArray(); if (count($blacklistedCategoryIds) !== 0) { self::recursiveBlacklist($categories, $blacklistedCategoryIds); } - return Category::getEntitiesFromArray($categories); + $bResponse->setEntityStore( + new EntityStore( + [ + Category::getEntitiesFromArray($categories) + ] + ) + ); + + return $bResponse; } private static function recursiveBlacklist(&$categories, $blacklistedCategoryIds) @@ -65,9 +86,9 @@ private static function recursiveBlacklist(&$categories, $blacklistedCategoryIds */ public static function getDownloadUrl(Bookboon $bookboon, $categoryId, array $variables) { - $download = $bookboon->rawRequest("/categories/$categoryId/download", $variables, Client::HTTP_POST); + $bResponse = $bookboon->rawRequest("/categories/$categoryId/download", $variables, Client::HTTP_POST); - return $download['url']; + return $bResponse->getReturnArray()['url']; } protected function isValid(array $array) diff --git a/src/Entity/EntityStore.php b/src/Entity/EntityStore.php new file mode 100644 index 0000000..0b2c0e2 --- /dev/null +++ b/src/Entity/EntityStore.php @@ -0,0 +1,32 @@ +contents = $contents; + } + + /** + * @return array|mixed + */ + public function get() + { + if (count($this->contents) === 1) { + return $this->contents[0]; + } + + return $this->contents; + } +} diff --git a/src/Entity/Exam.php b/src/Entity/Exam.php index 8b6d7c5..248ac25 100644 --- a/src/Entity/Exam.php +++ b/src/Entity/Exam.php @@ -3,6 +3,7 @@ namespace Bookboon\Api\Entity; use Bookboon\Api\Bookboon; +use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Client\Client; use Bookboon\Api\Exception\BadUUIDException; @@ -13,7 +14,7 @@ class Exam extends Entity * * @param Bookboon $bookboon * @param string $examId - * @return Exam + * @return BookboonResponse * @throws BadUUIDException */ public static function get(Bookboon $bookboon, $examId) @@ -22,28 +23,52 @@ public static function get(Bookboon $bookboon, $examId) throw new BadUUIDException("UUID Not Formatted Correctly"); } - return new static($bookboon->rawRequest("/exams/$examId")); + $bResponse = $bookboon->rawRequest("/exams/$examId"); + + $bResponse->setEntityStore( + new EntityStore( + [ + new static($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } + /** + * @param Bookboon $bookboon + * @param $bookId + * @return BookboonResponse + * @throws BadUUIDException + */ public static function getByBookId(Bookboon $bookboon, $bookId) { - if (Entity::isValidUUID($bookId) === false) { + if (false === Entity::isValidUUID($bookId)) { throw new BadUUIDException("UUID Not Formatted Correctly"); } - $exams = $bookboon->rawRequest("/books/$bookId/exams"); + $bResponse = $bookboon->rawRequest("/books/$bookId/exams"); + + $bResponse->setEntityStore( + new EntityStore( + [ + Exam::getEntitiesFromArray($exams) + ] + ) + ); - return Exam::getEntitiesFromArray($exams); + return $bResponse; } public static function start(Bookboon $bookboon, $examId) { - return $bookboon->rawRequest("/exams/$examId", [], Client::HTTP_POST); + return $bookboon->rawRequest("/exams/$examId", [], Client::HTTP_POST)->getReturnArray(); } public static function finish(Bookboon $bookboon, $examId, $postVars) { - return $bookboon->rawRequest("/exams/$examId/submit", $postVars, Client::HTTP_POST); + return $bookboon->rawRequest("/exams/$examId/submit", $postVars, Client::HTTP_POST)->getReturnArray(); } protected function isValid(array $array) diff --git a/src/Entity/Frontpage.php b/src/Entity/Frontpage.php index e86527c..3856ad8 100644 --- a/src/Entity/Frontpage.php +++ b/src/Entity/Frontpage.php @@ -3,6 +3,7 @@ namespace Bookboon\Api\Entity; use Bookboon\Api\Bookboon; +use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Exception\UsageException; class Frontpage extends Entity @@ -16,11 +17,21 @@ class Frontpage extends Entity * Get all front page books * * @param Bookboon $bookboon - * @return Frontpage[] + * @return BookboonResponse */ public static function get(Bookboon $bookboon) { - return Frontpage::getEntitiesFromArray($bookboon->rawRequest("/frontpage")); + $bResponse = $bookboon->rawRequest("/frontpage"); + + $bResponse->setEntityStore( + new EntityStore( + [ + Frontpage::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** diff --git a/src/Entity/Question.php b/src/Entity/Question.php index e8bc231..e476431 100644 --- a/src/Entity/Question.php +++ b/src/Entity/Question.php @@ -3,6 +3,7 @@ namespace Bookboon\Api\Entity; use Bookboon\Api\Bookboon; +use Bookboon\Api\Client\BookboonResponse; class Question extends Entity { @@ -11,13 +12,21 @@ class Question extends Entity * * @param Bookboon $bookboon * @param array $answerIds array of answer ids, can be empty - * @return Question[] + * @return BookboonResponse */ public static function get(Bookboon $bookboon, array $answerIds = array()) { - $questions = $bookboon->rawRequest('/questions', array('answer' => $answerIds)); + $bResponse = $bookboon->rawRequest('/questions', array('answer' => $answerIds)); - return Question::getEntitiesFromArray($questions); + $bResponse->setEntityStore( + new EntityStore( + [ + Question::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } protected function isValid(array $array) diff --git a/src/Entity/Review.php b/src/Entity/Review.php index 65de9e6..89d4821 100644 --- a/src/Entity/Review.php +++ b/src/Entity/Review.php @@ -3,6 +3,7 @@ namespace Bookboon\Api\Entity; use Bookboon\Api\Bookboon; +use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Client\Client; use Bookboon\Api\Exception\BadUUIDException; @@ -13,7 +14,7 @@ class Review extends Entity * Get Reviews for specified Book. * * @param $bookId - * @return array of Review objects + * @return BookboonResponse * * @throws BadUUIDException */ @@ -23,9 +24,17 @@ public static function getByBookId(Bookboon $bookboon, $bookId) throw new BadUUIDException("UUID Not Formatted Correctly"); } - $reviews = $bookboon->rawRequest("/books/$bookId/review"); + $bResponse = $bookboon->rawRequest("/books/$bookId/review"); - return Review::getEntitiesFromArray($reviews); + $bResponse->setEntityStore( + new EntityStore( + [ + Review::getEntitiesFromArray($bResponse->getReturnArray()) + ] + ) + ); + + return $bResponse; } /** diff --git a/src/run.php b/src/run.php new file mode 100644 index 0000000..8939753 --- /dev/null +++ b/src/run.php @@ -0,0 +1,7 @@ +getEntityStore()->get(); } public function testGetText() diff --git a/tests/Entity/AuthorTest.php b/tests/Entity/AuthorTest.php index ba1b8cb..9f5b559 100644 --- a/tests/Entity/AuthorTest.php +++ b/tests/Entity/AuthorTest.php @@ -18,7 +18,7 @@ public static function setUpBeforeClass() { include_once(__DIR__ . '/../Helpers.php'); $bookboon = \Helpers::getBookboon(); - self::$data = Author::get($bookboon, '0908031c-ce02-9b86-11e6-6dd9aa4699d1'); + self::$data = Author::get($bookboon, '0908031c-ce02-9b86-11e6-6dd9aa4699d1')->getEntityStore()->get(); } public function providerTestGetters() diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index 13a67de..0a1855c 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -25,7 +25,7 @@ public static function setUpBeforeClass() { include_once(__DIR__ . '/../Helpers.php'); self::$bookboon = \Helpers::getBookboon(); - self::$data = Book::get(self::$bookboon, '3bf58559-034f-4676-bb5f-a2c101015a58'); + self::$data = Book::get(self::$bookboon, '3bf58559-034f-4676-bb5f-a2c101015a58')->getEntityStore()->get(); } public function testGetId() @@ -123,19 +123,20 @@ public function testBookDownloadBasic() public function testGetSearch() { // choose a query with almost certain response; - $search = Book::search(self::$bookboon, 'engineering'); + $search = Book::search(self::$bookboon, 'engineering')->getEntityStore()->get(); $this->assertCount(10, $search); } public function testGetRecommendations() { - $recommendations = Book::recommendations(self::$bookboon); - $this->assertCount(5, $recommendations); + $bResponse = Book::recommendations(self::$bookboon); + + $this->assertCount(5, $bResponse->getEntityStore()->get()); } public function testGetRecommendationsSpecific() { - $recommendations = Book::recommendations(self::$bookboon, array('3bf58559-034f-4676-bb5f-a2c101015a58'), 8); + $recommendations = Book::recommendations(self::$bookboon, array('3bf58559-034f-4676-bb5f-a2c101015a58'), 8)->getEntityStore()->get(); $this->assertCount(8, $recommendations); } } diff --git a/tests/Entity/CategoryTest.php b/tests/Entity/CategoryTest.php index d3de225..8bdd7ba 100644 --- a/tests/Entity/CategoryTest.php +++ b/tests/Entity/CategoryTest.php @@ -18,7 +18,9 @@ public static function setUpBeforeClass() { include_once(__DIR__ . '/../Helpers.php'); self::$bookboon = \Helpers::getBookboon(); - self::$data = Category::get(self::$bookboon, '062adfac-844b-4e8c-9242-a1620108325e'); + self::$data = Category::get(self::$bookboon, '062adfac-844b-4e8c-9242-a1620108325e') + ->getEntityStore() + ->get(); } public function testGetId() @@ -55,7 +57,7 @@ public function testInvalidCategory() public function testGetCategoryTree() { - $categories = Category::getTree(self::$bookboon); + $categories = Category::getTree(self::$bookboon)->getEntityStore()->get(); $this->assertEquals(2, count($categories)); } diff --git a/tests/Entity/QuestionTest.php b/tests/Entity/QuestionTest.php index 3b691dd..d538c65 100644 --- a/tests/Entity/QuestionTest.php +++ b/tests/Entity/QuestionTest.php @@ -19,7 +19,7 @@ public static function setUpBeforeClass() { include_once(__DIR__ . '/../Helpers.php'); self::$bookboon = \Helpers::getBookboon(); - self::$data = Question::get(self::$bookboon); + self::$data = Question::get(self::$bookboon)->getEntityStore()->get(); } public function testGetText() @@ -40,7 +40,7 @@ public function testSecondQuestions() $answers = $firstQuestion->getAnswers(); $firstAnswer = $answers[0]; - $questions = Question::get(self::$bookboon, array($firstAnswer->getId())); + $questions = Question::get(self::$bookboon, array($firstAnswer->getId()))->getEntityStore()->get(); $this->assertGreaterThan(1, count($questions)); } diff --git a/tests/Entity/ReviewTest.php b/tests/Entity/ReviewTest.php index b57973e..c04e217 100644 --- a/tests/Entity/ReviewTest.php +++ b/tests/Entity/ReviewTest.php @@ -15,7 +15,7 @@ public static function setUpBeforeClass() { include_once(__DIR__ . '/../Helpers.php'); $bookboon = \Helpers::getBookboon(); - self::$data = Review::getByBookId($bookboon, '3bf58559-034f-4676-bb5f-a2c101015a58'); + self::$data = Review::getByBookId($bookboon, '3bf58559-034f-4676-bb5f-a2c101015a58')->getEntityStore()->get(); } public function testGetAuthor()