-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from bookboon/feature/new-entities
Feature/new entities
- Loading branch information
Showing
7 changed files
with
354 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
namespace Bookboon\Api\Entity; | ||
|
||
use Bookboon\Api\Bookboon; | ||
use Bookboon\Api\Client\BookboonResponse; | ||
use Bookboon\Api\Exception\UsageException; | ||
|
||
class Journey extends Entity | ||
{ | ||
private $books; | ||
|
||
/** | ||
* @param Bookboon $bookboon | ||
* @param string $journeyId | ||
* @return BookboonResponse | ||
* @throws \Bookboon\Api\Exception\UsageException | ||
*/ | ||
public static function get(Bookboon $bookboon, string $journeyId) : BookboonResponse | ||
{ | ||
$bResponse = $bookboon->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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Bookboon\Api\Entity; | ||
|
||
use Bookboon\Api\Bookboon; | ||
use Bookboon\Api\Client\BookboonResponse; | ||
use Bookboon\Api\Exception\UsageException; | ||
|
||
class Language extends Entity | ||
{ | ||
/** | ||
* Get all languages | ||
* | ||
* @param Bookboon $bookboon | ||
* @param array $bookTypes | ||
* @return BookboonResponse | ||
* @throws UsageException | ||
* @throws \Bookboon\Api\Exception\ApiDecodeException | ||
*/ | ||
public static function get(Bookboon $bookboon, array $bookTypes = ['pdf']) : BookboonResponse | ||
{ | ||
$bResponse = $bookboon->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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,4 +86,4 @@ public static function remove(Bookboon $bookboon, string $email, ?string $alias) | |
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Bookboon\Api\Entity; | ||
|
||
use Bookboon\Api\Bookboon; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class JourneyTest | ||
* @package Bookboon\Api\Entity | ||
* @group entity | ||
*/ | ||
class JourneyTest extends TestCase | ||
{ | ||
private $methodsToTest = ['getTitle', 'getId', 'getAbstract', 'getDescription', 'getPublished']; | ||
private static $bookboon; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
include_once(__DIR__ . '/../Helpers.php'); | ||
self::$bookboon = \Helpers::getBookboon(); | ||
} | ||
|
||
public function providerTestGetters() | ||
{ | ||
return [ | ||
'getTitle' => ['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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Bookboon\Api\Entity; | ||
|
||
use Bookboon\Api\Bookboon; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class LanguageTest | ||
* @package Bookboon\Api\Entity | ||
* @group entity | ||
*/ | ||
class LanguageTest extends TestCase | ||
{ | ||
/** @var []Language */ | ||
private static $data = null; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
include_once(__DIR__ . '/../Helpers.php'); | ||
$bookboon = \Helpers::getBookboon(); | ||
self::$data = Language::get($bookboon) | ||
->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']); | ||
} | ||
} |