Skip to content

Commit

Permalink
Merge pull request #126 from ker0x/feature/custom-exceptions
Browse files Browse the repository at this point in the history
[n/a] add custom Exception
  • Loading branch information
ker0x authored Oct 6, 2018
2 parents 324951f + 95c84c5 commit 2f5caca
Show file tree
Hide file tree
Showing 33 changed files with 201 additions and 65 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Version `3.0.0` of the Messenger library is an enhancement of version `2.1.1` wi

- 3.0.0 (2018-10)
- Update API version to `v3.1`.
- Add support for Persona API (Thanks to @misantron)
- Add support for the BUSINESS_PRODUCTIVITY message tag (Thanks to @atgg)
- Add support for Persona API (Thanks to @misantron).
- Add support for the BUSINESS_PRODUCTIVITY message tag (Thanks to @atgg).
- Adding `$appVersion` as optional 4th argument in `\Kerox\Messenger\Messenger::__construct()` with default value to const `API_VERSION`.
- Remove parameters `$notificationType` and `$tag` for methods `message()` and `action` in `\Kerox\Messenger\Api\Send()`.
- Remove parameters `$notificationType` and `$tag` for methods `send()` in `\Kerox\Messenger\Api\Broadcast()`.
Expand All @@ -25,6 +25,7 @@ Version `3.0.0` of the Messenger library is an enhancement of version `2.1.1` wi
- Method `addQuickReply()` in `\Kerox\Messenger\Model\Message()` will throw an exception if there is already too many quick replies.
- Parameters `$ref`, `$source` and `$type` in `\Kerox\Messenger\Model\Callback\Referral()` can now return `null`.
- Add parameter `$sharable` in `\Kerox\Messenger\Model\Message\Attachment\Template\ButtonTemplate()`.
- Increase limit to 640 characters for `$text` in `\Kerox\Messenger\Model\Message\Attachment\Template\ButtonTemplate()`. (Thanks to @mferrara)
- Improve tests
- Refactor code
- Increase limit to 640 characters for `$text` in `\Kerox\Messenger\Model\Message\Attachment\Template\ButtonTemplate()`. (Thanks to @mferrara).
- Throw new Exception for better information.
- Improve tests.
- Refactor code.
6 changes: 3 additions & 3 deletions src/Api/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Api;

