Skip to content

Commit

Permalink
Merge pull request #144 from tigranmaestro/master
Browse files Browse the repository at this point in the history
Fix & Update `template` Examples (Part 2)
  • Loading branch information
emarukyan authored Jul 11, 2020
2 parents 64ccfcf + 624cbce commit 020b166
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 61 deletions.
3 changes: 1 addition & 2 deletions examples/templates/get-template-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
18 changes: 16 additions & 2 deletions examples/templates/get-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
27 changes: 0 additions & 27 deletions examples/templates/get-templates-categories.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Template/Category/Collection/CategoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Renderforest\Template\Category\Collection;

use Exception;
use Renderforest\Base\CollectionBase;
use Renderforest\Template\Category\Category;

Expand Down Expand Up @@ -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(
Expand Down
26 changes: 12 additions & 14 deletions src/Template/CategoryExtended/CategoryExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
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,
];

/** @var int */
protected $id;

/** @var int */
protected $projectId;
protected $parentId;

/** @var string */
protected $title;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
}
85 changes: 85 additions & 0 deletions src/Template/Duration/Collection/DurationCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Renderforest\Template\Duration\Collection;

use Renderforest\Base\CollectionBase;
use Renderforest\Template\Duration\Duration;

/**
* Class DurationCollection
* @package Renderforest\Template\Duration\Collection
*/
class DurationCollection extends CollectionBase
{
/**
* @var Duration[]
*/
private $durations;

/**
* @var Duration[]
*/
private $durationsAssocArray;

/**
* DurationCollection constructor.
*/
public function __construct()
{
$this->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;
}
}
Loading

0 comments on commit 020b166

Please sign in to comment.