Skip to content

Commit

Permalink
Merge pull request #6 from CyberSai/master
Browse files Browse the repository at this point in the history
InitialState can accept closure
  • Loading branch information
cybersai authored May 12, 2020
2 parents d6f8732 + b0d2cd1 commit cc50bd2
Show file tree
Hide file tree
Showing 16 changed files with 572 additions and 240 deletions.
12 changes: 10 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
All notable changes to `laravel Ussd` will be documented in this file.

## [Unreleased]

## [v1.0.0] - 2020-05-12
### Added
- More test
- Machine SetInitialState can take callable
### Changed
- State type changed to action
- License File changed to github format
- Updated Readme
- Updated changelog

## [v0.1.0] - 2020-05-02
## [v0.1.0] - 2020-05-02
### Added
- Ussd Package Project with README, contributing, changelog, license, etc.
- State class to define what should occur at various stages when user navigates
Expand All @@ -22,5 +29,6 @@ All notable changes to `laravel Ussd` will be documented in this file.
- Ussd config to allow developers customize behaviour
- Ussd service Provider class to allow laravel know how to integrate the package

[Unreleased]: ../../compare/v0.1.0...HEAD
[Unreleased]: ../../compare/v1.0.0...HEAD
[v1.0.0]: ../../compare/v0.1.0...v1.0.0
[v0.1.0]: ../../releases/tag/v0.1.0
8 changes: 4 additions & 4 deletions config/ussd.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
|
*/

'class_namespace' => 'App\\Http\\Ussd',
'class_namespace' => env('USSD_NAMESPACE', 'App\\Http\\Ussd'),

/*
|--------------------------------------------------------------------------
Expand All @@ -24,7 +24,7 @@
|
*/

'store' => null,
'cache_store' => env('USSD_STORE', null),


/*
Expand All @@ -37,7 +37,7 @@
|
*/

'cache_ttl' => null,
'cache_ttl' => env('USSD_TTL', null),

/*
|--------------------------------------------------------------------------
Expand All @@ -49,5 +49,5 @@
|
*/

'cache_default' => null,
'cache_default' => env('USSD_DEFAULT_VALUE', null),
];
73 changes: 46 additions & 27 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ Build Ussd (Unstructured Supplementary Service Data) applications with laravel w
You can install the package via composer:

``` bash
$ composer require sparors/laravel-ussd
composer require sparors/laravel-ussd
```

Laravel Ussd provides zero configuration out of the box. To publish the config, run the vendor publish command:

``` bash
$ php artisan vendor:publish --provider="Sparors\Ussd\UssdServiceProvider" --tag=config
php artisan vendor:publish --provider="Sparors\Ussd\UssdServiceProvider" --tag=config
```

## Usage
Expand All @@ -28,21 +28,21 @@ We provide a ussd artisan command which allows you to quickly create new states.

``` bash
php artisan ussd:state Welcome
````
```

### Creating Nested States

Linux/Unix

``` bash
php artisan ussd:state Airtime/Welcome
````
```

Windows

``` bash
php artisan ussd:state Airtime\Welcome
````
```

Welcome state class generated

Expand Down Expand Up @@ -82,12 +82,18 @@ class Welcome extends State
{
protected function beforeRendering(): void
{
$this->menu->text('Welcome To LaravelUSSD')
->lineBreak(1)
$this->menu->text('Welcome To Laravel USSD')
->lineBreak(2)
->line('Select an option')
->listing(['Airtime Topup', 'Data Bundle', 'TV Subscription', 'ECG/GWCL', 'Talk To Us'])
->listing([
'Airtime Topup',
'Data Bundle',
'TV Subscription',
'ECG/GWCL',
'Talk To Us'
])
->lineBreak(2);
->line('Powered by Sparors')
->text('Powered by Sparors')
}

protected function afterRendering(string $argument): void
Expand All @@ -97,9 +103,9 @@ class Welcome extends State
}
```

### Creating Decisions
### Creating Decisions and Linking them with States

Add your decision to the afterRendering method
Add your decision to the afterRendering method and link them with states

``` php
<?php
Expand All @@ -116,27 +122,30 @@ class Welcome extends State
protected function beforeRendering(): void
{
$this->menu->text('Welcome To Laravel Ussd')
->lineBreak(1)
->lineBreak(2)
->line('Select an option')
->listing(['Airtime Topup', 'Data Bundle', 'TV Subscription', 'ECG/GWCL', 'Talk To Us'])
->listing([
'Airtime Topup',
'Data Bundle',
'TV Subscription',
'ECG/GWCL',
'Talk To Us'
])
->lineBreak(2);
->line('Powered by Sparors')
->text('Powered by Sparors')
}

protected function afterRendering(string $argument): void
{
// If input is equal to 1, 2, 3, 4 or 5, render the appropriate state
$this->decision->equal('1', GetRecipientNumber::class)
->equal('2', MaintenanceMode::class)
->equal('3', MaintenanceMode::class)
->equal('4', MaintenanceMode::class)
->equal('5', MaintenanceMode::class)
->between(2, 5, MaintenanceMode::class)
->any(Error::class);
}
}
```

### Using States
### Setting Initial State

Import the welcome state class and pass it to the setInitialState method

Expand All @@ -152,12 +161,22 @@ class UssdController extends Controller
{
public function index()
{
$ussd = Ussd::machine()
->setInput('1')
->setNetwork('MTN')
->setSessionId('12350')
->setPhoneNumber('0545112466')
->setInitialState(Welcome::class);
$ussd = Ussd::machine()
->setFromRequest([
'network',
'phone_number' => 'msisdn',
'sessionId' => 'UserSessionID'
'input' => 'msg'
])
->setInitialState(Welcome::class)
->setResponse(function (string $message, string $action) {
return [
'USSDResp' => [
'action' => $acion,
'menus' => '',
'title' => $message,
];
});

return response()->json($ussd->run());
}
Expand All @@ -168,10 +187,10 @@ class UssdController extends Controller

You can use the development server the ships with Laravel by running, from the project root:

```bash
``` bash
php artisan serve
```
You can visit [http://localhot:8000](http://localhot:8000) to see the application in action.
You can visit [http://localhost:8000](http://localhost:8000) to see the application in action.

Enjoy!!!

Expand Down
Loading

0 comments on commit cc50bd2

Please sign in to comment.