Skip to content

Commit

Permalink
Adds support for the Standard card type and cleans up the rendering i…
Browse files Browse the repository at this point in the history
…n OutputSpeech
  • Loading branch information
Pete Burkindine committed Mar 11, 2017
1 parent 21a8ceb commit d95207e
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 25 deletions.
148 changes: 134 additions & 14 deletions src/Response/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,50 @@
*/
class Card
{
// Constants

const CARD_TYPE_SIMPLE = 'Simple';
const CARD_TYPE_STANDARD = 'Standard';
const CARD_TYPE_LINK_ACCOUNT = 'LinkAccount';

const ERROR_INVALID_CARD_TYPE = 'The card type \'%s\' is not one of the valid options';

// Fields

protected $type = 'Simple';
protected $title = '';
protected $content = '';
/**
* @var array
*/
public static $validCardTypes = [
self::CARD_TYPE_SIMPLE,
self::CARD_TYPE_STANDARD,
self::CARD_TYPE_LINK_ACCOUNT
];

/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $title;
/**
* @var string
*/
protected $simpleCardContent;
/**
* @var string
*/
protected $standardCardText;
/**
* @var string
*/
protected $smallImageUrl;
/**
* @var string
*/
protected $largeImageUrl;


// Public Methods

Expand All @@ -24,11 +63,36 @@ class Card
*/
public function render()
{
return [
'type' => $this->type,
'title' => $this->title,
'content' => $this->content,
];
$cardType = $this->getType();

switch ($cardType) {
case self::CARD_TYPE_SIMPLE:
return [
'type' => $cardType,
'title' => $this->getTitle(),
'content' => $this->getSimpleCardContent()
];
case self::CARD_TYPE_STANDARD:
return [
'type' => $cardType,
'title' => $this->getTitle(),
'text' => $this->getStandardCardText(),
'image' => [
'smallImageUrl' => $this->getSmallImageUrl(),
'largeImageUrl' => $this->getLargeImageUrl()
]
];
case self::CARD_TYPE_LINK_ACCOUNT:
return [
'type' => $cardType
];
default:
throw new \InvalidArgumentException(
sprintf(self::ERROR_INVALID_CARD_TYPE, $cardType)
);
}


}

// Accessors
Expand All @@ -52,34 +116,90 @@ public function getTitle()
/**
* @return string
*/
public function getContent()
public function getSimpleCardContent()
{
return $this->simpleCardContent;
}

/**
* @return string
*/
public function getStandardCardText()
{
return $this->content;
return $this->standardCardText;
}

/**
* @return string
*/
public function getSmallImageUrl()
{
return $this->smallImageUrl;
}

/**
* @return string
*/
public function getLargeImageUrl()
{
return $this->largeImageUrl;
}

// Mutators

/**
* @param string $type
*
* @throws \InvalidArgumentException - If the card type is not in self::$validCardTypes
*/
public function setType($type)
{
$this->type = $type;
if (!in_array($type, self::$validCardTypes)) {
throw new \InvalidArgumentException(
sprintf(self::ERROR_INVALID_CARD_TYPE, $type)
);
}

$this->type = (string)$type;
}

/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
$this->title = (string)$title;
}

/**
* @param string $content
*/
public function setContent($content)
public function setSimpleCardContent($content)
{
$this->simpleCardContent = (string)$content;
}

/**
* @param string $standardCardText
*/
public function setStandardCardText($standardCardText)
{
$this->standardCardText = (string)$standardCardText;
}

/**
* @param string $smallImageUrl
*/
public function setSmallImageUrl($smallImageUrl)
{
$this->smallImageUrl = (string)$smallImageUrl;
}

/**
* @param string $largeImageUrl
*/
public function setLargeImageUrl($largeImageUrl)
{
$this->content = $content;
$this->largeImageUrl = (string)$largeImageUrl;
}
}
40 changes: 32 additions & 8 deletions src/Response/OutputSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ class OutputSpeech
const TYPE_TEXT = 'PlainText';
const TYPE_SSML = 'SSML';

const ERROR_INVALID_SPEECH_TYPE = 'The provided speech type \'%s\' is not one of the valid options.';

// Fields

/**
* @var array
*/
public static $validSpeechTypes = [
self::TYPE_TEXT,
self::TYPE_SSML
];

/**
* @var string
*/
Expand All @@ -38,17 +48,23 @@ class OutputSpeech
*/
public function render()
{
switch($this->type) {
$speechType = $this->getType();

switch($speechType) {
case self::TYPE_TEXT:
return [
'type' => $this->type,
'text' => $this->text
'type' => $this->getType(),
'text' => $this->getText()
];
case self::TYPE_SSML:
return [
'type' => $this->type,
'ssml' => $this->ssml
'type' => $this->getType(),
'ssml' => $this->getSsml()
];
default:
throw new \InvalidArgumentException(
sprintf(self::ERROR_INVALID_SPEECH_TYPE, $speechType)
);
}
}

Expand Down Expand Up @@ -82,25 +98,33 @@ public function getSsml()

/**
* @param string $type
*
* @throws \InvalidArgumentException - If the speech type is not in self::$validSpeechTypes
*/
public function setType($type)
{
$this->type = $type;
if (!in_array($type, self::$validSpeechTypes)) {
throw new \InvalidArgumentException(
sprintf(self::ERROR_INVALID_SPEECH_TYPE, $type)
);
}

$this->type = (string)$type;
}

/**
* @param string $text
*/
public function setText($text)
{
$this->text = $text;
$this->text = (string)$text;
}

/**
* @param string $ssml
*/
public function setSsml($ssml)
{
$this->ssml = $ssml;
$this->ssml = (string)$ssml;
}
}
46 changes: 43 additions & 3 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,62 @@ public function repromptSSML($ssml)
}

/**
* Add card information
* withCard()
*
* Create a SimpleCard
*
* @param string $title
* @param string $content
*
* @return \Alexa\Response\Response
*/
public function withCard($title, $content = '')
public function withCard($title, $content)
{
$this->card = new Card;
$this->card->setTitle($title);
$this->card->setContent($content);
$this->card->setSimpleCardContent($content);

return $this;
}

/**
* withStandardCard()
*
* Create a StandardCard with image URLs
*
* @param string $title
* @param $cardText
* @param $smallImageUrl
* @param $largeImageUrl
*
* @return Response
*/
public function withStandardCard($title, $cardText, $smallImageUrl, $largeImageUrl)
{
$this->card = new Card;
$this->card->setTitle($title);
$this->card->setStandardCardText($cardText);
$this->card->setSmallImageUrl($smallImageUrl);
$this->card->setLargeImageUrl($largeImageUrl);

return $this;
}

/**
* withLinkAccountCard()
*
* Create a LinkAccount card
*
* @return Response
*/
public function withLinkAccountCard()
{
$this->card = new Card;
$this->card->setType(Card::CARD_TYPE_LINK_ACCOUNT);

return $this;
}

/**
* Set if it should end the session
*
Expand Down

0 comments on commit d95207e

Please sign in to comment.