From 703e285a3fde550b1373c850c9e2c167e6c4a7e7 Mon Sep 17 00:00:00 2001 From: Madalin Tache Date: Thu, 28 Jul 2022 15:44:48 +0300 Subject: [PATCH] Readme updates & changelog --- CHANGELOG.md | 4 ++ README.md | 94 +++++++++++++++++++++++++ src/LaravelSendsms.php | 152 ++++++++--------------------------------- 3 files changed, 125 insertions(+), 125 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c002f9..9b5da03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,7 @@ ### v1.0 Initial release + +### v1.1 + +Updated README and simplified the main class. diff --git a/README.md b/README.md index 3da936a..454a49e 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,8 @@ return [ ## Usage +### Sending a message + ```php use Niladam\LaravelSendsms\SendSmsMessage; @@ -139,6 +141,98 @@ SendSmsMessage::create(to: 0744123123, message: 'Example message here')->send(); SendSmsMessage::create(to: '0744123123', message: 'Example message here', from: '0744123456')->send(); ``` +## Other available operations + +### ping (check system) + +```php +use Niladam\LaravelSendsms\Facades\LaravelSendsms; + +LaravelSendSms::ping(); + +// returns the following output +[ + "status" => 0, + "message" => "OK", +] +``` + +### balance (check balance) +```php +use Niladam\LaravelSendsms\Facades\LaravelSendsms; + +LaravelSendSms::balance(); + +// returns the following output +[ + "status" => 0, + "message" => "OK", + "details" => 49.76696, +] +``` + +### price (check pricing for a number) +```php +use Niladam\LaravelSendsms\Facades\LaravelSendsms; + +LaravelSendSms::price('0744123123'); + +// returns the following output +[ + "status" => 0, + "message" => "OK", + "details" => [ + "cost" => 0.035, // this is the cost. + "status" => 64, + "reason" => "64: Routed OK", + ], +] +``` + +### info (get user info) +```php +use Niladam\LaravelSendsms\Facades\LaravelSendsms; + +LaravelSendSms::info(); + +// returns the following output +[ + "status" => 0, + "message" => "OK", + "details" => [ + "balance" => "49.76696", + "name" => "Some Cool Company Name", + "phone_number" => "", + "currency_code" => "EUR", + "default_prefix" => "40", + "timezone" => "Europe/Bucharest", + "last_message_sent_at" => "2022-07-28 12:48:37", + "currency_symbol" => "€ ", + "affiliate" => [ + "active" => false, + "id" => null, + "auth_ip_list" => null, + "max_registrations" => null, + "max_registrations_period" => null, + ], + ], +] +``` + +### number +```php +use Niladam\LaravelSendsms\Facades\LaravelSendsms; + +LaravelSendSms::number(); + +// returns the following output +[ + "status" => 0, + "message" => "OK", + "details" => "", // This should contain information related to the user's verified phone number. +] +``` + ### Command ```shell # The package also publishes a command with the following signature: diff --git a/src/LaravelSendsms.php b/src/LaravelSendsms.php index c58ed1e..fb6720a 100755 --- a/src/LaravelSendsms.php +++ b/src/LaravelSendsms.php @@ -6,18 +6,14 @@ use Illuminate\Support\Facades\Http; use Niladam\LaravelSendsms\Exceptions\InvalidMessageProvidedException; use Niladam\LaravelSendsms\Exceptions\InValidPhoneNumberProvidedException; -use ReflectionMethod; +use Niladam\LaravelSendsms\Exceptions\UnknownOperationException; +use Throwable; class LaravelSendsms { protected string $username; - protected string $password; - protected string $url; - - protected bool $performActionsImmediately = true; - protected array $operations = []; public function __construct(protected array $config = []) @@ -28,16 +24,22 @@ public function __construct(protected array $config = []) $this->operations = $this->config["operations"]; } - public function price(string $to = "") + /** + * @param string $to + * @return array|string + * + * @throws Throwable + */ + public function price(string $to = ""): array|string { + $operationName = "price"; + throw_if( !$to, InValidPhoneNumberProvidedException::class, "No valid phone number provided." ); - $operationName = __FUNCTION__; - throw_if( !array_key_exists($operationName, $this->operations), UnknownOperationException::class, @@ -53,8 +55,10 @@ public function price(string $to = "") return $this->call_api_action($operation, $args); } - public function call_api_action($method, $params): array|string + public function call_api_action($method, $parameters): array|string { + $params = $this->removeNumericKeysFromParameters($parameters); + $url = $this->url . "?action=" . urlencode($method); $url .= "&username=" . urlencode($this->username); $url .= "&password=" . urlencode($this->password); @@ -78,6 +82,15 @@ public function call_api_action($method, $params): array|string return $this->sendRequest($url); } + public function removeNumericKeysFromParameters(array $parameters): array + { + return array_filter( + $parameters, + static fn($key) => !is_numeric($key), + ARRAY_FILTER_USE_KEY + ); + } + public function sendRequest($url): array|string { try { @@ -106,50 +119,9 @@ private function extractDataFromUrl(string $url): array : []; } - public function dump($str) - { - if ($this->$debug) { - dump($str); - } - } - - /** - * This action allows you to check the price you can expect to pay for a message to the destination in 'to' - * - * @param string $to : A phone number - * - * @global string $password - * @global string $username - */ - public function route_check_price($to) - { - $args = func_get_args(); - - return $this->call_api_action( - new ReflectionMethod(__CLASS__, __FUNCTION__), - $args - ); - } - - /** - * Gets the user balance - * - * @global string $username - * @global string $password - */ - public function user_get_balance() - { - $args = func_get_args(); - - return $this->call_api_action( - new ReflectionMethod(__CLASS__, __FUNCTION__), - $args - ); - } - - public function __call(string $name, array $arguments) + public function balance() { - $operationName = $name; + $operationName = "balance"; throw_if( !array_key_exists($operationName, $this->operations), @@ -164,9 +136,9 @@ public function __call(string $name, array $arguments) return $this->call_api_action($operation, $args); } - public function balance() + public function __call(string $name, array $arguments) { - $operationName = __FUNCTION__; + $operationName = $name; throw_if( !array_key_exists($operationName, $this->operations), @@ -181,38 +153,6 @@ public function balance() return $this->call_api_action($operation, $args); } - /** - * Gets the user details - * - * @global string $username - * @global string $password - */ - public function user_get_info() - { - $args = func_get_args(); - - return $this->call_api_action( - new ReflectionMethod(__CLASS__, __FUNCTION__), - $args - ); - } - - /** - * This function returns the verified phone number for the given user - * - * @global string $username - * @global string $password - */ - public function user_get_phone_number() - { - $args = func_get_args(); - - return $this->call_api_action( - new ReflectionMethod(__CLASS__, __FUNCTION__), - $args - ); - } - public function send(string $to, string $message, ?string $from = "") { $operationName = __FUNCTION__; @@ -250,42 +190,4 @@ public function send(string $to, string $message, ?string $from = "") return $this->call_api_action($operation, $args); } - - /** - * Send an SMS message - * - * @param string $to - * @param string $text : The body of your message - * @param string $from (optional): The expeditor's label - * @param int $report_mask (optional): Delivery report request bitmask - * @param string $report_url (optional): URL to call when delivery status changes - * @param string $charset (optional): Character set to use - * @param int $data_coding (optional): Data coding - * @param int $message_class (optional): Message class - * @param int $auto_detect_encoding (optional): Auto detect the encoding and send appropriately 1 = on, 0 = off. - * @param string/boolean $short (optional): 1. "string" Add sort url at the end of message or search for key {short} in message and replace with short url when parameter contain URL - * 2. "boolean" Searches long url and replaces them with coresponding sort url when shrot parameter is "true" - * - * @global string $username - * @global string $password - */ - public function message_send( - $to, - $text, - $from = null, - $report_mask = 19, - $report_url = null, - $charset = null, - $data_coding = null, - $message_class = -1, - $auto_detect_encoding = null, - $short = false - ) { - $args = func_get_args(); - - return $this->call_api_action( - new ReflectionMethod(__CLASS__, __FUNCTION__), - $args - ); - } }