From fc0df1de4e641dfdfaf72ab4cceb92003c75af7c Mon Sep 17 00:00:00 2001 From: Lasse Mammen Date: Thu, 31 Jan 2019 14:06:38 +0000 Subject: [PATCH 1/3] Add AudioTalk book type Signed-off-by: Lasse Mammen --- src/Entity/AudioTalkBook.php | 8 ++++++++ src/Entity/Book.php | 1 + 2 files changed, 9 insertions(+) create mode 100644 src/Entity/AudioTalkBook.php diff --git a/src/Entity/AudioTalkBook.php b/src/Entity/AudioTalkBook.php new file mode 100644 index 0000000..f3b561e --- /dev/null +++ b/src/Entity/AudioTalkBook.php @@ -0,0 +1,8 @@ + Date: Thu, 31 Jan 2019 14:07:05 +0000 Subject: [PATCH 2/3] Prevent unknown book types throwing exceptions for missing class Signed-off-by: Lasse Mammen --- src/Entity/Book.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 4d86bd3..d201b9a 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -118,7 +118,13 @@ public static function getEntitiesFromArray(array $array) { $entities = []; foreach ($array as $object) { - $entities[] = self::objectTransformer($object); + if (in_array( + $object['_type'], + [self::TYPE_PDF, self::TYPE_AUDIO, self::TYPE_VIDEO, self::TYPE_AUDIOTALK], + true) + ) { + $entities[] = self::objectTransformer($object); + } } return $entities; From 9ccd486008ac5fea14b50258bd0b0165d4ff88db Mon Sep 17 00:00:00 2001 From: Lasse Mammen Date: Thu, 31 Jan 2019 14:17:49 +0000 Subject: [PATCH 3/3] Make Book entity abstract Signed-off-by: Lasse Mammen --- src/Entity/Book.php | 4 ++-- src/Entity/Exam.php | 3 +-- tests/Entity/BookTest.php | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Entity/Book.php b/src/Entity/Book.php index d201b9a..dd30f2e 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -6,7 +6,7 @@ use Bookboon\Api\Client\BookboonResponse; use Bookboon\Api\Client\ClientInterface; -class Book extends Entity +abstract class Book extends Entity { const _OWN_TYPE = ''; @@ -104,7 +104,7 @@ public static function getAll( * @param array $objectArray * @return Book */ - private static function objectTransformer(array $objectArray) + public static function objectTransformer(array $objectArray) { $className = 'Bookboon\Api\Entity\\' . ucfirst($objectArray['_type']) . 'Book'; return new $className($objectArray); diff --git a/src/Entity/Exam.php b/src/Entity/Exam.php index 2080450..fafdbb2 100644 --- a/src/Entity/Exam.php +++ b/src/Entity/Exam.php @@ -128,11 +128,10 @@ public function getTimeSeconds() * Return book to which the exam is related * * @return Book - * @throws \Bookboon\Api\Exception\EntityDataException */ public function getBook() { - return new Book($this->safeGet('book', [])); + return Book::objectTransformer($this->safeGet('book', [])); } /** diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index dc3d92f..45ff6b0 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -107,7 +107,7 @@ public function testHasPdf() */ public function testInvalidBook() { - $book = new Book(['blah']); + $book = new PdfBook(['blah']); } public function testBookDownloadOauth()