Skip to content

Commit

Permalink
Merge pull request #156 from ker0x/feature/product-template
Browse files Browse the repository at this point in the history
Add Product template & element
  • Loading branch information
ker0x authored Oct 26, 2020
2 parents 7671f5b + e71378e commit 3718466
Show file tree
Hide file tree
Showing 28 changed files with 195 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/Api/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Kerox\Messenger\Exception\InvalidRecipientException;
use Kerox\Messenger\Exception\InvalidTypeException;
use Kerox\Messenger\Helper\ValidatorTrait;
use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Model\Message\AbstractAttachment;
use Kerox\Messenger\Request\SendRequest;
use Kerox\Messenger\Response\SendResponse;
use Kerox\Messenger\SendInterface;
Expand Down Expand Up @@ -55,7 +55,7 @@ public function action($recipient, string $action, array $options = []): SendRes
/**
* @throws \Exception
*/
public function attachment(Attachment $attachment): SendResponse
public function attachment(AbstractAttachment $attachment): SendResponse
{
$message = $this->isValidMessage($attachment);

Expand Down
6 changes: 3 additions & 3 deletions src/Helper/ValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Model\Common\Button\AbstractButton;
use Kerox\Messenger\Model\Message;
use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Model\Message\AbstractAttachment;
use Kerox\Messenger\Model\Message\Attachment\Template\GenericTemplate;
use Kerox\Messenger\SendInterface;

Expand Down Expand Up @@ -158,11 +158,11 @@ protected function isValidMessage($message): Message
return $message;
}

if (\is_string($message) || $message instanceof Attachment) {
if (\is_string($message) || $message instanceof AbstractAttachment) {
return Message::create($message);
}

throw new MessengerException(sprintf('"message" must be a string or an instance of "%s" or "%s".', Message::class, Attachment::class));
throw new MessengerException(sprintf('"message" must be a string or an instance of "%s" or "%s".', Message::class, AbstractAttachment::class));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Kerox\Messenger\Exception\InvalidClassException;
use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Helper\ValidatorTrait;
use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Model\Message\AbstractAttachment;
use Kerox\Messenger\Model\Message\QuickReply;

class Message implements \JsonSerializable
Expand All @@ -23,7 +23,7 @@ class Message implements \JsonSerializable
protected $type;

/**
* @var \Kerox\Messenger\Model\Message\Attachment|string
* @var \Kerox\Messenger\Model\Message\AbstractAttachment|string
*/
protected $message;

Expand All @@ -40,7 +40,7 @@ class Message implements \JsonSerializable
/**
* Message constructor.
*
* @param \Kerox\Messenger\Model\Message\Attachment|string $message
* @param \Kerox\Messenger\Model\Message\AbstractAttachment|string $message
*
* @throws \Exception
*/
Expand All @@ -49,17 +49,17 @@ public function __construct($message)
if (\is_string($message)) {
$this->isValidString($message, 640);
$this->type = self::TYPE_TEXT;
} elseif ($message instanceof Attachment) {
} elseif ($message instanceof AbstractAttachment) {
$this->type = self::TYPE_ATTACHMENT;
} else {
throw new MessengerException(sprintf('message must be a string or an instance of %s.', Attachment::class));
throw new MessengerException(sprintf('message must be a string or an instance of %s.', AbstractAttachment::class));
}

$this->message = $message;
}

