Skip to content

Commit

Permalink
Programmed and debugged
Browse files Browse the repository at this point in the history
  • Loading branch information
yiiman-dev committed Apr 3, 2022
1 parent b5cf48f commit 942513f
Show file tree
Hide file tree
Showing 12 changed files with 494 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor/
.idea
composer.lock
env.php
testm.php
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,83 @@
# mediana-sms
# Mediana-sms
MedianaSMS API - created by APIStorm package

This package is fully classified responses and send data
In this package you can validate data before send to server with internal validation method,then this package is safe for programmers that want to know different between server errors and posted data errors or etc
## Install
You can install this package easily by composer

Just type this code on command line:

`composer require yiiman/mediana-sms`

## Usage
Usage of this package is easy and safe.

Let's start together:

In this example we want to know credit:

```php
$api = new Yiiman\MedianaSms\MedianaSMS();
$api->apiKey = 'Your SMS panel API key';
$creditResult = $api->getCredit();
if ($creditResult->isSuccess()) {
echo $creditResult->credit;//is Float
}else{
echo "Response has some errors:\n\n";
var_dump($creditResult->getError()->message);
}
```

In this example we want to validate our data and then send new SMS:

```php
$api = new Yiiman\MedianaSms\MedianaSMS();
$api->apiKey = 'Your SMS panel API key';
// < Initializing data >
{
$dataSms = new SendSMSRequest();
$dataSms->originator0 = $sms_line;
$dataSms->recipients0 = [$test_mobile_number];
$dataSms->message0 = 'this is test';
}
// </ Initializing data >
if ($dataSms->validate()) {
$sendResult = $api->send($dataSms);
if ($sendResult && $sendResult->bulk_id) {
$message_info = $api->getMessage($sendResult->bulk_id);
if ($message_info->isSuccess()) {
echo "Message info :\n\n";
echo '-- message:'.$message_info->message."\n";
echo '-- status:'.$message_info->status."\n";
echo '-- created at:'.$message_info->created_at."\n";
echo '-- type:'.$message_info->type."\n";
echo '-- confirm status:'.$message_info->confirm_state."\n";
echo '-- number:'.$message_info->number."\n";
echo '-- sent at:'.$message_info->sent_at."\n";
echo '-- cost:'.$message_info->cost."\n";
echo '-- payback cost:'.$message_info->payback_cost."\n";
echo '-- count of receivers:'.$message_info->recipients_count."\n";
echo '-- count of valid receivers:'.$message_info->valid_recipients_count."\n";
} else {
echo "Server returned some errors:\n\n";
var_dump($message_info->getError()->message);
}
}
} else {
echo "Your posted data has some errors:\n\n";
var_dump($dataSms->errors());
}

```

## Sample codes
You can find samples in `test.php` file

For run `test.php` file ,you should change `env-example.php` to `env.php` and fill data in that

When `env.php` file is ready, open terminal and type:

```bash
php test.php
```
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "yiiman/mediana-sms",
"description": "Classified Mediana(IPPanel) sms API, created by APIStorm package",
"type": "library",
"require": {
"yiiman/apistorm": "dev-main",
"ippanel/php-rest-sdk": "^1.0"
},
"license": "Apache-2.0",
"autoload": {
"psr-4": {
"Yiiman\\MedianaSms\\": "src/"
}
},
"authors": [
{
"name": "YiiMan",
"email": "[email protected]"
}
]
}
11 changes: 11 additions & 0 deletions env-examples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$api_key = 'your api key';// Your API key
$sms_line = '3000505';// Default sms line number for send messages
$test_mobile_number = '09353466620';//Test mobile number for receive sms messages
$pattern_code = 'your pattern code in sms panel';
$pattern_values =
[
'first_value',
'second_value'
];
120 changes: 120 additions & 0 deletions src/MedianaSMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* @author YiiMan <[email protected]>
* @link https://yiiman.ir
*/

namespace Yiiman\MedianaSms;

use YiiMan\ApiStorm\Core\Connection;
use YiiMan\ApiStorm\Core\Res;
use YiiMan\ApiStorm\Post\BasePostDataInterface;
use Yiiman\MedianaSms\RequestClasses\SendPatternRequest;
use Yiiman\MedianaSms\RequestClasses\SendSMSRequest;
use Yiiman\MedianaSms\Responses\BulkIdResponse;
use Yiiman\MedianaSms\Responses\CreditResponse;
use Yiiman\MedianaSms\Responses\MessageInfoResponse;

