diff --git a/src/Entity/Frontpage.php b/src/Entity/Frontpage.php index 8e069cf..dd3a496 100644 --- a/src/Entity/Frontpage.php +++ b/src/Entity/Frontpage.php @@ -24,7 +24,7 @@ class Frontpage extends Entity */ public static function get(Bookboon $bookboon, array $bookTypes = ['pdf']) : BookboonResponse { - $bResponse = $bookboon->rawRequest("/frontpage", ['bookType' => join(',', $bookTypes)]); + $bResponse = $bookboon->rawRequest('/frontpage', ['bookType' => implode(',', $bookTypes)]); $bResponse->setEntityStore( new EntityStore(Frontpage::getEntitiesFromArray($bResponse->getReturnArray())) @@ -107,4 +107,4 @@ public function getBooks() : array { return Book::getEntitiesFromArray($this->safeGet('books', [])); } -} \ No newline at end of file +} diff --git a/src/Entity/Journey.php b/src/Entity/Journey.php new file mode 100644 index 0000000..f1801da --- /dev/null +++ b/src/Entity/Journey.php @@ -0,0 +1,147 @@ +rawRequest("/journeys/$journeyId"); + + $journeyEntity = new static($bResponse->getReturnArray()); + + if (count($journeyEntity->getBookIds()) > 0) { + $books = Book::getMultiple($bookboon, $journeyEntity->getBookIds(), false); + } + + $journeyEntity->setBooks($books->getEntityStore()->get()); + + $bResponse->setEntityStore( + new EntityStore( + [ + $journeyEntity + ] + ) + ); + + return $bResponse; + } + + /** + * Get all journeys + * + * @param Bookboon $bookboon + * @param array $bookTypes + * @return BookboonResponse + * @throws UsageException + * @throws \Bookboon\Api\Exception\ApiDecodeException + */ + public static function getAll(Bookboon $bookboon, array $bookTypes = ['pdf']) : BookboonResponse + { + $bResponse = $bookboon->rawRequest('/journeys'); + + $bResponse->setEntityStore( + new EntityStore(Journey::getEntitiesFromArray($bResponse->getReturnArray())) + ); + + return $bResponse; + } + + /** + * @return string id + */ + public function getId() : string + { + return $this->safeGet('_id'); + } + + /** + * @return string title + */ + public function getTitle() : string + { + return $this->safeGet('title'); + } + + /** + * @return string abstract + */ + public function getAbstract() : string + { + return $this->safeGet('abstract'); + } + + /** + * @return string description + */ + public function getDescription() : string + { + return $this->safeGet('description'); + } + + /** + * Return publish date. + * + * @return string date of publishing + */ + public function getPublished() : string + { + return $this->safeGet('published'); + } + + /** + * @return bool is featured + */ + public function isFeatured() : bool + { + return $this->safeGet('isFeatured'); + } + + /** + * @return array book ids + */ + public function getBookIds() : array + { + return $this->safeGet('books', []); + } + + /** + * @return array books + */ + public function getBooks() : array + { + return $this->books; + } + + /** + * @param array $books + * @return void + */ + public function setBooks(array $books) : void + { + $this->books = $books; + } + + /** + * Determines whether api response is valid + * + * @param array $array + * @return bool + */ + protected function isValid(array $array): bool + { + return isset($array['_id'], $array['title'], $array['abstract']); + } +} diff --git a/src/Entity/Language.php b/src/Entity/Language.php new file mode 100644 index 0000000..1c0dbdf --- /dev/null +++ b/src/Entity/Language.php @@ -0,0 +1,81 @@ +rawRequest('/languages'); + + $bResponse->setEntityStore( + new EntityStore(Language::getEntitiesFromArray($bResponse->getReturnArray())) + ); + + return $bResponse; + } + + /** + * @return string id + */ + public function getId() : string + { + return $this->safeGet('id'); + } + + /** + * @return string code + */ + public function getCode() : string + { + return $this->safeGet('code'); + } + + /** + * @return string slug + */ + public function getSlug() : string + { + return $this->safeGet('_slug'); + } + + /** + * @return string name + */ + public function getName() : string + { + return $this->safeGet('name'); + } + + /** + * @return string localized name + */ + public function getLocalizedName() : string + { + return $this->safeGet('localizedName'); + } + + /** + * Determines whether api response is valid + * + * @param array $array + * @return bool + */ + protected function isValid(array $array): bool + { + return isset($array['code'], $array['id'], $array['name']); + } +} diff --git a/src/Entity/Subscription.php b/src/Entity/Subscription.php index 212c3a0..e1d4231 100644 --- a/src/Entity/Subscription.php +++ b/src/Entity/Subscription.php @@ -86,4 +86,4 @@ public static function remove(Bookboon $bookboon, string $email, ?string $alias) } } - + diff --git a/tests/Entity/JourneyTest.php b/tests/Entity/JourneyTest.php new file mode 100644 index 0000000..b68979b --- /dev/null +++ b/tests/Entity/JourneyTest.php @@ -0,0 +1,66 @@ + ['getTitle'], + 'getId' => ['getId'], + 'getAbstract' => ['getAbstract'], + 'getDescription' => ['getDescription'], + 'getPublished' => ['getPublished'], + ]; + } + + public function testFirstAllJourneysData() + { + $data = Journey::getAll(self::$bookboon) + ->getEntityStore() + ->get()[0]; + + foreach ($this->methodsToTest as $method) { + $this->assertNotFalse($data->$method()); + } + } + + public function testGetJourney() + { + $data = Journey::get(self::$bookboon, '010e0268-0eec-4859-a67a-ce41ee2315c4') + ->getEntityStore() + ->get()[0]; + + foreach ($this->methodsToTest as $method) { + $this->assertNotFalse($data->$method()); + } + + $this->assertInstanceOf(Book::class, $data->getBooks()[0]); + } + + /** + * @expectedException \Bookboon\Api\Exception\EntityDataException + */ + public function testInvalidJourney() + { + $language = new Language(['blah']); + } +} diff --git a/tests/Entity/LanguageTest.php b/tests/Entity/LanguageTest.php new file mode 100644 index 0000000..aacf200 --- /dev/null +++ b/tests/Entity/LanguageTest.php @@ -0,0 +1,52 @@ +getEntityStore() + ->get()[0]; + } + + public function providerTestGetters() + { + return [ + 'getName' => ['getName'], + 'getId' => ['getId'], + 'getLocalizedName' => ['getLocalizedName'], + 'getCode' => ['getCode'], + ]; + } + + /** + * @dataProvider providerTestGetters + */ + public function testNotFalse($method) + { + $this->assertNotFalse(self::$data->$method()); + } + + /** + * @expectedException \Bookboon\Api\Exception\EntityDataException + */ + public function testInvalidLanguage() + { + $language = new Language(['blah']); + } +}