-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from maxbeckers/develop
Json Handling for Template
- Loading branch information
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |