Skip to content

Commit

Permalink
feat: introduced Chapter and TimeTag entities
Browse files Browse the repository at this point in the history
  • Loading branch information
robo220 committed Aug 6, 2021
1 parent ad52409 commit 3856e39
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,40 @@ public static function getAll(

return $bResponse;
}
/**
* Get many books by filter
*
* @param Bookboon $bookboon
* @param array $bookTypes
* @param array $filters
* @return BookboonResponse<Book>
* @throws \Bookboon\Api\Exception\UsageException
**/
public static function getByFilters(
Bookboon $bookboon,
array $bookTypes = [self::_OWN_TYPE],
array $filters
) : BookboonResponse {
$variables = [
'bookType' => join(',', $bookTypes),
];

$variables = array_merge($variables, $filters);

$bResponse = $bookboon->rawRequest(
'/v1/books',
$variables,
ClientInterface::HTTP_GET,
true,
Book::class
);

$bResponse->setEntityStore(
new EntityStore(static::getEntitiesFromArray($bResponse->getReturnArray()), Book::class)
);

return $bResponse;
}

/**
* @param array $objectArray
Expand Down
84 changes: 84 additions & 0 deletions src/Entity/Chapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Bookboon\Api\Entity;

use Bookboon\Api\Bookboon;
use Bookboon\Api\Client\BookboonResponse;
use Bookboon\Api\Client\ClientInterface;
use Bookboon\Api\Exception\BadUUIDException;

class Chapter extends Entity
{
/**
* @param Bookboon $bookboon
* @param string $authorId
* @return BookboonResponse<Chapter>
* @throws BadUUIDException
* @throws \Bookboon\Api\Exception\EntityDataException
* @throws \Bookboon\Api\Exception\UsageException
*/
public static function getByBookId(Bookboon $bookboon, string $bookId) : BookboonResponse
{
if (Entity::isValidUUID($bookId) === false) {
throw new BadUUIDException();
}

$bResponse = $bookboon->rawRequest(
"/v1/books/$bookId/chapters",
[],
ClientInterface::HTTP_GET,
true,
Chapter::class
);

$bResponse->setEntityStore(
new EntityStore(static::getEntitiesFromArray($bResponse->getReturnArray()), Chapter::class)
);

return $bResponse;
}

protected function isValid(array $array) : bool
{
return isset($array['_id'], $array['duration'], $array['position'], $array['title']);
}


/**
* @return string UUID of entity
*/
public function getId() : string
{
return $this->safeGet('_id');
}

/**
* Return position of the chapter.
*
* @return int position
*/
public function getPosition() : int
{
return $this->safeGet('position');
}

/**
* Return duration of the chapter.
*
* @return int duration
*/
public function getDuration() : int
{
return $this->safeGet('duration');
}

/**
* Return title of the chapter
*
* @return string title
*/
public function getTitle()
{
return $this->safeGet('title');
}
}
74 changes: 74 additions & 0 deletions src/Entity/TimeTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Bookboon\Api\Entity;

use Bookboon\Api\Bookboon;
use Bookboon\Api\Client\BookboonResponse;
use Bookboon\Api\Client\ClientInterface;
use Bookboon\Api\Exception\BadUUIDException;

class TimeTag extends Entity
{
/**
* @param Bookboon $bookboon
* @param string $authorId
* @return BookboonResponse<TimeTag>
* @throws BadUUIDException
* @throws \Bookboon\Api\Exception\EntityDataException
* @throws \Bookboon\Api\Exception\UsageException
*/
public static function getByBookId(Bookboon $bookboon, string $bookId) : BookboonResponse
{
if (Entity::isValidUUID($bookId) === false) {
throw new BadUUIDException();
}

$bResponse = $bookboon->rawRequest(
"/v1/books/$bookId/timetags",
[],
ClientInterface::HTTP_GET,
true,
TimeTag::class
);

$bResponse->setEntityStore(
new EntityStore(static::getEntitiesFromArray($bResponse->getReturnArray()), TimeTag::class)
);

return $bResponse;
}

protected function isValid(array $array) : bool
{
return isset($array['_id'], $array['offset'], $array['name']);
}


/**
* @return string UUID of entity
*/
public function getId() : string
{
return $this->safeGet('_id');
}

/**
* Return offset of the timetag.
*
* @return int offset
*/
public function getOffset() : int
{
return $this->safeGet('offset');
}

/**
* Return name of the chapter
*
* @return string name
*/
public function getName()
{
return $this->safeGet('name');
}
}

0 comments on commit 3856e39

Please sign in to comment.