class MedianaSMS
{

public $protocol = 'https';
public $baseURl = 'rest.ippanel.com';

public $apiKey = '';

/**
* @param BasePostDataInterface|null $dataClass
* @param string $path
* @return Res
*/
private function call(string $path, BasePostDataInterface $dataClass = null, $method = 'post')
{
$servedArrayOfDataClass = !empty($dataClass) ? $dataClass->serve() : [];
$connection = new Connection();
$connection->baseURL = $this->baseURl;
$connection->protocol = 'https';
$userAgent = sprintf("IPPanel/ApiClient/%s PHP/%s", "1.0.1", phpversion());

return $connection->call($path, [], $servedArrayOfDataClass, [], $method,
[
"Authorization: AccessKey ".$this->apiKey,
"User-Agent: ".$userAgent,
'Accept: application/json',
'Content-Type: application/json'
],
true
);

}

/**
* دریافت موجودی ریالی پنل پیامک
* @return Res|CreditResponse
*/
public function getCredit()
{
$response = $this->call("v1/credit", null, 'get');
if ($response->isSuccess()) {
return new CreditResponse($response);
} else {
return $response;
}
}


/**
* @param SendSMSRequest $data needed data for send
* @return false|BulkIdResponse|Res if false,its mean posted data is not valid
*/
public function send(SendSMSRequest $data)
{
if ($data->validate()) {
$res = $this->call("v1/messages", $data);
if ($res->isSuccess()) {
return new BulkIdResponse($res);
} else {
return $res;
}
} else {
return false;
}
}


/**
* get message info using bulk id
* @param int $bulkID
* @return Res|MessageInfoResponse
*/
public function getMessage(int $bulkID)
{
$res = $this->call(sprintf("v1/messages/%d", $bulkID), null, 'get');
if ($res->isSuccess()) {
return new MessageInfoResponse($res);
} else {
return $res;
}
}


/**
* Send message with pattern
* @param SendPatternRequest $data
* @return false|Res
*/
public function sendPattern(SendPatternRequest $data)
{
if ($data->validate()) {
$res = $this->call("v1/messages/patterns/send", $data);
if ($res->isSuccess()) {
return new BulkIdResponse($res);
} else {
return $res;
}
} else {
return false;
}
}

}
35 changes: 35 additions & 0 deletions src/RequestClasses/SendPatternRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* @date_of_create: 4/3/2022 AD 11:57
*/

namespace Yiiman\MedianaSms\RequestClasses;

/**
* @property string $pattern_code0 pattern code that is created in sms panel
* @property string $originator0 sms line number that using for send message
* @property string $recipient0 receiver mobile number
* @property string $values0 pattern variables
*/
class SendPatternRequest extends \YiiMan\ApiStorm\Post\BasePostData
{

public string $pattern_code0;
public string $originator0;
public string $recipient0;
public array $values0;

/**
* @inheritDoc
*/
public function rules(): array
{
return
[
'pattern_code' => 'string',
'originator' => 'string',
'recipient' => 'string',
'values' => 'array',
];
}
}
29 changes: 29 additions & 0 deletions src/RequestClasses/SendSMSRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @date_of_create: 4/3/2022 AD 10:46
*/

namespace Yiiman\MedianaSms\RequestClasses;

/**
* @property string $originator0 this is line number, that using for send new message
* @property array $recipients0 this is an array of receivers numbers
* @property string $message0 your message
*/
class SendSMSRequest extends \YiiMan\ApiStorm\Post\BasePostData
{

public string $originator0;
public array $recipients0;
public string $message0;

public function rules(): array
{
return
[
'originator' => 'string',
'recipients' => 'array',
'message' => 'string'
];
}
}
18 changes: 18 additions & 0 deletions src/Responses/BaseResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* @date_of_create: 3/29/2022 AD 18:13
*/

namespace Yiiman\MedianaSms\Responses;

/**
* @property string $status وضعیت
* @property string $code کد وضعیت
* @property string $message متن پاسخ
*/
class BaseResponse extends \YiiMan\ApiStorm\Response\BaseResponse
{
public $status = 'string';
public $code = 'string';
public $message = 'string';
}
14 changes: 14 additions & 0 deletions src/Responses/BulkIdResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @date_of_create: 4/3/2022 AD 11:19
*/

namespace Yiiman\MedianaSms\Responses;

/**
* @property int $bulk_id you can use this id for check status of messages
*/
class BulkIdResponse extends BaseResponse
{
public $bulk_id='key_in_object: data.bulk_id';
}
14 changes: 14 additions & 0 deletions src/Responses/CreditResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @date_of_create: 3/29/2022 AD 15:59
*/

namespace Yiiman\MedianaSms\Responses;

/**
* @property float $credit
*/
class CreditResponse extends BaseResponse
{
public $credit='key_in_object: data.credit';
}
Loading

0 comments on commit 942513f

Please sign in to comment.