Skip to content

Commit

Permalink
Readme updates & changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
niladam committed Jul 28, 2022
1 parent f1b2621 commit 703e285
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 125 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
### v1.0

Initial release

### v1.1

Updated README and simplified the main class.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ return [

## Usage

### Sending a message

```php
use Niladam\LaravelSendsms\SendSmsMessage;

Expand Down Expand Up @@ -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:
Expand Down
152 changes: 27 additions & 125 deletions src/LaravelSendsms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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__;
Expand Down Expand Up @@ -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
);
}
}

0 comments on commit 703e285

Please sign in to comment.