diff --git a/README.md b/README.md index 4becf0e..5777a67 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,11 @@ $response = $messenger->sendMessage($message); require_once __DIR__.'/vendor/autoload.php'; -use Tgallice\FBMessenger\Attachment\Structured; +use Tgallice\FBMessenger\Attachment\Structured\Receipt; use Tgallice\FBMessenger\Messenger; use Tgallice\FBMessenger\Message\Message; use Tgallice\FBMessenger\Model\Receipt\Element; use Tgallice\FBMessenger\Model\Summary; -use Tgallice\FBMessenger\Template\Receipt; $messenger = new Messenger('page_token'); @@ -61,8 +60,7 @@ $elements = [ $summary = new Summary(); -$receipt = new Receipt('My Receipt', '123456789', 'EUR', 'Visa', $elements, $summary); -$attachment = new Structured($receipt); +$attachment = new Receipt('My Receipt', '123456789', 'EUR', 'Visa', $elements, $summary); $message = new Message('', $attachment); $response = $messenger->sendMessage($message); @@ -75,10 +73,9 @@ $response = $messenger->sendMessage($message); require_once __DIR__.'/vendor/autoload.php'; -use Tgallice\FBMessenger\Attachment\Structured; +use Tgallice\FBMessenger\Attachment\Structured\Button; use Tgallice\FBMessenger\Messenger; use Tgallice\FBMessenger\Message\Message; -use Tgallice\FBMessenger\Template\Button as ButtonTemplate; use Tgallice\FBMessenger\Model\Button\WebUrl; use Tgallice\FBMessenger\Model\Button\Postback; @@ -89,8 +86,7 @@ $elements = [ new Postback('Button2', 'EVENT_NAME'), ]; -$template = new ButtonTemplate('My template', $elements); -$attachment = new Structured($template); +$attachment = new Button('My template', $elements); $message = new Message('', $attachment); $response = $messenger->sendMessage($message); diff --git a/spec/Attachment/Structured/ButtonSpec.php b/spec/Attachment/Structured/ButtonSpec.php new file mode 100644 index 0000000..0d834b4 --- /dev/null +++ b/spec/Attachment/Structured/ButtonSpec.php @@ -0,0 +1,67 @@ +beConstructedWith('text', [$button]); + } + + function it_is_initializable() + { + $this->shouldHaveType('Tgallice\FBMessenger\Attachment\Structured\Button'); + } + + function it_is_a_structured_attachment() + { + $this->shouldImplement(Structured::class); + } + + function it_should_return_the_type() + { + $this->getType()->shouldReturn(Attachment::TYPE_TEMPLATE); + } + + function it_should_return_the_main_text() + { + $this->getText()->shouldReturn('text'); + } + + function it_should_return_the_buttons($button) + { + $this->getButtons()->shouldReturn([$button]); + } + + function it_should_return_the_payload($button) + { + $this->getPayload()->shouldReturn([ + 'template_type' => Button::TEMPLATE_TYPE, + 'text' => 'text', + 'buttons' => [$button], + ]); + } + + function it_should_be_serializable($button) + { + $this->shouldImplement(\JsonSerializable::class); + + $expected = [ + 'type' => Attachment::TYPE_TEMPLATE, + 'payload' => [ + 'template_type' => Button::TEMPLATE_TYPE, + 'text' => 'text', + 'buttons' => [$button], + ], + ]; + + $this->jsonSerialize()->shouldBeLike($expected); + } +} diff --git a/spec/Template/GenericSpec.php b/spec/Attachment/Structured/GenericSpec.php similarity index 58% rename from spec/Template/GenericSpec.php rename to spec/Attachment/Structured/GenericSpec.php index 9b73d35..3ea58c4 100644 --- a/spec/Template/GenericSpec.php +++ b/spec/Attachment/Structured/GenericSpec.php @@ -1,10 +1,12 @@ shouldHaveType('Tgallice\FBMessenger\Template\Generic'); + $this->shouldHaveType('Tgallice\FBMessenger\Attachment\Structured\Generic'); } - function it_is_a_template() + function it_is_a_structured_attachment() { - $this->shouldImplement(Template::class); + $this->shouldImplement(Structured::class); } function it_should_return_the_type() { - $this->getType()->shouldReturn(Template::TYPE_GENERIC); + $this->getType()->shouldReturn(Attachment::TYPE_TEMPLATE); } function it_should_return_the_elements($element) @@ -33,13 +35,24 @@ function it_should_return_the_elements($element) $this->getElements()->shouldReturn([$element]); } + function it_should_return_the_payload($element) + { + $this->getPayload()->shouldReturn([ + 'template_type' => Generic::TEMPLATE_TYPE, + 'elements' => [$element], + ]); + } + function it_should_be_serializable($element) { $this->shouldImplement(\JsonSerializable::class); $expected = [ - 'template_type' => Template::TYPE_GENERIC, - 'elements' => [$element], + 'type' => Attachment::TYPE_TEMPLATE, + 'payload' => [ + 'template_type' => Generic::TEMPLATE_TYPE, + 'elements' => [$element], + ], ]; $this->jsonSerialize()->shouldBeLike($expected); diff --git a/spec/Template/ReceiptSpec.php b/spec/Attachment/Structured/ReceiptSpec.php similarity index 73% rename from spec/Template/ReceiptSpec.php rename to spec/Attachment/Structured/ReceiptSpec.php index 7b1e0be..5d1ec92 100644 --- a/spec/Template/ReceiptSpec.php +++ b/spec/Attachment/Structured/ReceiptSpec.php @@ -1,13 +1,15 @@ shouldHaveType('Tgallice\FBMessenger\Template\Receipt'); + $this->shouldHaveType('Tgallice\FBMessenger\Attachment\Structured\Receipt'); } - function it_is_a_template() + function it_is_a_structured_attachment() { - $this->shouldImplement(Template::class); + $this->shouldImplement(Structured::class); } function it_should_return_type() { - $this->getType()->shouldReturn(Template::TYPE_RECEIPT); + $this->getType()->shouldReturn(Attachment::TYPE_TEMPLATE); } function it_should_return_the_recipient_name() @@ -106,12 +108,10 @@ function its_adjustments_are_mutable(Adjustment $adjustment) $this->getAdjustments()->shouldReturn([$adjustment]); } - function it_should_be_serializable($element, $summary, Address $address, Adjustment $adjustment) + function it_should_return_the_payload($element, $summary) { - $this->shouldImplement(\JsonSerializable::class); - - $expected = [ - 'template_type' => Template::TYPE_RECEIPT, + $this->getPayload()->shouldReturn([ + 'template_type' => Receipt::TEMPLATE_TYPE, 'recipient_name' => 'recipient', 'order_number' => '1a2b', 'currency' => 'currency', @@ -122,12 +122,35 @@ function it_should_be_serializable($element, $summary, Address $address, Adjustm 'address' => null, 'summary' => $summary, 'adjustments' => [], + ]); + } + + function it_should_be_serializable($element, $summary, Address $address, Adjustment $adjustment) + { + $this->shouldImplement(\JsonSerializable::class); + + $expected = [ + 'type' => Attachment::TYPE_TEMPLATE, + 'payload' => [ + 'template_type' => Receipt::TEMPLATE_TYPE, + 'recipient_name' => 'recipient', + 'order_number' => '1a2b', + 'currency' => 'currency', + 'payment_method' => 'payment_method', + 'timestamp' => null, + 'order_url' => null, + 'elements' => [$element], + 'address' => null, + 'summary' => $summary, + 'adjustments' => [], + ], ]; $this->jsonSerialize()->shouldBeLike($expected); + $time = time(); - $expected = array_merge($expected, [ + $expected['payload'] = array_merge($expected['payload'], [ 'timestamp' => $time, 'order_url' => 'http://order.com', 'address' => $address, diff --git a/spec/Attachment/StructuredSpec.php b/spec/Attachment/StructuredSpec.php deleted file mode 100644 index 1e3e7af..0000000 --- a/spec/Attachment/StructuredSpec.php +++ /dev/null @@ -1,47 +0,0 @@ -beConstructedWith($template); - } - - function it_is_initializable() - { - $this->shouldHaveType('Tgallice\FBMessenger\Attachment\Structured'); - } - - function it_is_a_attachment() - { - $this->shouldImplement('Tgallice\FBMessenger\Attachment'); - } - - function it_should_return_the_type() - { - $this->getType()->shouldReturn(Attachment::TYPE_TEMPLATE); - } - - function it_should_return_the_payload($template) - { - $this->getPayload()->shouldReturn($template); - } - - function it_should_be_serializable($template) - { - $this->shouldImplement(\JsonSerializable::class); - - $expected = [ - 'type' => Attachment::TYPE_TEMPLATE, - 'payload' => $template, - ]; - - $this->jsonSerialize()->shouldBeLike($expected); - } -} diff --git a/spec/Model/Generic/ElementSpec.php b/spec/Model/Generic/ElementSpec.php index 299d541..3543209 100644 --- a/spec/Model/Generic/ElementSpec.php +++ b/spec/Model/Generic/ElementSpec.php @@ -3,7 +3,7 @@ namespace spec\Tgallice\FBMessenger\Model\Generic; use PhpSpec\ObjectBehavior; -use Tgallice\FBMessenger\Template\Button; +use Tgallice\FBMessenger\Model\Button; class ElementSpec extends ObjectBehavior { diff --git a/spec/Template/ButtonSpec.php b/spec/Template/ButtonSpec.php deleted file mode 100644 index ffa6e78..0000000 --- a/spec/Template/ButtonSpec.php +++ /dev/null @@ -1,53 +0,0 @@ -beConstructedWith('text', [$button]); - } - - function it_is_initializable() - { - $this->shouldHaveType('Tgallice\FBMessenger\Template\Button'); - } - - function it_is_a_template() - { - $this->shouldImplement(Template::class); - } - - function it_should_return_the_type() - { - $this->getType()->shouldReturn(Template::TYPE_BUTTON); - } - - function it_should_return_the_main_text() - { - $this->getText()->shouldReturn('text'); - } - - function it_should_return_the_buttons($button) - { - $this->getButtons()->shouldReturn([$button]); - } - - function it_should_be_serializable($button) - { - $this->shouldImplement(\JsonSerializable::class); - - $expected = [ - 'template_type' => Template::TYPE_BUTTON, - 'text' => 'text', - 'buttons' => [$button], - ]; - - $this->jsonSerialize()->shouldBeLike($expected); - } -} diff --git a/src/Attachment.php b/src/Attachment.php index 338a86e..d3f94a3 100644 --- a/src/Attachment.php +++ b/src/Attachment.php @@ -9,25 +9,12 @@ abstract class Attachment implements \JsonSerializable const TYPE_TEMPLATE = 'template'; /** - * @var string - */ - protected $type; - - /** - * @var array|Template + * @var array */ protected $payload; /** - * @inheritdoc - */ - public function getType() - { - return $this->type; - } - - /** - * @return array|Template + * @return array */ public function getPayload() { @@ -40,8 +27,13 @@ public function getPayload() public function jsonSerialize() { return [ - 'type' => $this->type, - 'payload' => $this->payload, + 'type' => $this->getType(), + 'payload' => $this->getPayload(), ]; } + + /** + * @inheritdoc + */ + abstract public function getType(); } diff --git a/src/Attachment/Image.php b/src/Attachment/Image.php index dc1edc1..4d58669 100644 --- a/src/Attachment/Image.php +++ b/src/Attachment/Image.php @@ -23,7 +23,6 @@ class Image extends Attachment public function __construct($filePath) { $this->path = $filePath; - $this->type = Attachment::TYPE_IMAGE; // Local image is sent with a multipart request $this->payload = []; @@ -69,6 +68,14 @@ public function isRemoteFile() return $this->isRemoteFile = preg_match('/^(https?|ftp):\/\/.*/', $this->path) === 1; } + /** + * @inheritdoc + */ + public function getType() + { + return Attachment::TYPE_IMAGE; + } + private function validateFile() { if (!ExtensionChecker::check($this->path, ['jpg', 'png'])) { diff --git a/src/Attachment/Structured.php b/src/Attachment/Structured.php index aa31667..d18f0ba 100644 --- a/src/Attachment/Structured.php +++ b/src/Attachment/Structured.php @@ -3,16 +3,14 @@ namespace Tgallice\FBMessenger\Attachment; use Tgallice\FBMessenger\Attachment; -use Tgallice\FBMessenger\Template; -class Structured extends Attachment +abstract class Structured extends Attachment { /** - * @param Template $template + * @inheritdoc */ - public function __construct(Template $template) + public function getType() { - $this->payload = $template; - $this->type = Attachment::TYPE_TEMPLATE; + return Attachment::TYPE_TEMPLATE; } } diff --git a/src/Attachment/Structured/Button.php b/src/Attachment/Structured/Button.php new file mode 100644 index 0000000..4bdba42 --- /dev/null +++ b/src/Attachment/Structured/Button.php @@ -0,0 +1,40 @@ +payload = [ + 'template_type' => self::TEMPLATE_TYPE, + 'text' => $text, + 'buttons' => $buttons, + ]; + } + + /** + * @return null|string + */ + public function getText() + { + return $this->payload['text']; + } + + /** + * @return array + */ + public function getButtons() + { + return $this->payload['buttons']; + } +} diff --git a/src/Attachment/Structured/Generic.php b/src/Attachment/Structured/Generic.php new file mode 100644 index 0000000..3d657fa --- /dev/null +++ b/src/Attachment/Structured/Generic.php @@ -0,0 +1,36 @@ + 10) { + throw new \InvalidArgumentException('A generic template can not have more than 10 bubbles'); + } + + $this->payload = [ + 'template_type' => self::TEMPLATE_TYPE, + 'elements' => $elements, + ]; + } + + /** + * @return Element[] + */ + public function getElements() + { + return $this->payload['elements']; + } +} diff --git a/src/Attachment/Structured/Receipt.php b/src/Attachment/Structured/Receipt.php new file mode 100644 index 0000000..f0465ad --- /dev/null +++ b/src/Attachment/Structured/Receipt.php @@ -0,0 +1,151 @@ +payload = [ + 'template_type' => self::TEMPLATE_TYPE, + 'recipient_name' => $recipientName, + 'order_number' => $orderNumber, + 'currency' => $currency, + 'payment_method' => $paymentMethod, + 'timestamp' => null, + 'order_url' => null, + 'elements' => $elements, + 'address' => null, + 'summary' => $summary, + 'adjustments' => [], + ]; + } + + /** + * @return null|Address + */ + public function getAddress() + { + return $this->payload['address']; + } + + /** + * @return Adjustment[] + */ + public function getAdjustments() + { + return $this->payload['adjustments']; + } + + /** + * @return Element[] + */ + public function getElements() + { + return $this->payload['elements']; + } + + /** + * @return string + */ + public function getCurrency() + { + return $this->payload['currency']; + } + + /** + * @return string + */ + public function getOrderNumber() + { + return $this->payload['order_number']; + } + + /** + * @return null|string + */ + public function getOrderUrl() + { + return $this->payload['order_url']; + } + + /** + * @return string + */ + public function getPaymentMethod() + { + return $this->payload['payment_method']; + } + + /** + * @return string + */ + public function getRecipientName() + { + return $this->payload['recipient_name']; + } + + /** + * @return Summary + */ + public function getSummary() + { + return $this->payload['summary']; + } + + /** + * @return null|string + */ + public function getTimestamp() + { + return $this->payload['timestamp']; + } + + /** + * @param Address $address + */ + public function setAddress(Address $address) + { + $this->payload['address'] = $address; + } + + /** + * @param Adjustment[] $adjustments + */ + public function setAdjustments(array $adjustments) + { + $this->payload['adjustments'] = $adjustments; + } + + /** + * @param string $orderUrl + */ + public function setOrderUrl($orderUrl) + { + $this->payload['order_url'] = $orderUrl; + } + + /** + * @param string $timestamp + */ + public function setTimestamp($timestamp) + { + $this->payload['timestamp'] = $timestamp; + } +} diff --git a/src/Model/Button/WebUrl.php b/src/Model/Button/WebUrl.php index dd2a3a7..70b6153 100644 --- a/src/Model/Button/WebUrl.php +++ b/src/Model/Button/WebUrl.php @@ -3,7 +3,6 @@ namespace Tgallice\FBMessenger\Model\Button; use Tgallice\FBMessenger\Model\Button; -use Tgallice\FBMessenger\Template; class WebUrl extends Button { diff --git a/src/Template.php b/src/Template.php deleted file mode 100644 index aeb0525..0000000 --- a/src/Template.php +++ /dev/null @@ -1,17 +0,0 @@ -text = $text; - $this->buttons = $buttons; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @return null|string - */ - public function getText() - { - return $this->text; - } - - /** - * @return array - */ - public function getButtons() - { - return $this->buttons; - } - - /** - * @return array - */ - public function jsonSerialize() - { - return [ - 'template_type' => $this->type, - 'text' => $this->text, - 'buttons' => $this->buttons, - ]; - } -} diff --git a/src/Template/Generic.php b/src/Template/Generic.php deleted file mode 100644 index 5f044da..0000000 --- a/src/Template/Generic.php +++ /dev/null @@ -1,60 +0,0 @@ - 10) { - throw new \InvalidArgumentException('A generic template can not have more than 10 bubbles'); - } - - $this->elements = $elements; - } - - /** - * @return Element[] - */ - public function getElements() - { - return $this->elements; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @return array - */ - public function jsonSerialize() - { - return [ - 'template_type' => $this->type, - 'elements' => $this->elements, - ]; - } -} diff --git a/src/Template/Receipt.php b/src/Template/Receipt.php deleted file mode 100644 index aa99390..0000000 --- a/src/Template/Receipt.php +++ /dev/null @@ -1,222 +0,0 @@ -currency = $currency; - $this->elements = $elements; - $this->orderNumber = $orderNumber; - $this->paymentMethod = $paymentMethod; - $this->summary = $summary; - $this->recipientName = $recipientName; - } - - /** - * @return Address - */ - public function getAddress() - { - return $this->address; - } - - /** - * @return Adjustment[] - */ - public function getAdjustments() - { - return $this->adjustments; - } - - /** - * @return Element[] - */ - public function getElements() - { - return $this->elements; - } - - /** - * @return string - */ - public function getCurrency() - { - return $this->currency; - } - - /** - * @return string - */ - public function getOrderNumber() - { - return $this->orderNumber; - } - - /** - * @return string - */ - public function getOrderUrl() - { - return $this->orderUrl; - } - - /** - * @return string - */ - public function getPaymentMethod() - { - return $this->paymentMethod; - } - - /** - * @return string - */ - public function getRecipientName() - { - return $this->recipientName; - } - - /** - * @return Summary - */ - public function getSummary() - { - return $this->summary; - } - - /** - * @return string - */ - public function getTimestamp() - { - return $this->timestamp; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @param Address $address - */ - public function setAddress($address) - { - $this->address = $address; - } - - /** - * @param Adjustment[] $adjustments - */ - public function setAdjustments($adjustments) - { - $this->adjustments = $adjustments; - } - - /** - * @param string $orderUrl - */ - public function setOrderUrl($orderUrl) - { - $this->orderUrl = $orderUrl; - } - - /** - * @param string $timestamp - */ - public function setTimestamp($timestamp) - { - $this->timestamp = $timestamp; - } - - public function jsonSerialize() - { - return [ - 'template_type' => $this->type, - 'recipient_name' => $this->recipientName, - 'order_number' => $this->orderNumber, - 'currency' => $this->currency, - 'payment_method' => $this->paymentMethod, - 'timestamp' => $this->timestamp, - 'order_url' => $this->orderUrl, - 'elements' => $this->elements, - 'address' => $this->address, - 'summary' => $this->summary, - 'adjustments' => $this->adjustments, - ]; - } -}