/**
* @param \Kerox\Messenger\Model\Message\Attachment|string $message
* @param \Kerox\Messenger\Model\Message\AbstractAttachment|string $message
*
* @throws \Exception
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Kerox\Messenger\Helper\UtilityTrait;
use Kerox\Messenger\Helper\ValidatorTrait;

abstract class Attachment implements \JsonSerializable
abstract class AbstractAttachment implements \JsonSerializable
{
use UtilityTrait;
use ValidatorTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Kerox\Messenger\Model\Message\Attachment;

use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Model\Message\AbstractAttachment;

abstract class Template extends Attachment
abstract class AbstractTemplate extends AbstractAttachment
{
protected const TYPE_AIRLINE_BOARDINGPASS = 'airline_boardingpass';
protected const TYPE_AIRLINE_CHECKIN = 'airline_checkin';
Expand All @@ -15,15 +15,16 @@ abstract class Template extends Attachment
protected const TYPE_BUTTON = 'button';
protected const TYPE_GENERIC = 'generic';
protected const TYPE_LIST = 'list';
protected const TYPE_RECEIPT = 'receipt';
protected const TYPE_OPEN_GRAPH = 'open_graph';
protected const TYPE_MEDIA = 'media';
protected const TYPE_OPEN_GRAPH = 'open_graph';
protected const TYPE_PRODUCT = 'product';
protected const TYPE_RECEIPT = 'receipt';

/**
* Template constructor.
*/
public function __construct()
{
parent::__construct(Attachment::TYPE_TEMPLATE);
parent::__construct(AbstractAttachment::TYPE_TEMPLATE);
}
}
4 changes: 2 additions & 2 deletions src/Model/Message/Attachment/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Model\Message\Attachment;

use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Model\Message\AbstractAttachment;

