Skip to content

Commit

Permalink
Template retrieval now supports fetching specific orientation templat…
Browse files Browse the repository at this point in the history
…es for wall art.
  • Loading branch information
MBriedis committed Oct 10, 2019
1 parent 5d905a9 commit 1379269
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/PrintfulMockupGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public function groupPrintfiles(ProductPrintfiles $productPrintfiles)
* Create an asynchronous generation task and return task that is in pending state
* To retrieve the generation result use <b>PrintfulMockupGenerator::getGenerationTask</b> method.
*
* @see PrintfulMockupGenerator::getGenerationTask
* @param MockupGenerationParameters $parameters
* @return GenerationResultItem Pending task
* @throws Exceptions\PrintfulException
* @see PrintfulMockupGenerator::getGenerationTask
*/
public function createGenerationTask(MockupGenerationParameters $parameters)
{
Expand Down Expand Up @@ -185,12 +185,20 @@ private function parametersToArray(MockupGenerationParameters $parameters)
* This includes background images and area positions.
*
* @param int $productId
* @param string|null $orientation Used for wall art only {@see TemplateItem::ORIENTATION_HORIZONTAL} {@see TemplateItem::ORIENTATION_VERTICAL}
* @return ProductTemplates
* @throws Exceptions\PrintfulApiException
* @throws Exceptions\PrintfulException
*/
public function getProductTemplates($productId)
public function getProductTemplates($productId, $orientation = null)
{
$response = $this->printfulClient->get('/mockup-generator/templates/' . $productId);
$query = [];

if ($orientation) {
$query['orientation'] = $orientation;
}

$response = $this->printfulClient->get('/mockup-generator/templates/' . $productId, $query);

$templates = new ProductTemplates;
$templates->version = (int)$response['version'];
Expand Down
19 changes: 19 additions & 0 deletions src/Structures/Generator/Templates/TemplateItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@

class TemplateItem extends BaseItem
{
/**
* Horizontal template orientation (used for wall art)
*/
const ORIENTATION_HORIZONTAL = 'horizontal';
/**
* Any template orientation (used for wall art, usually means that the template is a square)
*/
const ORIENTATION_ANY = 'any';
/**
* Vertical template orientation (used for wall art)
*/
const ORIENTATION_VERTICAL = 'vertical';

/**
* Template's id
* @var int
Expand Down Expand Up @@ -84,6 +97,11 @@ class TemplateItem extends BaseItem
*/
public $printAreaLeft;

/**
* @var string
*/
public $orientation;

/**
* @param array $raw
* @return TemplateItem
Expand All @@ -104,6 +122,7 @@ public static function fromArray(array $raw)
$item->printAreaHeight = (int)$raw['print_area_height'];
$item->printAreaTop = (int)$raw['print_area_top'];
$item->printAreaLeft = (int)$raw['print_area_left'];
$item->orientation = (string)$raw['orientation'];

return $item;
}
Expand Down
35 changes: 34 additions & 1 deletion tests/MockupGenerator/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Printful\PrintfulMockupGenerator;
use Printful\Structures\Generator\Templates\ProductTemplates;
use Printful\Structures\Generator\Templates\TemplateItem;
use Printful\Tests\TestCase;

class TemplateTest extends TestCase
Expand All @@ -23,6 +24,38 @@ public function testTemplateRetrieval()
self::assertNotEmpty($templates->variantMapping);
self::assertGreaterThan(0, $templates->version);
self::assertGreaterThan(0, $templates->minDpi);
self::assertCount(2, $templates->placementConflicts, 'Two placement conflicts exist (outside label, back)');
self::assertCount(3, $templates->placementConflicts, 'Two placement conflicts exist (outside label, inside label, back)');
}

public function testVerticalTemplateRetrieval()
{
$generator = new PrintfulMockupGenerator($this->api);
$productId = 1; // Poster

$templates = $generator->getProductTemplates($productId, TemplateItem::ORIENTATION_VERTICAL);

foreach($templates->templates as $template){
self::assertContains(
$template->orientation,
[TemplateItem::ORIENTATION_VERTICAL, TemplateItem::ORIENTATION_ANY],
'Vertical or any orientation'
);
}
}

public function testHorizontalTemplateRetrieval()
{
$generator = new PrintfulMockupGenerator($this->api);
$productId = 1; // Poster

$templates = $generator->getProductTemplates($productId, TemplateItem::ORIENTATION_HORIZONTAL);

foreach($templates->templates as $template){
self::assertContains(
$template->orientation,
[TemplateItem::ORIENTATION_HORIZONTAL, TemplateItem::ORIENTATION_ANY],
'Horizontal or any orientation'
);
}
}
}

0 comments on commit 1379269

Please sign in to comment.