Skip to content

Docs ‐ Record

Isaac Sai edited this page Jan 21, 2024 · 1 revision

Introduction to Record

USSD record provides a place to save data during USSD execution. You can inject in any USSD state or action where you need to persist data or to retrieved already saved data.

<?php

namespace App\Ussd\States;

use Sparors\Ussd\Contracts\State;
use Sparors\Ussd\Menu;
use Sparors\Ussd\Record;

class SimpleState implements State
{
    public function render(Record $record): Menu
    {
        $name = 'John Doe';
        $record->set('name', $name);
        $age = $record->get('age');

        return Menu::build()->format('%s is %d years old.', $name, $age);
    }
}

Available Methods

$record->set('name', 'John Doe');
$record->get('age', 13);
$record->setMany(['name' => 'John Doe', 'age' => 13]);
[$name, $age] = $record->getMany(['name', 'age']);
$record->forget('age');
$record->forgetMany(['name', 'age']);
$record->increment('age');
$record->decrement('age');
$record->has('name');
$name = $record('name'); // to get value
$name = $record->name; // to get value
$record(['age' => 17]); // to set values
isset($record->name); // to check value

All data saved with records are only available within a USSD session. Once the session ends, all the data can not be retrieved any more. If you will like to save data across sessions for a particular user, set the public property of the method to true.

$record->set('name', 'John Doe', public: true);

If you would want to data to be deleted after some time, set the ttl value;

$record->set('name', 'John Doe', ttl: now()->addMinute());