From 624cbcef14ff11b29f914024bae23a1d06bbcac6 Mon Sep 17 00:00:00 2001 From: Tigran Petrosyan Date: Sat, 11 Jul 2020 22:40:42 +0400 Subject: [PATCH] Fix & Update `template` Examples (Part 2) * Fix/Update `get-template` example * Fix/Update `get-template-categories` example * Remove unused `get-templates-categories` example --- .../templates/get-template-categories.php | 3 +- examples/templates/get-template.php | 18 +- .../templates/get-templates-categories.php | 27 --- src/ApiClient.php | 5 +- .../Collection/CategoryCollection.php | 5 +- .../CategoryExtended/CategoryExtended.php | 26 ++- .../Collection/DurationCollection.php | 85 +++++++++ src/Template/Duration/Duration.php | 175 ++++++++++++++++++ src/Template/TemplateExtended.php | 83 +++++++-- 9 files changed, 366 insertions(+), 61 deletions(-) delete mode 100644 examples/templates/get-templates-categories.php create mode 100644 src/Template/Duration/Collection/DurationCollection.php create mode 100644 src/Template/Duration/Duration.php diff --git a/examples/templates/get-template-categories.php b/examples/templates/get-template-categories.php index 229640f..6fc83a5 100644 --- a/examples/templates/get-template-categories.php +++ b/examples/templates/get-template-categories.php @@ -2,7 +2,7 @@ require '../../vendor/autoload.php'; -$templateCategories = \Renderforest\ApiClient::getTemplateCategories('ru'); +$templateCategories = \Renderforest\ApiClient::getTemplateCategories('en'); echo 'Count - ' . count($templateCategories) . PHP_EOL; echo PHP_EOL; @@ -14,7 +14,6 @@ foreach ($templateCategory->getSubCategoriesCollection() as $templateSubCategory) { echo '-- ' . 'ID - ' . $templateSubCategory->getId() . PHP_EOL; - echo '-- ' . 'Parent ID - ' . $templateSubCategory->getParentId() . PHP_EOL; echo '-- ' . 'Title - ' . $templateSubCategory->getTitle() . PHP_EOL; } diff --git a/examples/templates/get-template.php b/examples/templates/get-template.php index e2d7f96..3e3949a 100644 --- a/examples/templates/get-template.php +++ b/examples/templates/get-template.php @@ -2,7 +2,7 @@ require '../../vendor/autoload.php'; -$template = \Renderforest\ApiClient::getTemplate(701, 'ru'); +$template = \Renderforest\ApiClient::getTemplate(1445, 'en'); echo 'ID - ' . $template->getId() . PHP_EOL; echo 'Description - ' . $template->getDescription() . PHP_EOL; @@ -19,10 +19,24 @@ foreach ($template->getCategories() as $category) { echo '-- ID - ' . $category->getId() . PHP_EOL; - echo '-- Project ID - ' . $category->getProjectId() . PHP_EOL; + echo '-- Parent ID - ' . $category->getParentId() . PHP_EOL; echo '-- Title - ' . $category->getTitle() . PHP_EOL; echo PHP_EOL; } echo PHP_EOL; + +echo 'Durations:' . PHP_EOL; + +foreach ($template->getDurations() as $duration) { + echo '-- Template ID - ' . $duration->getTemplateId() . PHP_EOL; + echo '-- Duration - ' . $duration->getDuration() . PHP_EOL; + echo '-- Name - ' . $duration->getName() . PHP_EOL; + echo '-- Link Name - ' . $duration->getLinkName() . PHP_EOL; + echo '-- Video URL - ' . $duration->getVideoUrl() . PHP_EOL; + + echo PHP_EOL; +} + +echo PHP_EOL; diff --git a/examples/templates/get-templates-categories.php b/examples/templates/get-templates-categories.php deleted file mode 100644 index 2372f45..0000000 --- a/examples/templates/get-templates-categories.php +++ /dev/null @@ -1,27 +0,0 @@ -getTemplatesCategories('ru'); - -echo 'Count - ' . count($templateCategories) . PHP_EOL; -echo PHP_EOL; - -foreach ($templateCategories as $templateCategory) { - echo 'ID - ' . $templateCategory->getId() . PHP_EOL; - echo 'Parent ID - ' . $templateCategory->getParentId() . PHP_EOL; - echo 'Title - ' . $templateCategory->getTitle() . PHP_EOL; - - foreach ($templateCategory->getSubCategoriesCollection()->getItems() as $templateSubCategory) { - echo '-- ' . 'ID - ' . $templateSubCategory->getId() . PHP_EOL; - echo '-- ' . 'Parent ID - ' . $templateSubCategory->getParentId() . PHP_EOL; - echo '-- ' . 'Title - ' . $templateSubCategory->getTitle() . PHP_EOL; - } - - echo PHP_EOL; -} diff --git a/src/ApiClient.php b/src/ApiClient.php index 337ffe8..0fea2fe 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -11,6 +11,7 @@ use Renderforest\Sound\Collection\SoundCollection; use Renderforest\Support\SupportTicket; use Renderforest\Support\SupportTicketResponse; +use Renderforest\Template\Category\Category; use Renderforest\Template\Category\Collection\CategoryCollection; use Renderforest\Template\Collection\TemplateCollection; use Renderforest\Template\ColorPreset\Collection\ColorPresetCollection; @@ -481,10 +482,10 @@ public function renderProject( * @todo validate language code, supported codes - ar, de, en, es, fr, pt, ru, tr * * @param string|null $languageIsoCode - * @return CategoryCollection + * @return CategoryCollection|Category[] * @throws GuzzleException */ - public static function getTemplatesCategories(string $languageIsoCode = null): CategoryCollection + public static function getTemplateCategories(string $languageIsoCode = null): CategoryCollection { $endpoint = self::TEMPLATE_CATEGORIES_API_PATH; $uri = self::API_ENDPOINT . self::TEMPLATE_CATEGORIES_API_PATH; diff --git a/src/Template/Category/Collection/CategoryCollection.php b/src/Template/Category/Collection/CategoryCollection.php index aef2f97..8339b65 100644 --- a/src/Template/Category/Collection/CategoryCollection.php +++ b/src/Template/Category/Collection/CategoryCollection.php @@ -2,6 +2,7 @@ namespace Renderforest\Template\Category\Collection; +use Exception; use Renderforest\Base\CollectionBase; use Renderforest\Template\Category\Category; @@ -87,9 +88,9 @@ public function getArrayCopy(): array /** * @param int $categoryId * @return Category - * @throws \Exception + * @throws Exception */ - public function getCategoryById(int $categoryId) + public function getCategoryById(int $categoryId): Category { if (false === array_key_exists($categoryId, $this->categoriesAssocArray)) { throw new \Exception( diff --git a/src/Template/CategoryExtended/CategoryExtended.php b/src/Template/CategoryExtended/CategoryExtended.php index 5c3ac02..d65acc1 100644 --- a/src/Template/CategoryExtended/CategoryExtended.php +++ b/src/Template/CategoryExtended/CategoryExtended.php @@ -11,12 +11,12 @@ class CategoryExtended extends EntityBase { const KEY_ID = 'id'; - const KEY_PROJECT_ID = 'projectId'; + const KEY_PARENT_ID = 'parentId'; const KEY_TITLE = 'title'; const REQUIRED_KEYS = [ self::KEY_ID, - self::KEY_PROJECT_ID, + self::KEY_PARENT_ID, self::KEY_TITLE, ]; @@ -24,7 +24,7 @@ class CategoryExtended extends EntityBase protected $id; /** @var int */ - protected $projectId; + protected $parentId; /** @var string */ protected $title; @@ -48,17 +48,17 @@ private function setId(int $id) /** * @return int */ - public function getProjectId(): int + public function getParentId(): int { - return $this->projectId; + return $this->parentId; } /** - * @param int $projectId + * @param int $parentId */ - private function setProjectId(int $projectId) + private function setParentId(int $parentId) { - $this->projectId = $projectId; + $this->parentId = $parentId; } /** @@ -89,11 +89,11 @@ public function exchangeArray(array $categoryArrayData) } $categoryId = $categoryArrayData[self::KEY_ID]; - $categoryProjectId = $categoryArrayData[self::KEY_PROJECT_ID]; + $categoryParentId = $categoryArrayData[self::KEY_PARENT_ID]; $categoryTitle = $categoryArrayData[self::KEY_TITLE]; $this->setId($categoryId); - $this->setProjectId($categoryProjectId); + $this->setParentId($categoryParentId); $this->setTitle($categoryTitle); } @@ -102,12 +102,10 @@ public function exchangeArray(array $categoryArrayData) */ public function getArrayCopy(): array { - $arrayCopy = [ + return [ self::KEY_ID => $this->getId(), - self::KEY_PROJECT_ID => $this->getProjectId(), + self::KEY_PARENT_ID => $this->getParentId(), self::KEY_TITLE => $this->getTitle(), ]; - - return $arrayCopy; } } diff --git a/src/Template/Duration/Collection/DurationCollection.php b/src/Template/Duration/Collection/DurationCollection.php new file mode 100644 index 0000000..1c0db5e --- /dev/null +++ b/src/Template/Duration/Collection/DurationCollection.php @@ -0,0 +1,85 @@ +durations = []; + $this->durationsAssocArray = []; + + parent::__construct(); + } + + /** + * @param Duration $duration + * @return DurationCollection + */ + private function add(Duration $duration): DurationCollection + { + $this->iteratorItems[] = $duration; + $this->durations[] = $duration; + + return $this; + } + + /** + * @param string $durationCollectionJson + */ + public function exchangeJson(string $durationCollectionJson) + { + $durationCollectionArrayData = json_decode($durationCollectionJson, true); + + $durationCollectionArrayData = $durationCollectionArrayData['data']; + + $this->exchangeArray($durationCollectionArrayData); + } + + /** + * @param array $durationCollectionArrayData + */ + public function exchangeArray(array $durationCollectionArrayData) + { + foreach ($durationCollectionArrayData as $durationArrayData) { + $duration = new Duration(); + $duration->exchangeArray($durationArrayData); + + $this->add($duration); + } + } + + /** + * @return array + */ + public function getArrayCopy(): array + { + $arrayCopy = []; + + foreach ($this->durations as $duration) { + $arrayCopy[] = $duration->getArrayCopy(); + } + + return $arrayCopy; + } +} diff --git a/src/Template/Duration/Duration.php b/src/Template/Duration/Duration.php new file mode 100644 index 0000000..cb866b3 --- /dev/null +++ b/src/Template/Duration/Duration.php @@ -0,0 +1,175 @@ +templateId; + } + + /** + * @param int $templateId + * @return Duration + */ + public function setTemplateId(int $templateId): Duration + { + $this->templateId = $templateId; + + return $this; + } + + /** + * @return int + */ + public function getDuration(): int + { + return $this->duration; + } + + /** + * @param int $duration + * @return Duration + */ + public function setDuration(int $duration): Duration + { + $this->duration = $duration; + + return $this; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + * @return Duration + */ + public function setName(string $name): Duration + { + $this->name = $name; + + return $this; + } + + /** + * @return string + */ + public function getLinkName(): string + { + return $this->linkName; + } + + /** + * @param string $linkName + * @return Duration + */ + public function setLinkName(string $linkName): Duration + { + $this->linkName = $linkName; + + return $this; + } + + /** + * @return string + */ + public function getVideoUrl(): string + { + return $this->videoUrl; + } + + /** + * @param string $videoUrl + * @return Duration + */ + public function setVideoUrl(string $videoUrl): Duration + { + $this->videoUrl = $videoUrl; + + return $this; + } + + /** + * @param array $durationArrayData + */ + public function exchangeArray(array $durationArrayData) + { + foreach (self::REQUIRED_KEYS as $requiredKey) { + if (false === array_key_exists($requiredKey, $durationArrayData)) { + // throw exception + } + } + + $durationTemplateId = $durationArrayData[self::KEY_TEMPLATE_ID]; + $durationDuration = $durationArrayData[self::KEY_DURATION]; + $durationName = $durationArrayData[self::KEY_NAME]; + $durationLinkName = $durationArrayData[self::KEY_LINK_NAME]; + $durationVideoUrl = $durationArrayData[self::KEY_VIDEO_URL]; + + $this + ->setTemplateId($durationTemplateId) + ->setDuration($durationDuration) + ->setName($durationName) + ->setLinkName($durationLinkName) + ->setVideoUrl($durationVideoUrl); + } + + /** + * @return array + */ + public function getArrayCopy(): array + { + return [ + self::KEY_TEMPLATE_ID => $this->getTemplateId(), + self::KEY_DURATION => $this->getDuration(), + self::KEY_NAME => $this->getName(), + self::KEY_LINK_NAME => $this->getLinkName(), + self::KEY_VIDEO_URL => $this->getVideoUrl(), + ]; + } +} diff --git a/src/Template/TemplateExtended.php b/src/Template/TemplateExtended.php index a939f9c..e053005 100644 --- a/src/Template/TemplateExtended.php +++ b/src/Template/TemplateExtended.php @@ -3,7 +3,10 @@ namespace Renderforest\Template; use Renderforest\Base\EntityBase; +use Renderforest\Template\CategoryExtended\CategoryExtended; use Renderforest\Template\CategoryExtended\Collection\CategoryExtendedCollection; +use Renderforest\Template\Duration\Collection\DurationCollection; +use Renderforest\Template\Duration\Duration; /** * Class TemplateExtended @@ -25,8 +28,6 @@ class TemplateExtended extends EntityBase * } */ - - const KEY_ID = 'id'; const KEY_DESCRIPTION = 'description'; const KEY_DURATION = 'duration'; @@ -36,6 +37,7 @@ class TemplateExtended extends EntityBase const KEY_VIDEO_URL = 'videoUrl'; const KEY_VIDEO = 'video'; const KEY_CATEGORIES = 'categories'; + const KEY_DURATIONS = 'durations'; const REQUIRED_KEYS = [ self::KEY_ID, @@ -47,6 +49,7 @@ class TemplateExtended extends EntityBase self::KEY_VIDEO_URL, self::KEY_VIDEO, self::KEY_CATEGORIES, + self::KEY_DURATIONS, ]; /** @var int */ @@ -76,6 +79,9 @@ class TemplateExtended extends EntityBase /** @var CategoryExtendedCollection */ protected $categories; + /** @var DurationCollection */ + protected $durations; + /** * @return int */ @@ -86,10 +92,13 @@ public function getId(): int /** * @param int $id + * @return TemplateExtended */ - private function setId(int $id) + private function setId(int $id): TemplateExtended { $this->id = $id; + + return $this; } /** @@ -102,10 +111,13 @@ public function getDescription(): string /** * @param string $description + * @return TemplateExtended */ - private function setDescription(string $description) + private function setDescription(string $description): TemplateExtended { $this->description = $description; + + return $this; } /** @@ -118,10 +130,13 @@ public function getDuration(): int /** * @param int $duration + * @return TemplateExtended */ - private function setDuration(int $duration) + private function setDuration(int $duration): TemplateExtended { $this->duration = $duration; + + return $this; } /** @@ -134,10 +149,13 @@ public function getRendCount(): int /** * @param int $rendCount + * @return TemplateExtended */ - private function setRendCount(int $rendCount) + private function setRendCount(int $rendCount): TemplateExtended { $this->rendCount = $rendCount; + + return $this; } /** @@ -150,10 +168,13 @@ public function getThumbnail(): string /** * @param string $thumbnail + * @return TemplateExtended */ - private function setThumbnail(string $thumbnail) + private function setThumbnail(string $thumbnail): TemplateExtended { $this->thumbnail = $thumbnail; + + return $this; } /** @@ -166,10 +187,13 @@ public function getTitle(): string /** * @param string $title + * @return TemplateExtended */ - private function setTitle(string $title) + private function setTitle(string $title): TemplateExtended { $this->title = $title; + + return $this; } /** @@ -182,10 +206,13 @@ public function getVideoUrl(): string /** * @param string $videoUrl + * @return TemplateExtended */ - private function setVideoUrl(string $videoUrl) + private function setVideoUrl(string $videoUrl): TemplateExtended { $this->videoUrl = $videoUrl; + + return $this; } /** @@ -198,14 +225,17 @@ public function isVideo(): bool /** * @param bool $video + * @return TemplateExtended */ - private function setVideo(bool $video) + private function setVideo(bool $video): TemplateExtended { $this->video = $video; + + return $this; } /** - * @return CategoryExtendedCollection + * @return CategoryExtendedCollection|CategoryExtended[] */ public function getCategories(): CategoryExtendedCollection { @@ -214,10 +244,32 @@ public function getCategories(): CategoryExtendedCollection /** * @param CategoryExtendedCollection $categories + * @return TemplateExtended */ - private function setCategories(CategoryExtendedCollection $categories) + private function setCategories(CategoryExtendedCollection $categories): TemplateExtended { $this->categories = $categories; + + return $this; + } + + /** + * @return DurationCollection|Duration[] + */ + public function getDurations(): DurationCollection + { + return $this->durations; + } + + /** + * @param DurationCollection $durations + * @return TemplateExtended + */ + public function setDurations(DurationCollection $durations): TemplateExtended + { + $this->durations = $durations; + + return $this; } /** @@ -267,6 +319,13 @@ public function exchangeArray(array $templateArrayData) $categoryCollection->exchangeArray($categoriesArrayData); $this->setCategories($categoryCollection); + + $durationsArrayData = $templateArrayData[self::KEY_DURATIONS]; + + $durationCollection = new DurationCollection(); + $durationCollection->exchangeArray($durationsArrayData); + + $this->setDurations($durationCollection); } /**