class Audio extends File
{
Expand All @@ -15,7 +15,7 @@ class Audio extends File
*/
public function __construct(string $url, ?bool $reusable = null)
{
parent::__construct($url, $reusable, Attachment::TYPE_AUDIO);
parent::__construct($url, $reusable, AbstractAttachment::TYPE_AUDIO);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Message/Attachment/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Kerox\Messenger\Model\Message\Attachment;

use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Model\Message\AbstractAttachment;

class File extends Attachment
class File extends AbstractAttachment
{
/**
* @var string|null
Expand All @@ -30,7 +30,7 @@ class File extends Attachment
*
* @throws \Kerox\Messenger\Exception\MessengerException
*/
public function __construct(string $url, ?bool $reusable = null, $type = Attachment::TYPE_FILE)
public function __construct(string $url, ?bool $reusable = null, $type = AbstractAttachment::TYPE_FILE)
{
parent::__construct($type);

Expand Down
4 changes: 2 additions & 2 deletions src/Model/Message/Attachment/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Model\Message\Attachment;

use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Model\Message\AbstractAttachment;

class Image extends File
{
Expand All @@ -19,7 +19,7 @@ public function __construct(string $url, ?bool $reusable = null)
{
$this->isValidExtension($url, $this->getAllowedExtensions());

parent::__construct($url, $reusable, Attachment::TYPE_IMAGE);
parent::__construct($url, $reusable, AbstractAttachment::TYPE_IMAGE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Kerox\Messenger\Model\Message\Attachment\Template;

use Kerox\Messenger\Helper\ValidatorTrait;
use Kerox\Messenger\Model\Message\Attachment\Template;
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;

abstract class AbstractAirlineTemplate extends Template
abstract class AbstractAirlineTemplate extends AbstractTemplate
{
use ValidatorTrait;

Expand Down Expand Up @@ -36,7 +36,7 @@ public function __construct(string $locale)
}

/**
* @throws \Kerox\Messenger\Exception\MessengerException
*@throws \Kerox\Messenger\Exception\MessengerException
*
* @return \Kerox\Messenger\Model\Message\Attachment\Template\AbstractAirlineTemplate
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Model\Message\Attachment\Template;

use Kerox\Messenger\Model\Message\Attachment\Template;
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;

class AirlineBoardingPassTemplate extends AbstractAirlineTemplate
{
Expand Down Expand Up @@ -34,9 +34,9 @@ public function __construct(string $introMessage, string $locale, array $boardin
}

/**
* @throws \Kerox\Messenger\Exception\MessengerException
*@throws \Kerox\Messenger\Exception\MessengerException
*
* @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineBoardingPassTemplate
*@return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineBoardingPassTemplate
*/
public static function create(string $introMessage, string $locale, array $boardingPass): self
{
Expand All @@ -48,7 +48,7 @@ public function toArray(): array
$array = parent::toArray();
$array += [
'payload' => [
'template_type' => Template::TYPE_AIRLINE_BOARDINGPASS,
'template_type' => AbstractTemplate::TYPE_AIRLINE_BOARDINGPASS,
'intro_message' => $this->introMessage,
'locale' => $this->locale,
'theme_color' => $this->themeColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Model\Message\Attachment\Template;

use Kerox\Messenger\Model\Message\Attachment\Template;
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;

class AirlineCheckInTemplate extends AbstractAirlineTemplate
{
Expand Down Expand Up @@ -49,9 +49,9 @@ public function __construct(
}

/**
* @throws \Kerox\Messenger\Exception\MessengerException
*@throws \Kerox\Messenger\Exception\MessengerException
*
* @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineCheckInTemplate
*@return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineCheckInTemplate
*/
public static function create(
string $introMessage,
Expand All @@ -68,7 +68,7 @@ public function toArray(): array
$array = parent::toArray();
$array += [
'payload' => [
'template_type' => Template::TYPE_AIRLINE_CHECKIN,
'template_type' => AbstractTemplate::TYPE_AIRLINE_CHECKIN,
'intro_message' => $this->introMessage,
'locale' => $this->locale,
'theme_color' => $this->themeColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Model\Message\Attachment\Template;

use Kerox\Messenger\Model\Message\Attachment\Template;
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;

class AirlineItineraryTemplate extends AbstractAirlineTemplate
{
Expand Down Expand Up @@ -114,9 +114,9 @@ public static function create(
/**
* @param string $currency
*
* @throws \Kerox\Messenger\Exception\MessengerException
*@throws \Kerox\Messenger\Exception\MessengerException
*
* @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineItineraryTemplate
*@return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineItineraryTemplate
*/
public function addPriceInfo(string $title, string $amount, ?string $currency = null): self
{
Expand Down Expand Up @@ -162,7 +162,7 @@ public function toArray(): array
$array = parent::toArray();
$array += [
'payload' => [
'template_type' => Template::TYPE_AIRLINE_ITINERARY,
'template_type' => AbstractTemplate::TYPE_AIRLINE_ITINERARY,
'intro_message' => $this->introMessage,
'locale' => $this->locale,
'theme_color' => $this->themeColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Kerox\Messenger\Model\Message\Attachment\Template;

use Kerox\Messenger\Exception\InvalidTypeException;
use Kerox\Messenger\Model\Message\Attachment\Template;
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;
use Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo;

class AirlineUpdateTemplate extends AbstractAirlineTemplate
Expand Down Expand Up @@ -51,9 +51,9 @@ public function __construct(string $updateType, string $locale, string $pnrNumbe
}

/**
* @throws \Kerox\Messenger\Exception\MessengerException
*@throws \Kerox\Messenger\Exception\MessengerException
*
* @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineUpdateTemplate
*@return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineUpdateTemplate
*/
public static function create(
string $updateType,
Expand Down Expand Up @@ -99,7 +99,7 @@ public function toArray(): array
$array = parent::toArray();
$array += [
'payload' => [
'template_type' => Template::TYPE_AIRLINE_UPDATE,
'template_type' => AbstractTemplate::TYPE_AIRLINE_UPDATE,
'intro_message' => $this->introMessage,
'update_type' => $this->updateType,
'locale' => $this->locale,
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Message/Attachment/Template/ButtonTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Kerox\Messenger\Model\Message\Attachment\Template;

use Kerox\Messenger\Model\Message\Attachment\Template;
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;

class ButtonTemplate extends Template
class ButtonTemplate extends AbstractTemplate
{
/**
* @var string
Expand Down Expand Up @@ -53,7 +53,7 @@ public function toArray(): array
$array = parent::toArray();
$array += [
'payload' => [
'template_type' => Template::TYPE_BUTTON,
'template_type' => AbstractTemplate::TYPE_BUTTON,
'text' => $this->text,
'buttons' => $this->buttons,
'sharable' => $this->sharable,
Expand Down
37 changes: 37 additions & 0 deletions src/Model/Message/Attachment/Template/Element/ProductElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Model\Message\Attachment\Template\Element;

class ProductElement implements \JsonSerializable
{
/**
* @var string
*/
private $id;

public function __construct(string $id)
{
$this->id = $id;
}

public static function create(string $id): self
{
return new self($id);
}

public function toArray(): array
{
$array = [
'id' => $this->id,
];

return array_filter($array);
}

public function jsonSerialize(): array
{
return $this->toArray();
}
}
Loading

0 comments on commit 3718466

Please sign in to comment.