Skip to content

Commit

Permalink
Merge pull request #84 from maxbeckers/develop
Browse files Browse the repository at this point in the history
Json Handling for Template
  • Loading branch information
maxbeckers authored Aug 28, 2020
2 parents d021979 + 3d948fb commit 749ee48
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [1.9.0](https://github.com/maxbeckers/amazon-alexa-php/tree/1.9.0) (2020-08-28)
[Full Changelog](https://github.com/maxbeckers/amazon-alexa-php/compare/1.8.0...1.9.0)

## [1.8.0](https://github.com/maxbeckers/amazon-alexa-php/tree/1.8.0) (2020-04-15)
[Full Changelog](https://github.com/maxbeckers/amazon-alexa-php/compare/1.7.0...1.8.0)

Expand Down
37 changes: 36 additions & 1 deletion src/Response/Directives/Display/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @author Maximilian Beckers <[email protected]>
*/
class Template
class Template implements \JsonSerializable
{
const BACK_BUTTON_MODE_HIDDEN = 'HIDDEN';
const BACK_BUTTON_MODE_VISIBLE = 'VISIBLE';
Expand Down Expand Up @@ -85,4 +85,39 @@ public static function create($type, $token, $backButton = self::BACK_BUTTON_MOD

return $template;
}

/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
$data = new \ArrayObject();

if (null !== $this->type) {
$data['type'] = $this->type;
}
if (null !== $this->token) {
$data['token'] = $this->token;
}
if (null !== $this->backButton) {
$data['backButton'] = $this->backButton;
}
if (null !== $this->backgroundImage) {
$data['backgroundImage'] = $this->backgroundImage;
}
if (null !== $this->title) {
$data['title'] = $this->title;
}
if (null !== $this->textContent) {
$data['textContent'] = $this->textContent;
}
if (null !== $this->image) {
$data['image'] = $this->image;
}
if (!empty($this->listItems)) {
$data['listItems'] = $this->listItems;
}

return $data;
}
}
55 changes: 55 additions & 0 deletions test/Test/Response/Directives/Display/TemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\Display;

use ArrayObject;
use MaxBeckers\AmazonAlexa\Response\Directives\Display\Image;
use MaxBeckers\AmazonAlexa\Response\Directives\Display\ListItem;
use MaxBeckers\AmazonAlexa\Response\Directives\Display\Template;
use MaxBeckers\AmazonAlexa\Response\Directives\Display\TextContent;
use PHPUnit\Framework\TestCase;

/**
* @author Maximilian Beckers <[email protected]>
*/
class TemplateTest extends TestCase
{
public function testSerializeTypeAndToken()
{
$type = 'BodyTemplate1';
$token = 'token';

$card = Template::create($type, $token);

$this->assertEquals(new ArrayObject([
'type' => $type,
'token' => $token,
'backButton' => Template::BACK_BUTTON_MODE_VISIBLE,
]), $card->jsonSerialize());
}

public function testSerializeAll()
{
$type = 'BodyTemplate1';
$token = 'token';
$backButton = Template::BACK_BUTTON_MODE_VISIBLE;
$backgroundImage = 'background image';
$title = 'title';
$textContent = TextContent::create();
$image = Image::create();
$listItems = [ListItem::create()];

$card = Template::create($type, $token, $backButton, $backgroundImage, $title, $textContent, $image, $listItems);

$this->assertEquals(new ArrayObject([
'type' => $type,
'token' => $token,
'backButton' => $backButton,
'backgroundImage' => $backgroundImage,
'title' => $title,
'textContent' => $textContent,
'image' => $image,
'listItems' => $listItems,
]), $card->jsonSerialize());
}
}

0 comments on commit 749ee48

Please sign in to comment.