Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Dynamic Entities #80

Merged
merged 2 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ composer.phar
/vendor/
composer.lock
.php_cs.cache
/.idea/
.phpunit.result.cache
32 changes: 32 additions & 0 deletions src/Response/Directives/Dialog/Entity/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity;

class Type
{
/**
* @var string
*/
public $name;

/**
* @var TypeValue[]
*/
public $values;

/**
* @param string $name
* @param TypeValue[] $values
*
* @return Type
*/
public static function create(string $name, array $values): self
{
$type = new self();

$type->name = $name;
$type->values = $values;

return $type;
}
}
53 changes: 53 additions & 0 deletions src/Response/Directives/Dialog/Entity/TypeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity;

class TypeValue implements \JsonSerializable
{
/**
* @var string
*/
public $id;

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

/**
* @var array
*/
public $synonyms;

/**
* @param string $id
* @param string $value
* @param array $synonyms
*
* @return TypeValue
*/
public static function create(string $id, string $value, array $synonyms = []): self
{
$typeValue = new self();

$typeValue->id = $id;
$typeValue->value = $value;
$typeValue->synonyms = $synonyms;

return $typeValue;
}

/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'id' => $this->id,
'name' => [
'value' => $this->value,
'synonyms' => $this->synonyms,
],
];
}
}
21 changes: 21 additions & 0 deletions src/Response/Directives/Dialog/UpdateDynamicEntities/Clear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MaxBeckers\AmazonAlexa\Response\Directives\Dialog\UpdateDynamicEntities;

class Clear extends UpdateDynamicEntities
{
const UPDATE_BEHAVIOR = 'CLEAR';

/**
* @return Clear
*/
public static function create(): self
{
$directive = new static();

$directive->type = static::TYPE;
$directive->updateBehavior = static::UPDATE_BEHAVIOR;

return $directive;
}
}
37 changes: 37 additions & 0 deletions src/Response/Directives/Dialog/UpdateDynamicEntities/Replace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace MaxBeckers\AmazonAlexa\Response\Directives\Dialog\UpdateDynamicEntities;

use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity\Type;

class Replace extends UpdateDynamicEntities
{
const UPDATE_BEHAVIOR = 'REPLACE';

/**
* @var array | null
*/
public $types;

/**
* @param Type $type
*/
public function addType(Type $type)
{
$this->types[] = $type;
}

/**
* @return Replace
*/
public static function create(): self
{
$directive = new static();

$directive->type = self::TYPE;
$directive->types = [];
$directive->updateBehavior = static::UPDATE_BEHAVIOR;

return $directive;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MaxBeckers\AmazonAlexa\Response\Directives\Dialog\UpdateDynamicEntities;

use MaxBeckers\AmazonAlexa\Response\Directives\Directive;

abstract class UpdateDynamicEntities extends Directive
{
const TYPE = 'Dialog.UpdateDynamicEntities';

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

/**
* @var string
*/
public $updateBehavior;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"type":"Dialog.UpdateDynamicEntities",
"updateBehavior":"REPLACE",
"types":[
{
"name":"AirportSlotType",
"values":[
{
"id":"LGA",
"name":{
"value":"LaGuardia Airport",
"synonyms":[
"New York"
]
}
}
]
}
]
}
23 changes: 23 additions & 0 deletions test/Test/Response/Data/directive_entity_type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name":"AirportSlotType",
"values":[
{
"id":"BOS",
"name":{
"value":"Logan International Airport",
"synonyms":[
"Boston Logan"
]
}
},
{
"id":"LGA",
"name":{
"value":"LaGuardia Airport",
"synonyms":[
"New York"
]
}
}
]
}
9 changes: 9 additions & 0 deletions test/Test/Response/Data/directive_entity_type_value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id":"BOS",
"name":{
"value":"Logan International Airport",
"synonyms":[
"Boston Logan"
]
}
}
24 changes: 24 additions & 0 deletions test/Test/Response/Directives/Dialog/Entity/TypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\Dialog\Entity;

use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity\Type;
use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity\TypeValue;
use PHPUnit\Framework\TestCase;

class TypeTest extends TestCase
{
public function testCreate()
{
$type = Type::create('AirportSlotType', [
TypeValue::create('BOS', 'Logan International Airport', ['Boston Logan']),
TypeValue::create('LGA', 'LaGuardia Airport', ['New York']),
]);

$json = \file_get_contents(__DIR__.'/../../../../Response/Data/directive_entity_type.json');
$expected = \json_encode(\json_decode($json));
$actual = \json_encode($type);

$this->assertSame($expected, $actual);
}
}
20 changes: 20 additions & 0 deletions test/Test/Response/Directives/Dialog/Entity/TypeValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\Dialog\Entity;

use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity\TypeValue;
use PHPUnit\Framework\TestCase;

class TypeValueTest extends TestCase
{
public function testCreate()
{
$typeValue = TypeValue::create('BOS', 'Logan International Airport', ['Boston Logan']);

$json = \file_get_contents(__DIR__.'/../../../../Response/Data/directive_entity_type_value.json');
$expected = \json_encode(\json_decode($json));
$actual = \json_encode($typeValue);

$this->assertSame($expected, $actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\Dialog\UpdateDynamicEntities;

use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\UpdateDynamicEntities\Clear;
use PHPUnit\Framework\TestCase;

class ClearTest extends TestCase
{
public function testCreate()
{
/** @var Clear $directive */
$directive = Clear::create();
$this->assertSame('Dialog.UpdateDynamicEntities', $directive->type);
$this->assertSame('CLEAR', $directive->updateBehavior);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\Dialog\UpdateDynamicEntities;

use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity\Type;
use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\Entity\TypeValue;
use MaxBeckers\AmazonAlexa\Response\Directives\Dialog\UpdateDynamicEntities\Replace;
use PHPUnit\Framework\TestCase;

class ReplaceTest extends TestCase
{
public function testCreateWithReplaceBehaviour()
{
$type = Type::create('AirportSlotType', [
TypeValue::create('LGA', 'LaGuardia Airport', ['New York']),
]);

/** @var Replace $directive */
$directive = Replace::create();
$directive->addType($type);
$this->assertInternalType('array', $directive->types);
$this->assertSame([$type], $directive->types);
$this->assertSame('Dialog.UpdateDynamicEntities', $directive->type);
$this->assertSame('REPLACE', $directive->updateBehavior);

$json = \file_get_contents(__DIR__.'/../../../../Response/Data/directive_dialog_update_dynamic_entities_replace.json');
$expected = \json_decode($json);

$this->assertSame($expected->type, $directive->type);
$this->assertSame($expected->updateBehavior, $directive->updateBehavior);
$this->assertSame(\count($expected->types), \count($directive->types));
$this->assertSame(\json_encode($expected->types), \json_encode($directive->types));
}
}