Skip to content

Commit

Permalink
Update to release 4.0.0 (greensms-io#12)
Browse files Browse the repository at this point in the history
* Update to release 3

* Update to release 4

* Handle 400+ code with wrong body;
Fix curl_errno RestException;

* Fix php 8.2 unit tests
  • Loading branch information
someAction authored Jul 24, 2024
1 parent 1fd62cc commit b860cd5
Show file tree
Hide file tree
Showing 26 changed files with 620 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
operating-system: ["ubuntu-latest"]
#@wontfix v.5.3 conflicts with php-cs-fixer
#@todo fix v.8 conflicts with phpunit
php-versions: ["7.3", "7.4", "8.2"]
php-versions: ["7.3", "7.4", "8.2", "8.3"]
#phpunit-versions: ['latest']

steps:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ echo "Balance : " . $response->balance. "\n";

## Compatibility

`greensms-php` is compatible with PHP 5.5 and PHP 7+ onwards until the latest PHP Version
`greensms-php` is compatible with PHP 7.3+ onwards until the latest PHP Version

## Methods

Expand All @@ -78,7 +78,7 @@ echo "Balance : " . $response->balance. "\n";

## Getting help

If you need help installing or using the library, please contact us: [support@greensms.ru](mailto:support@greensms.ru).
If you need help installing or using the library, please contact us: [support@greensms.io](mailto:support@greensms.io).

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

Expand All @@ -103,4 +103,4 @@ GreenSMS has all the unit tests defined under **tests** folder with `*Test.php`
./vendor/bin/phpunit tests
```

[apidocs]: https://api.greensms.ru/
[apidocs]: https://api.greensms.io/
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "greensms/greensms",
"description": "GREENSMS API: SMS, Viber, Voce, Call, HLR, Pay",
"description": "GREENSMS API: SMS, WhatsApp, Viber, VK, Voice, Call, HLR",
"type": "library",
"keywords": [
"greensms",
Expand All @@ -14,12 +14,14 @@
"authors": [
{
"name": "Team GreenSMS",
"email": "support@greensms.ru"
"email": "support@greensms.io"
}
],
"require": {
"php": ">=7.0",
"vlucas/valitron": "^1.4"
"php": ">=7.3",
"vlucas/valitron": "^1.4",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
Expand Down
20 changes: 20 additions & 0 deletions examples/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,23 @@
$response = $client->account->tariff();
echo "Tariff Response: ";
print_r($response);

$response = $client->account->blacklist->add(['to' => '70000000000', 'module' => 'ALL', 'comment' => 'test']);
echo "Blacklist Add Response: ";
print_r($response);
$response = $client->account->blacklist->get();
echo "Blacklist Get Response: ";
print_r($response);
$response = $client->account->blacklist->delete(['to' => '70000000000']);
echo "Blacklist Delete Response: ";
print_r($response);

$response = $client->account->limits->set(['type' => 'IP', 'module' => 'ALL', 'value' => '1.1.1.1,8.8.8.8', 'comment' => 'test']);
echo "Limits Set Response: ";
print_r($response);
$response = $client->account->limits->get();
echo "Limits Get Response: ";
print_r($response);
$response = $client->account->limits->delete(['type' => 'IP']);
echo "Limits Delete Response: ";
print_r($response);
133 changes: 94 additions & 39 deletions src/Api/ModuleLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace GreenSMS\Api;

use GreenSMS\Api\Modules;
use GreenSMS\Api\Module;
use GreenSMS\Utils\Url;
use GreenSMS\Utils\Helpers;
use GreenSMS\Api\MethodInvoker;

class ModuleLoader
{
Expand Down Expand Up @@ -40,44 +37,13 @@ public function registerModules($sharedOptions, $filters = null)


foreach ($moduleVersions as $version => $versionFunctions) {
if (!property_exists($this->moduleMap->{$moduleName}, $version)) {
$this->moduleMap->{$moduleName}->{$version} = new MethodInvoker;
}

foreach ($versionFunctions as $functionName => $functionDefinition) {
$moduleSchema = null;
$schemaExists = self::doesSchemaExists($moduleInfo, $version, $functionName);
if ($schemaExists) {
$moduleSchema = $moduleInfo['schema'][$version][$functionName];
}

$urlParts = [];
if (!$isStaticModule) {
array_push($urlParts, $moduleName);
}

array_push($urlParts, $functionName);
$apiUrl = Url::buildUrl($sharedOptions['baseUrl'], $urlParts);

$functionOptions = [
'url' => $apiUrl,
'definition' => $functionDefinition,
'sharedOptions' => $sharedOptions,
'moduleSchema' => $moduleSchema
];


$this->moduleMap->{$moduleName}->{$version}->{$functionName} = $this->getFunctionInstance($functionOptions);

if ($version === $currentVersion) {
$this->moduleMap->{$moduleName}->{$functionName} = $this->moduleMap->{$moduleName}->{$version}->{$functionName};
}

if ($isStaticModule) {
$this->moduleMap->{$functionName} = $this->moduleMap->{$moduleName}->{$version}->{$functionName};
unset($this->moduleMap->{$moduleName});
}
if ($version === 'v1') {
$this->addModuleMapV1($versionFunctions, $moduleInfo, $version, $isStaticModule, $moduleName, $sharedOptions, $currentVersion);
} elseif ($version === 'v4.0.0') {
$this->addModuleMapV4_0_0($versionFunctions, $moduleInfo, $version, $isStaticModule, $moduleName, $sharedOptions, $currentVersion);
}

}
}

Expand Down Expand Up @@ -110,4 +76,93 @@ private function getFunctionInstance($options)
$functionInstance = array($module, 'apiFunction');
return $functionInstance;
}

private function addModuleMapV1($versionFunctions, $moduleInfo, $version, $isStaticModule, $moduleName, $sharedOptions, $currentVersion)
{
if (!property_exists($this->moduleMap->{$moduleName}, $version)) {
$this->moduleMap->{$moduleName}->{$version} = new MethodInvoker;
}

foreach ($versionFunctions as $functionName => $functionDefinition) {
$moduleSchema = null;
$schemaExists = self::doesSchemaExists($moduleInfo, $version, $functionName);
if ($schemaExists) {
$moduleSchema = $moduleInfo['schema'][$version][$functionName];
}

$urlParts = [];
if (!$isStaticModule) {
array_push($urlParts, $moduleName);
}

array_push($urlParts, $functionName);
$apiUrl = Url::buildUrl($sharedOptions['baseUrl'], $urlParts);

$functionOptions = [
'url' => $apiUrl,
'definition' => $functionDefinition,
'sharedOptions' => $sharedOptions,
'moduleSchema' => $moduleSchema
];

$this->moduleMap->{$moduleName}->{$version}->{$functionName} = $this->getFunctionInstance($functionOptions);

if ($version === $currentVersion) {
$this->moduleMap->{$moduleName}->{$functionName} = $this->moduleMap->{$moduleName}->{$version}->{$functionName};
}

if ($isStaticModule) {
$this->moduleMap->{$functionName} = $this->moduleMap->{$moduleName}->{$version}->{$functionName};
unset($this->moduleMap->{$moduleName});
}
}
}

private function addModuleMapV4_0_0($functions, $moduleInfo, $version, $isStaticModule, $moduleName, $sharedOptions, $currentVersion)
{

foreach ($functions as $prop => $definitions) {
if (!property_exists($this->moduleMap->{$moduleName}, $prop)) {
$this->moduleMap->{$moduleName}->{$prop} = new MethodInvoker;
}
if (!property_exists($this->moduleMap->{$moduleName}->{$prop}, $version)) {
$this->moduleMap->{$moduleName}->{$prop}->{$version} = new MethodInvoker;
}

foreach ($definitions as $functionName => $functionDefinition) {
$urlParts = [];
if (!$isStaticModule) {
array_push($urlParts, $moduleName);
}

array_push($urlParts, $prop);
$apiUrl = Url::buildUrl($sharedOptions['baseUrl'], $urlParts);

$functionOptions = [
'url' => $apiUrl,
'definition' => $functionDefinition,
'sharedOptions' => $sharedOptions,
'moduleSchema' => $this->getSchema($moduleInfo['schema'] ?? [], $version, $prop, $functionName),
];


$this->moduleMap->{$moduleName}->{$prop}->{$version}->{$functionName} = $this->getFunctionInstance($functionOptions);
$this->moduleMap->{$moduleName}->{$prop}->{$functionName} = $this->moduleMap->{$moduleName}->{$prop}->{$version}->{$functionName};

}
}
}

private function getSchema($schema, $version, $prop, $functionName): ?array
{
if (!array_key_exists($version, $schema)) {
return null;
} elseif (!array_key_exists($prop, $schema[$version])) {
return null;
} elseif (!array_key_exists($functionName, $schema[$version][$prop])) {
return null;
}

return $schema[$version][$prop][$functionName];
}
}
85 changes: 62 additions & 23 deletions src/Api/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace GreenSMS\Api;

use GreenSMS\Api\Schema;

class Modules
{
public static function getModules()
public static function getModules(): array
{
$schema = Schema::getSchema();

$modules = [
return [
'account' => [
'schema' => $schema['account'],
'versions' => [
Expand All @@ -26,8 +24,66 @@ public static function getModules()
'tariff' => [
'args' => null,
'method' => 'GET'
]
]
],
],
'v4.0.0' => [
'blacklist' => [
'get' => [
'args' => null,
'method' => 'GET',
],
'add' => [
'args' => ['params'],
'method' => 'POST',
],
'delete' => [
'args' => ['params'],
'method' => 'DELETE',
],
],
'limits' => [
'get' => [
'args' => null,
'method' => 'GET',
],
'set' => [
'args' => null,
'method' => 'POST',
],
'delete' => [
'args' => null,
'method' => 'DELETE',
],
],
'webhook' => [
'get' => [
'args' => null,
'method' => 'GET',
],
'set' => [
'args' => null,
'method' => 'POST',
],
'delete' => [
'args' => null,
'method' => 'DELETE',
],
],
'whitelist' => [
'get' => [
'args' => null,
'method' => 'GET',
],
'add' => [
'args' => null,
'method' => 'POST',
],
'delete' => [
'args' => null,
'method' => 'DELETE',
],
],
],
]
],
'call' => [
Expand Down Expand Up @@ -177,23 +233,6 @@ public static function getModules()
]
]
],
'social' => [
'schema' => $schema['social'],
'versions' => [
'v1' => [
'send' => [
'args' => ['params'],
'method' => 'POST',
],
'status' => [
'args' => ['params'],
'method' => 'GET',
],
]
]
],
];

return $modules;
}
}
Loading

0 comments on commit b860cd5

Please sign in to comment.