commit 0cdfd0185b8df68ece3fd75bd84370661869e346 Author: Daniel Seifert Date: Mon Jun 20 14:47:33 2022 +0200 initial diff --git a/README.md b/README.md new file mode 100644 index 0000000..ac89790 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +``` +$client = new Client('accesstoken'); +$request = (new \D3\LinkmobilityClient\SMS\Request('me', 'message')) + ->addRecipient(new Recipient('recipient')); +$client->request($request) +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..153c76c --- /dev/null +++ b/composer.json @@ -0,0 +1,43 @@ +{ + "name": "d3/linkmobility-php-client", + "type": "library", + "description": "PHP client for Linkmobility API (de)", + "keywords": [ + "linkmobility", + "php", + "sms", + "sms gateway" + ], + "homepage": "https://d3data.de/", + "authors": [ + { + "name": "D3 Data Development (Inh. Thomas Dartsch)", + "email": "info@shopmodule.com", + "homepage": "https://www.d3data.de", + "role": "Owner" + } + ], + "require": { + "php": "^7.0", + "beberlei/assert": "^2.7", + "guzzlehttp/guzzle": "~6.2", + "psr/http-message": "~1.0", + "symfony/options-resolver": "^3.0|^4.0", + "phlib/sms-length": "^1.1.0 || ^2.0.0", + "giggsey/libphonenumber-for-php": "^8.12" + }, + "require-dev": { + "phpunit/phpunit" : "~5.7||~6.0", + "friendsofphp/php-cs-fixer": "^2.0" + }, + "autoload": { + "psr-4": { + "D3\\LinkmobilityClient\\": "src" + } + }, + "scripts": { + "test": "phpunit", + "check-style": "./vendor/bin/php-cs-fixer fix --verbose --dry-run", + "fix-style": "./vendor/bin/php-cs-fixer fix --verbose" + } +} diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..4e2095b --- /dev/null +++ b/src/Client.php @@ -0,0 +1,79 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\LinkmobilityClient; + +use D3\LinkmobilityClient\Request\RequestInterface; + +class Client +{ + private $accessToken; + public $apiUrl; + public $requestClient; + + public function __construct(string $accessToken, $apiUrl = false, $client = false) + { + if ($apiUrl !== false && false === $apiUrl instanceof UrlInterface) { + throw new \RuntimeException('ApiUrl instance must implement UrlInterface'); + } + + $this->accessToken = $accessToken; + $this->apiUrl = $apiUrl ?: new Url(); + $this->requestClient = $client ?: new \GuzzleHttp\Client( [ 'base_uri' => $this->apiUrl->getBaseUri() ] ); + } + + public function request(RequestInterface $request) : ResponseInterface + { + $request->validate(); + $responseClass = $request->getResponseClass(); + return $request->getResponseInstance( + $this->rawRequest($request->getUri(), $request->getMethod(), $request->getOptions()) + ); + } + + /** + * @param $url + * @param string $method + * @param array $postArgs + * + * @return string + * @throws ApiException + * @throws GuzzleException + */ + protected function rawRequest( $url, string $method = RequestInterface::METHOD_GET, array $options = []): string + { + $options['headers']['Authorization'] = 'access_token '.$this->accessToken; + + if (!empty($body)) { + $options['json'] = $body; + } + dumpvar(__METHOD__.__LINE__.PHP_EOL); +dumpvar($options); +dumpVar($method); +dumpvar($url); +die(); + $response = $this->requestClient->request( + $method, + $url, + $options + ); + + if ($response->getStatusCode() != 200) { + throw new ApiException('request '.$url.' returns status code '.$response->getStatusCode()); + } + + return $response; + } +} \ No newline at end of file diff --git a/src/RecipientsList/RecipientsList.php b/src/RecipientsList/RecipientsList.php new file mode 100644 index 0000000..87ef2be --- /dev/null +++ b/src/RecipientsList/RecipientsList.php @@ -0,0 +1,82 @@ + + * @link http://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\LinkmobilityClient\RecipientsList; + +use D3\LinkmobilityClient\ValueObject\Recipient; + +class RecipientsList implements RecipientsListInterface, \Iterator +{ + /** + * @var array + */ + private $recipients = []; + + public function add(Recipient $recipient) + { + $this->recipients[md5(serialize($recipient))] = $recipient; + } + + public function clearRecipents() + { + $this->recipients = []; + } + + public function getRecipients() : array + { + return array_values( + array_map( + function (Recipient $recipient) { + return $recipient->get(); + }, + $this->recipients + ) + ); + } + + public function getRecipientsList() : array + { + return $this->recipients; + } + + public function current() + { + return current($this->recipients); + } + + public function next() + { + return next($this->recipients); + } + + public function key() + { + return key($this->recipients); + } + + public function rewind() + { + return reset($this->recipients); + } + + public function valid() + { + return (false !== current($this->recipients) && current($this->recipients) instanceof Recipient); + } + + //stract methods and must therefore be declared abstract or implement the remaining methods (Iterator::current, Iterator::next, Iterator::key, ...) in +} \ No newline at end of file diff --git a/src/RecipientsList/RecipientsListInterface.php b/src/RecipientsList/RecipientsListInterface.php new file mode 100644 index 0000000..0303328 --- /dev/null +++ b/src/RecipientsList/RecipientsListInterface.php @@ -0,0 +1,29 @@ + + * @link http://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\LinkmobilityClient\RecipientsList; + +use D3\LinkmobilityClient\ValueObject\Recipient; + +interface RecipientsListInterface +{ + public function add(Recipient $recipient); + + public function clearRecipents(); + + public function getRecipients() : array; +} \ No newline at end of file diff --git a/src/Request/Request.php b/src/Request/Request.php new file mode 100644 index 0000000..3dad0dc --- /dev/null +++ b/src/Request/Request.php @@ -0,0 +1,449 @@ + + * @link http://www.oxidmodule.com + */ + +declare( strict_types = 1 ); + +namespace D3\LinkmobilityClient\Request; + +use Assert\Assert; +use D3\LinkmobilityClient\RecipientsList\RecipientsList; +use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface; +use D3\LinkmobilityClient\Response\ResponseInterface; +use D3\LinkmobilityClient\ValueObject\Recipient; +use D3\LinkmobilityClient\ValueObject\Sender; +use D3\LinkmobilityClient\ValueObject\SmsMessage; +use D3\LinkmobilityClient\ValueObject\StringValueObject; +use OxidEsales\Eshop\Core\Registry; + +abstract class Request implements RequestInterface +{ + /** + * @var StringValueObject + */ + private $message; + + /** + * @var string + */ + private $method = RequestInterface::METHOD_GET; + + /** + * @var string + */ + private $contentType = RequestInterface::CONTENTTYPE_JSON; + + /** + * @var null|string + */ + private $clientMessageId; + + /** + * @var string + */ + private $contentCategory = RequestInterface::CONTENTCATEGORY_INFORMATIONAL; + + /** + * @var string + */ + private $messageType = RequestInterface::MESSAGETYPE_DEFAULT; + + /** + * @var string|null + */ + private $notificationCallbackUrl = null; + + /** + * @var null|int + */ + private $priority = null; + + /** + * @var RecipientsListInterface + */ + private $recipientsList; + + /** + * @var bool + */ + private $sendAsFlashSms = false; + + /** + * @var string|null + */ + private $senderAddress = null; + + /** + * @var string|null + */ + private $senderAddressType = null; + + /** + * @var int|null + */ + private $validityPeriode = null; + + /** + * @var bool + */ + private $test = false; + + /** + * @var bool + */ + private $maxSmsPerMessage = 0; + + public function __construct(StringValueObject $message) + { + $this->recipientsList = new RecipientsList(); + $this->setMessage( $message ); + + return $this; + } + + /** + * @throws InvalidArgumentException + */ + public function validate(): void + { + Assert::that( $this->getMethod() )->choice( self::getMethods() ); + Assert::that( $this->getUri() )->string()->startsWith( '/' ); + + Assert::that($this->getBody())->isArray(); + Assert::that($this->getResponseClass())->implementsInterface(ResponseInterface::class); + Assert::that($this->getOptions())->isArray(); + + Assert::that( $this->getRecipientsList() )->isInstanceOf(RecipientsList::class)->notEmpty(); + Assert::thatAll( $this->getRecipientsList() )->isInstanceOf( Recipient::class ); + + // optional properties + Assert::thatNullOr( $this->getClientMessageId() )->string(); + Assert::thatNullOr( $this->getContentCategory() )->choice(self::getContentCategories()); + Assert::thatNullOr( $this->getNotificationCallbackUrl() )->url(); + Assert::thatNullOr( $this->getPriority() )->integer(); + Assert::thatNullOr( $this->getSendAsFlashSms() )->boolean(); + Assert::thatNullOr( $this->getSenderAddress() )->isInstanceOf(Sender::class); + Assert::thatNullOr( $this->getSenderAddressType() )->choice(self::getSenderAddressTypes()); + Assert::thatNullOr( $this->isTest() )->boolean(); + Assert::thatNullOr( $this->getValidityPeriode() )->integer(); + } + + public function getRawBody() : array + { + return [ + 'clientMessageId' => $this->getClientMessageId(), + 'contentCategory' => $this->getContentCategory(), + 'maxSmsPerMessage' => $this->getMaxSmsPerMessage(), + 'messageContent' => (string) $this->getMessage(), + 'messageType' => $this->getMessageType(), + 'notificationCallbackUrl' => $this->getNotificationCallbackUrl(), + 'priority' => $this->getPriority(), + 'recipientAddressList' => $this->getRecipientsList()->getRecipients(), + 'sendAsFlashSms' => $this->getSendAsFlashSms(), + 'senderAddress' => $this->getSenderAddress()->get(), + 'senderAddressType' => $this->getSenderAddressType(), + 'test' => $this->isTest(), + 'validityPeriode' => $this->getValidityPeriode() + ]; + } + + public function getBody(): array + { + $body = array_filter( $this->getRawBody(), function( $elm ) { + return ! is_null( $elm ); + } ); + + switch ($this->getContentType()) { + case RequestInterface::CONTENTTYPE_JSON: + return ['json' => json_encode($body)]; + default: + return $body; + } + } + + public function getOptions(): array + { + return array_merge( + [ + 'headers' => [ + 'Accept' => $this->contentType, + 'Content-Type' => $this->contentType, + ] + + ], + $this->getBody() + ); + } + + public function setMessage(StringValueObject $message) + { + $this->message = $message; + + return $this; + } + + public function getMessage(): StringValueObject + { + return $this->message; + } + + public function setMethod(string $method) + { + $this->method = $method; + + return $this; + } + + public function getMethod(): string + { + return $this->method; + } + + public static function getMethods(): array + { + return [ + RequestInterface::METHOD_GET => RequestInterface::METHOD_GET, + RequestInterface::METHOD_POST => RequestInterface::METHOD_POST, + RequestInterface::METHOD_PUT => RequestInterface::METHOD_PUT, + RequestInterface::METHOD_PATCH => RequestInterface::METHOD_PATCH, + RequestInterface::METHOD_DELETE => RequestInterface::METHOD_DELETE, + ]; + } + + public function setContentType(string $contentType) + { + $this->contentType = $contentType; + + return $this; + } + + public function getContentType() : string + { + return $this->contentType; + } + + public function setClientMessageId($clientMessageId) + { + $this->clientMessageId = $clientMessageId; + + return $this; + } + + public function getClientMessageId() + { + return $this->clientMessageId; + } + + public function setContentCategory(string $contentCategory) + { + $this->contentCategory = $contentCategory; + + return $this; + } + + public function getContentCategory() : string + { + return $this->contentCategory; + } + + public static function getContentCategories(): array + { + return [ + RequestInterface::CONTENTCATEGORY_ADVERTISEMENT => RequestInterface::CONTENTCATEGORY_ADVERTISEMENT, + RequestInterface::CONTENTCATEGORY_INFORMATIONAL => RequestInterface::CONTENTCATEGORY_INFORMATIONAL + ]; + } + + public function setTest(bool $test) + { + $this->test = $test; + + return $this; + } + + public function isTest() : bool + { + return $this->test; + } + + public function setMaxSmsPerMessage(int $maxSmsPerMessage) + { + $this->maxSmsPerMessage = $maxSmsPerMessage; + + return $this; + } + + public function getMaxSmsPerMessage() : int + { + return $this->maxSmsPerMessage; + } + + public function setMessageType(string $messageType) + { + $this->messageType = $messageType; + + return $this; + } + + public function getMessageType() : string + { + return $this->messageType; + } + + /** + * @param string|null $notificationCallbackUrl + * + * @return $this + */ + public function setNotificationCallbackUrl($notificationCallbackUrl) + { + $this->notificationCallbackUrl = $notificationCallbackUrl; + + return $this; + } + + /** + * @return string|null + */ + public function getNotificationCallbackUrl() + { + return $this->notificationCallbackUrl; + } + + /** + * @param string|null $priority + * + * @return $this + */ + public function setPriority($priority) + { + $this->priority = $priority; + + return $this; + } + + /** + * @return string|null + */ + public function getPriority() + { + return $this->priority; + } + + /** + * @return RecipientsListInterface + */ + public function getRecipientsList() : RecipientsListInterface + { + return $this->recipientsList; + } + + /** + * @param array $recipientList + * + * @return $this + */ + public function setSendAsFlashSms(bool $flashSms) + { + $this->sendAsFlashSms = $flashSms; + + return $this; + } + + /** + * @return string|null + */ + public function getSendAsFlashSms() : bool + { + return $this->sendAsFlashSms; + } + + /** + * @param Sender $senderAddress + * + * @return $this + */ + public function setSenderAddress(Sender $senderAddress) + { + $this->senderAddress = $senderAddress; + + return $this; + } + + /** + * @return string|null + */ + public function getSenderAddress() : Sender + { + return $this->senderAddress; + } + + /** + * @param $senderAddressType + * + * @return $this + */ + public function setSenderAddressType($senderAddressType) + { + $this->senderAddressType = $senderAddressType; + + return $this; + } + + /** + * @return string|null + */ + public function getSenderAddressType() + { + return $this->senderAddressType; + } + + public static function getSenderAddressTypes(): array + { + return [ + RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC => RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC, + RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL => RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL, + RequestInterface::SENDERADDRESSTYPE_NATIONAL => RequestInterface::SENDERADDRESSTYPE_NATIONAL, + RequestInterface::SENDERADDRESSTYPE_SHORTCODE => RequestInterface::SENDERADDRESSTYPE_SHORTCODE + ]; + } + + /** + * @param $validityPeriode + * + * @return $this + */ + public function setValidityPeriode($validityPeriode) + { + $this->validityPeriode = $validityPeriode; + + return $this; + } + + /** + * @return int|null + */ + public function getValidityPeriode() + { + return $this->validityPeriode; + } + + /** + * @return ResponseInterface + */ + public function getResponseInstance(\Psr\Http\Message\ResponseInterface $rawResponse): ResponseInterface + { + return new $this->getResponseClass($rawResponse); + } +} \ No newline at end of file diff --git a/src/Request/RequestInterface.php b/src/Request/RequestInterface.php new file mode 100644 index 0000000..24c10ad --- /dev/null +++ b/src/Request/RequestInterface.php @@ -0,0 +1,93 @@ + + * @link http://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\LinkmobilityClient\Request; + +use D3\LinkmobilityClient\Response\ResponseInterface; + +interface RequestInterface +{ + const METHOD_GET = 'GET'; + const METHOD_POST = 'POST'; + const METHOD_PUT = 'PUT'; + const METHOD_PATCH = 'PATCH'; + const METHOD_DELETE = 'DELETE'; + + const CONTENTTYPE_JSON = 'application/json'; + + const CONTENTCATEGORY_INFORMATIONAL = 'informational'; + const CONTENTCATEGORY_ADVERTISEMENT = 'advertisement'; + + const MESSAGETYPE_DEFAULT = 'default'; + const MESSAGETYPE_VOICE = 'voice'; + + const SENDERADDRESSTYPE_NATIONAL = 'national'; + const SENDERADDRESSTYPE_INTERNATIONAL = 'international'; + const SENDERADDRESSTYPE_ALPHANUMERIC = 'alphanumeric'; + const SENDERADDRESSTYPE_SHORTCODE = 'shortcode'; + + public function setMethod(string $method); + + /** + * Must return the HTTP verb for this request, i.e. GET, POST, PUT + * + * @return string + */ + public function getMethod() : string; + + /** + * Must return the URI for the request with a leading slash, i.e. /messages.json + * + * @return string + */ + public function getUri() : string; + + /** + * Must return the body which is being sent as json + * + * @return array + */ + public function getBody() : array; + + /** + * Must return the class to where the response is handed over. It must implement the ResponseInterface + * + * @return string + */ + public function getResponseClass() : string; + + /** + * @param \Psr\Http\Message\ResponseInterface $rawResponse + * + * @return ResponseInterface + */ + public function getResponseInstance(\Psr\Http\Message\ResponseInterface $rawResponse): ResponseInterface; + + /** + * Must return the options for this request. If there are none, return [] (empty array) + * + * @return array + */ + public function getOptions() : array; + + /** + * Must validate the input of the request + * This is called before sending the request + * Must throw an exception if the validation fails + */ + public function validate() : void; +} \ No newline at end of file diff --git a/src/Response/Response.php b/src/Response/Response.php new file mode 100644 index 0000000..1373620 --- /dev/null +++ b/src/Response/Response.php @@ -0,0 +1,45 @@ +keyExists('status'); + + $this->data = $data; + $this->status = (int)$this->data['status']; + + if ($this->isSuccessful()) { + $this->init(); + } + } + + /** + * @return bool + */ + public function isSuccessful(): bool + { + return $this->status >= 200 && $this->status < 300; + } + + public function getError(): string + { + return $this->isSuccessful() ? '' : $this->data['message']; + } +} diff --git a/src/Response/ResponseInterface.php b/src/Response/ResponseInterface.php new file mode 100644 index 0000000..351d9fb --- /dev/null +++ b/src/Response/ResponseInterface.php @@ -0,0 +1,29 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\LinkmobilityClient\SMS; + +use D3\LinkmobilityClient\ValueObject\Sender; +use D3\LinkmobilityClient\ValueObject\SmsMessage; + +class Request +{ + public function __construct( Sender $sender, SmsMessage $message) + { + $this = new TextRequest($sender, $message); + /* + if ($message->isGsm7()) { + return new TextRequest($sender, $message); + } + + return new TextRequest($sender, $message); + */ + } +} \ No newline at end of file diff --git a/src/SMS/Response.php b/src/SMS/Response.php new file mode 100644 index 0000000..ff56b06 --- /dev/null +++ b/src/SMS/Response.php @@ -0,0 +1,49 @@ +data) + ->keyExists('stat') + ->keyExists('details') + ; + + $this->stat = new Stat($this->data['stat']); + $this->details = new Details($this->data['details']); + } + + /** + * @return Stat + */ + public function getStat(): Stat + { + return $this->stat; + } + + /** + * @return Details + */ + public function getDetails(): Details + { + return $this->details; + } +} diff --git a/src/SMS/TextRequest.php b/src/SMS/TextRequest.php new file mode 100644 index 0000000..0582b00 --- /dev/null +++ b/src/SMS/TextRequest.php @@ -0,0 +1,502 @@ + + * @link http://www.oxidmodule.com + */ + +declare( strict_types = 1 ); + +namespace D3\LinkmobilityClient\SMS; + +use Assert\Assert; +use D3\LinkmobilityClient\Request\RequestInterface; +use D3\LinkmobilityClient\Url; +use D3\LinkmobilityClient\ValueObject\Message; +use D3\LinkmobilityClient\ValueObject\Sender; +use D3\LinkmobilityClient\ValueObject\SmsMessage; + +class TextRequest extends \D3\LinkmobilityClient\Request\Request +{ + /** + * @var Sender + */ + protected $sender; + + /** + * @var boolean + */ + protected $status; + + /** + * @var string + */ + protected $statusUrl; + + /** + * @var string + */ + protected $returnData; + + /** + * @var int + */ + protected $class; + + /** + * @var \DateTimeInterface + */ + protected $sendTime; + + /** + * @var int + */ + protected $price; + + /** + * @var boolean + */ + protected $charity; + + /** + * @var string + */ + protected $invoiceText; + + /** + * @var int + */ + protected $validity; + + /** + * @var string + */ + protected $format; + + /** + * @var string + */ + protected $udh; + + /** + * @var array + */ + protected $attachment; + + /** + * @var string + */ + protected $pushUrl; + + /** + * @var string + */ + protected $pushExpire; + + /** + * @var array + */ + protected $filter; + + /** + * @var array + */ + protected $segmentation; + + /** + * @var int + */ + protected $pid; + + /** + * @var string + */ + protected $advanced; + + /** + * @var string + */ + protected $protocol; + + /** + * @var string + */ + protected $revenueText; + + public function getUri(): string + { + return '/smsmessaging/text/'; + } + + public function validate(): void + { + parent::validate(); + + Assert::thatNullOr( $this->getMessage() )->isInstanceOf(SmsMessage::class); + } + + public function getResponseClass(): string + { + return Response::class; + } + + /** + * @param Message $message + * + * @return PostMessageRequest + */ +/* + public function setMessage( SmsMessage $message ) + { + $this->message = $message; + + if ($message->isGsm7()) { + $this->setFormat(SmsMessage::GSM_7BIT); + } else { + $this->setFormat(SmsMessage::GSM_UCS2); + } + + return $this; + } +*/ + + /** + * @return bool + */ + public function isStatus(): bool + { + return $this->status; + } + + /** + * @param bool $status + * + * @return PostMessageRequest + */ + public function setStatus( bool $status ) + { + $this->status = $status; + + return $this; + } + + /** + * @return string + */ + public function getReturnData(): string + { + return $this->returnData; + } + + /** + * @param string $returnData + * + * @return PostMessageRequest + */ + public function setReturnData( string $returnData ) + { + $this->returnData = $returnData; + + return $this; + } + + /** + * @return int + */ + public function getClass(): int + { + return $this->class; + } + + /** + * @param int $class + * + * @return PostMessageRequest + */ + public function setClass( int $class ) + { + $this->class = $class; + + return $this; + } + + /** + * @return \DateTimeInterface + */ + public function getSendTime(): \DateTimeInterface + { + return $this->sendTime; + } + + /** + * @param \DateTimeInterface $sendTime + * + * @return PostMessageRequest + */ + public function setSendTime( \DateTimeInterface $sendTime ) + { + $this->sendTime = $sendTime; + + return $this; + } + + /** + * @return int + */ + public function getValidity(): int + { + return $this->validity; + } + + /** + * @param int|\DateInterval $validity In minutes + * + * @return PostMessageRequest + */ + public function setValidity( $validity ) + { + if ( $validity instanceof \DateInterval ) { + $now = new \DateTimeImmutable(); + $seconds = $now->add( $validity )->getTimestamp() - $now->getTimestamp(); + $validity = ceil( $seconds / 60 ); + } + + $validity = (int) $validity; + + $this->validity = $validity; + + return $this; + } + + /** + * @return string + */ + public function getFormat(): string + { + return $this->format; + } + + /** + * @param string $format + * + * @return PostMessageRequest + */ + public function setFormat( string $format ) + { + $this->format = $format; + + return $this; + } + + /** + * @return string + */ + public function getUdh(): string + { + return $this->udh; + } + + /** + * @param string $udh + * + * @return PostMessageRequest + */ + public function setUdh( string $udh ) + { + $this->udh = $udh; + + return $this; + } + + /** + * @return array + */ + public function getAttachment(): array + { + return $this->attachment; + } + + /** + * @param array $attachment + * + * @return PostMessageRequest + */ + public function setAttachment( array $attachment ) + { + $this->attachment = $attachment; + + return $this; + } + + /** + * @return string + */ + public function getPushUrl(): string + { + return $this->pushUrl; + } + + /** + * @param string $pushUrl + * + * @return PostMessageRequest + */ + public function setPushUrl( string $pushUrl ) + { + $this->pushUrl = $pushUrl; + + return $this; + } + + /** + * @return string + */ + public function getPushExpire(): string + { + return $this->pushExpire; + } + + /** + * @param string|\DateTimeInterface $pushExpire + * + * @return PostMessageRequest + */ + public function setPushExpire( $pushExpire ) + { + if ( $pushExpire instanceof \DateTimeInterface ) { + $pushExpire = (string) $pushExpire->getTimestamp(); + } + + $this->pushExpire = $pushExpire; + + return $this; + } + + /** + * @return array + */ + public function getFilter(): array + { + return $this->filter; + } + + /** + * @param array $filter + * + * @return PostMessageRequest + */ + public function setFilter( array $filter ) + { + $this->filter = $filter; + + return $this; + } + + /** + * @return array + */ + public function getSegmentation(): array + { + return $this->segmentation; + } + + /** + * @param array $segmentation + * + * @return PostMessageRequest + */ + public function setSegmentation( array $segmentation ) + { + $this->segmentation = $segmentation; + + return $this; + } + + /** + * @return int + */ + public function getPid(): int + { + return $this->pid; + } + + /** + * @param int $pid + * + * @return PostMessageRequest + */ + public function setPid( int $pid ) + { + $this->pid = $pid; + + return $this; + } + + /** + * @return string + */ + public function getAdvanced(): string + { + return $this->advanced; + } + + /** + * @param string $advanced + * + * @return PostMessageRequest + */ + public function setAdvanced( string $advanced ) + { + $this->advanced = $advanced; + + return $this; + } + + /** + * @return string + */ + public function getProtocol(): string + { + return $this->protocol; + } + + /** + * @param string $protocol + * + * @return PostMessageRequest + */ + public function setProtocol( string $protocol ) + { + $this->protocol = $protocol; + + return $this; + } + + /** + * @return string + */ + public function getRevenueText(): string + { + return $this->revenueText; + } + + /** + * @param string $revenueText + * + * @return PostMessageRequest + */ + public function setRevenueText( string $revenueText ) + { + $this->revenueText = $revenueText; + + return $this; + } +} \ No newline at end of file diff --git a/src/Url.php b/src/Url.php new file mode 100644 index 0000000..1edd27b --- /dev/null +++ b/src/Url.php @@ -0,0 +1,101 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\LinkmobilityClient; + +class Url implements UrlInterface +{ + public $baseUri = 'https://api.linkmobility.eu/rest'; + + /** + * @return string + */ + public function getBaseUri(): string + { + return $this->baseUri; + } + + /** + * @param float $lat + * @param float $lng + * @param float $radius + * @param string $sort + * @param string $type + * + * @return string + */ + public function getListUrl(float $lat, float $lng, float $radius, string $sort, string $type): string + { + $query = http_build_query( + [ + 'lat' => $lat, + 'lng' => $lng, + 'rad' => $radius, + 'sort' => $sort, + 'type' => $type, + 'apikey'=> $this->apiKey + ] + ); + return "list.php?$query"; + } + + /** + * @param $stationId + * + * @return string + */ + public function getStationDetailUrl($stationId): string + { + $query = http_build_query( + [ + 'id' => $stationId, + 'apikey'=> $this->apiKey + ] + ); + return "detail.php?$query"; + } + + /** + * @param array $stationList + * + * @return string + * @throws ApiException + */ + public function getPricesUrl(array $stationList): string + { + if (count($stationList) < 1 || count($stationList) > 10) { + throw new ApiException('Preisabfrage darf nur zwischen 1 und 10 Stationen beinhalten'); + } + + $query = http_build_query( + [ + 'ids' => implode( ',', $stationList), + 'apikey'=> $this->apiKey + ] + ); + + return "prices.php?$query"; + } + + /** + * @return string + */ + public function getComplaintUrl(): string + { + $query = http_build_query(['apikey'=> $this->apiKey]); + + return $this->baseUri . "complaint.php?$query"; + } +} \ No newline at end of file diff --git a/src/UrlInterface.php b/src/UrlInterface.php new file mode 100644 index 0000000..6507feb --- /dev/null +++ b/src/UrlInterface.php @@ -0,0 +1,21 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\LinkmobilityClient; + +interface UrlInterface +{ + public function getBaseUri(): string; +} \ No newline at end of file diff --git a/src/ValueObject/Recipient.php b/src/ValueObject/Recipient.php new file mode 100644 index 0000000..77e7b28 --- /dev/null +++ b/src/ValueObject/Recipient.php @@ -0,0 +1,25 @@ +regex('/^(\+|c)?[0-9]+$/i', 'Recipient does not match valid phone number.'); + + parent::__construct($value); + } +} diff --git a/src/ValueObject/Sender.php b/src/ValueObject/Sender.php new file mode 100644 index 0000000..7d3a3ac --- /dev/null +++ b/src/ValueObject/Sender.php @@ -0,0 +1,20 @@ +maxLength(11); + } + + parent::__construct($value); + } +} diff --git a/src/ValueObject/SmsMessage.php b/src/ValueObject/SmsMessage.php new file mode 100644 index 0000000..a8a9b2b --- /dev/null +++ b/src/ValueObject/SmsMessage.php @@ -0,0 +1,54 @@ +value); + $smsLength->validate(); + } + + /** + * @var bool + */ + private $gsm7; + + public function chunkCount() : int + { + $smsLength = new SmsLength($this->value); + return $smsLength->getMessageCount(); + } + + public function length() : int + { + $smsLength = new SmsLength($this->value); + $smsLength->getSize(); + } + + public function isGsm7() : bool + { + $smsLength = new SmsLength($this->value); + if (is_null($this->gsm7)) { + $this->gsm7 = $smsLength->getEncoding() === self::GSM_7BIT; + } + + return $this->gsm7; + } + + public function isUnicode() : bool + { + return !$this->isGsm7(); + } +} diff --git a/src/ValueObject/StringValueObject.php b/src/ValueObject/StringValueObject.php new file mode 100644 index 0000000..0e90e3f --- /dev/null +++ b/src/ValueObject/StringValueObject.php @@ -0,0 +1,27 @@ +notEmpty(); + + $this->value = $value; + } + + public function __toString() + { + return $this->get(); + } + + public function get() : string + { + return $this->value; + } +} diff --git a/src/ValueObject/ValueObject.php b/src/ValueObject/ValueObject.php new file mode 100644 index 0000000..e9467e5 --- /dev/null +++ b/src/ValueObject/ValueObject.php @@ -0,0 +1,10 @@ +