Usage
To make a Dialplan we first need an instance of the Dialplan class. This is the main holder classes for all lines you will add.
The Dialplan requires a name. This, in Asterisk, is the name found in [].
You have the option of passing in 1 line straight into the constructor.
$dialplan = new Dialplan('example_context', $line)
There are a few types of lines, for now we will just use the typical ExtenLine. The ExtenLine requires a pattern, string or number to match, the priority and the application tied with that line.
In this example, we are just assigning a number for the exten line, set to priority 1 and we pass the Application we want.
$line = new ExtenLine('100', 1, $application)
Every line requires a specification of an Application. In the above line, lets issue a Dial application. Different Applications have different methods, check that Applications class to find everything you have access to.
$application = new Dial('SIP', 'phone1')
The Dialplan class, all Lines and Applications have a toString method to easily turn that class to the Asterisk string.
The above code for ExtenLine would output the following.
echo $line->toString()
// outputs
// exten => 100,1,Dial(SIP/phone1)
Using the toString method on the Dialplan class will prepare the entire Dialplan in string form. The above Dialplan would look like this.
echo $dialplan->toString();
// outputs
// [example_context]
// exten => 100,1,Dial(SIP/phone1)
//
Dialplan::toString will also include line breaks "\n"
include "vendor/autoload.php";
use Clearvox\Asterisk\Dialplan\Application\Dial;
use Clearvox\Asterisk\Dialplan\Dialplan;
use Clearvox\Asterisk\Dialplan\Line\ExtenLine;
// Make the first line
$firstLine = new ExtenLine(100, 1, (new Dial('SIP', 'phone1', 30))->addOption('o'));
// Make the whole Dialplan Context
$dialplan = new Dialplan('example_context', $firstLine);
// Prepare the second Dial
$dial = new Dial('SIP', 'phone2', 30);
$dial
->addOption('o')
->addOption('t')
->addOption('T');
// Second line
$secondLine = new ExtenLine(101, 1, $dial);
// Add to the Dialplan
$dialplan->addLine($secondLine);
// Check the string representation of the Dialplan
echo $dialplan->toString();
// [example_context]
// exten => 100,1,Dial(SIP/phone1,30,o)
// exten => 101,1,Dial(SIP/phone2,30,otT)