use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Exception\InvalidOptionException;
use Kerox\Messenger\Helper\ValidatorTrait;
use Kerox\Messenger\Request\BroadcastRequest;
use Kerox\Messenger\Response\BroadcastResponse;
Expand Down Expand Up @@ -35,7 +35,7 @@ public function create($message): BroadcastResponse
* @param string $messageCreativeId
* @param array $options
*
* @throws \Kerox\Messenger\Exception\MessengerException
* @throws \Kerox\Messenger\Exception\InvalidOptionException
*
* @return \Kerox\Messenger\Response\BroadcastResponse
*/
Expand All @@ -61,7 +61,7 @@ private function isValidOptions(array $options): array
$allowedOptionsKeys = $this->getAllowedOptionsKeys();
foreach ($options as $key => $value) {
if (!\in_array($key, $allowedOptionsKeys, true)) {
throw new MessengerException(sprintf(
throw new InvalidOptionException(sprintf(
'Only "%s" are allowed keys for options.',
implode(', ', $allowedOptionsKeys)
));
Expand Down
6 changes: 4 additions & 2 deletions src/Api/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Kerox\Messenger\Api;

use Kerox\Messenger\Exception\InvalidStringException;
use Kerox\Messenger\Exception\InvalidTypeException;
use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Request\CodeRequest;
use Kerox\Messenger\Response\CodeResponse;
Expand Down Expand Up @@ -60,7 +62,7 @@ private function isValidCodeType(string $codeType): void
{
$allowedCodeType = $this->getAllowedCodeType();
if (!\in_array($codeType, $allowedCodeType, true)) {
throw new MessengerException(sprintf(
throw new InvalidTypeException(sprintf(
'codeType must be either "%s".',
implode(', ', $allowedCodeType)
));
Expand All @@ -75,7 +77,7 @@ private function isValidCodeType(string $codeType): void
private function isValidRef(string $ref): void
{
if (!preg_match('/^[a-zA-Z0-9\+\/=\-.:_ ]{1,250}$/', $ref)) {
throw new MessengerException(
throw new InvalidStringException(
'ref must be a string of max 250 characters. Valid characters are "a-z A-Z 0-9 +/=-.:_".'
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Api;

use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Exception\InvalidKeyException;
use Kerox\Messenger\InsightsInterface;
use Kerox\Messenger\Request\InsightsRequest;
use Kerox\Messenger\Response\InsightsResponse;
Expand Down Expand Up @@ -45,7 +45,7 @@ private function isValidMetrics(array $metrics): array
if ($metrics !== $allowedMetrics) {
array_map(function ($metric) use ($allowedMetrics): void {
if (!\in_array($metric, $allowedMetrics, true)) {
throw new MessengerException(sprintf(
throw new InvalidKeyException(sprintf(
'%s is not a valid value. Metrics must only contain "%s".',
$metric,
implode(', ', $allowedMetrics)
Expand Down
11 changes: 6 additions & 5 deletions src/Api/Nlp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Kerox\Messenger\Api;

use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Exception\InvalidKeyException;
use Kerox\Messenger\Exception\InvalidTypeException;
use Kerox\Messenger\NlpInterface;
use Kerox\Messenger\Request\NlpRequest;
use Kerox\Messenger\Response\NlpResponse;
Expand Down Expand Up @@ -39,7 +40,7 @@ private function isValidConfigs(array $configs): void
if (!empty($configs)) {
foreach ($configs as $key => $value) {
if (!\in_array($key, $allowedConfigKeys, true)) {
throw new MessengerException(sprintf(
throw new InvalidKeyException(sprintf(
'%s is not a valid key. configs must only contain "%s".',
$key,
implode(', ', $allowedConfigKeys)
Expand All @@ -64,7 +65,7 @@ private function isBool(string $key, $value): void
if (!\is_bool($value) &&
\in_array($key, [self::CONFIG_KEY_NLP_ENABLED, self::CONFIG_KEY_VERBOSE], true)
) {
throw new MessengerException(sprintf('%s must be a boolean.', $key));
throw new InvalidTypeException(sprintf('%s must be a boolean.', $key));
}
}

Expand All @@ -79,7 +80,7 @@ private function isString(string $key, $value): void
if (!\is_string($value) &&
\in_array($key, [self::CONFIG_KEY_CUSTOM_TOKEN, self::CONFIG_KEY_MODEL], true)
) {
throw new MessengerException(sprintf('%s must be a string.', $key));
throw new InvalidTypeException(sprintf('%s must be a string.', $key));
}
}

Expand All @@ -92,7 +93,7 @@ private function isString(string $key, $value): void
private function isValidNBest(string $key, $value): void
{
if ($key === self::CONFIG_KEY_N_BEST && (!\is_int($value) || $value < 1 || $value > 8)) {
throw new MessengerException(sprintf('%s must be an integer between 1 and 8.', $key));
throw new InvalidTypeException(sprintf('%s must be an integer between 1 and 8.', $key));
}
}

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

namespace Kerox\Messenger\Api;

use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Exception\InvalidKeyException;
use Kerox\Messenger\Model\ProfileSettings;
use Kerox\Messenger\ProfileInterface;
use Kerox\Messenger\Request\ProfileRequest;
Expand Down Expand Up @@ -71,7 +71,7 @@ private function isValidFields(array $fields): void
$allowedFields = $this->getAllowedFields();
foreach ($fields as $field) {
if (!\in_array($field, $allowedFields, true)) {
throw new MessengerException(sprintf(
throw new InvalidKeyException(sprintf(
'%s is not a valid value. fields must only contain "%s".',
$field,
implode(', ', $allowedFields)
Expand Down
7 changes: 4 additions & 3 deletions src/Api/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Kerox\Messenger\Api;

use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Exception\InvalidOptionException;
use Kerox\Messenger\Exception\InvalidTypeException;
use Kerox\Messenger\Helper\ValidatorTrait;
use Kerox\Messenger\Model\Message\Attachment;
use Kerox\Messenger\Request\SendRequest;
Expand Down Expand Up @@ -85,7 +86,7 @@ private function isValidOptions(array $options, $message): array
$allowedOptionsKeys = $this->getAllowedOptionsKeys();
foreach ($options as $key => $value) {
if (!\in_array($key, $allowedOptionsKeys, true)) {
throw new MessengerException(sprintf(
throw new InvalidOptionException(sprintf(
'Only "%s" are allowed keys for options.',
implode(', ', $allowedOptionsKeys)
));
Expand All @@ -112,7 +113,7 @@ protected function isValidMessagingType(string $messagingType): void
{
$allowedMessagingType = $this->getAllowedMessagingType();
if (!\in_array($messagingType, $allowedMessagingType, true)) {
throw new MessengerException(sprintf(
throw new InvalidTypeException(sprintf(
'messagingType must be either "%s".',
implode(', ', $allowedMessagingType)
));
Expand Down
4 changes: 2 additions & 2 deletions src/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kerox\Messenger\Api;

use Kerox\Messenger\Exception\MessengerException;
use Kerox\Messenger\Exception\InvalidKeyException;
use Kerox\Messenger\Request\UserRequest;
use Kerox\Messenger\Response\UserResponse;
use Kerox\Messenger\UserInterface;
Expand All @@ -27,7 +27,7 @@ public function profile(string $userId, array $fields = []): UserResponse
if ($fields !== $allowedFields) {
foreach ($fields as $field) {
if (!\in_array($field, $allowedFields, true)) {
throw new MessengerException(sprintf(
throw new InvalidKeyException(sprintf(
'%s is not a valid value. fields must only contain "%s".',
$field,
implode(', ', $allowedFields)
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/InvalidArrayException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidArrayException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidClassException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidColorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidColorException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidCountryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidCountryException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidCurrencyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidCurrencyException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidDateTimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidDateTimeException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidExtensionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidExtensionException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidKeyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidKeyException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidLocaleException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidLocaleException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidOptionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidOptionException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidStringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidStringException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidTypeException extends MessengerException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidUrlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Kerox\Messenger\Exception;

class InvalidUrlException extends MessengerException
{
}
Loading

0 comments on commit 2f5caca

Please sign in to comment.