PHP Class PAGI\Node\Node

Author: Marcelo Gornstein ([email protected])
Mostrar archivo Open project: marcelog/pagi Class Usage Examples

Protected Properties

Property Type Description
$state integer Node state.

Public Methods

Method Description
__toString ( ) : string A classic!
addPrePromptMessage ( string $filename ) : void Adds a sound file to play as a pre prompt message.
cancelWith ( string $digit ) : Node Configures a specific digit as the cancel digit.
cancelWithInputRetriesInput ( ) : Node Allow the user to retry input by pressing the cancel digit after entered one or more digits. For example, when entering a 12 number pin, the user might press the cancel digit at the 5th digit to re-enter it. This counts as a failed input, but will not cancel the node. The node will be cancelled only if the user presses the cancel digit with NO input at all.
clearPromptMessages ( ) : Node Removes prompt messages.
createValidatorInfo ( Closure $validation, string | null $soundOnError = null ) : validatorInfo Given a callback and an optional sound to play on error, this will return a validator information structure to be used with validateInputWith().
delCustomData ( string $key ) : Node Remove a key/value from the registry.
dontAcceptPrePromptInputAsInput ( ) : Node Digits entered during the pre prompt messages are not considered as node input.
endInputWith ( string $digit ) : Node Configures a specific digit as the end of input digit.
executeAfterFailedValidation ( Closure $callback ) : Node Executes after the 1st failed validation.
executeAfterRun ( Closure $callback ) : Node Executes after running the node.
executeBeforeRun ( Closure $callback ) : Node Executes before running the node.
executeOnInputFailed ( Closure $callback ) : Node Executes a callback when the node fails to properly get input from the user (either because of cancel, max attempts reached, timeout).
executeOnValidInput ( Closure $callback ) : Node Specify a callback function to invoke when the user entered a valid input.
expectAtLeast ( integer $length ) : Node Configure the node to expect at least this many digits. The input is considered complete when this many digits has been entered. Cancel and end of input digits (if configured) are not taken into account.
expectAtMost ( integer $length ) : Node Configure the node to expect at most this many digits. The reading loop will try to read this many digits.
expectExactly ( integer $length ) : Node Configure this node to expect at least and at most this many digits.
getClient ( ) : PAGI\Client\IClient Returns the agi client in use.
getCustomData ( string $key ) : mixed Returns the value for the given key in the registry.
getInput ( ) : string Returns input.
getName ( ) : string Returns the node name.
getTotalInputAttemptsUsed ( ) : integer Returns the total number of input attempts used by the user.
hasCustomData ( string $key ) : boolean True if the given key exists in the registry.
hasInput ( ) : boolean True if this node has at least 1 digit as input, excluding cancel and end of input digits.
inputLengthIsAtLeast ( integer $length ) : boolean True if this node has at least this many digits entered.
isComplete ( ) : boolean True if this node is in COMPLETE state.
isTimeout ( ) : boolean True if this node is in TIMEOUT state.
loadValidatorsFrom ( array $validatorsInformation ) : Node Given an array of validator information structures, this will load all validators into this node.
maxAttemptsForInput ( integer $number ) : Node Specify a maximum attempt number for the user to enter a valid input.
maxInputsReached ( ) : boolean True if the user reached the maximum allowed attempts for valid input.
maxTimeBetweenDigits ( integer $milliseconds ) : Node Configures the maximum time available between digits before a timeout.
maxTotalTimeForInput ( integer $milliseconds ) : Node Configures the maximum time available for the user to enter valid input per attempt.
playNoInputMessageOnLastAttempt ( ) : Node Forces to play "no input" message on last attempt too.
playOnMaxValidInputAttempts ( string $filename ) : Node Optional message to play when the user exhausted all the available attempts to enter a valid input.
playOnNoInput ( string $filename ) : Node Specify an optional message to play when the user did not enter any input at all. By default, will NOT be played if this happens in the last allowed attempt.
prePromptMessagesNotInterruptable ( ) : Node Make pre prompt messages not interruptable
run ( ) : Node Executes this node.
saveCustomData ( string $key, mixed $value ) : Node Saves a custom key/value to the registry.
sayDateTime ( integer $timestamp, string $format ) : Node Loads a prompt message for saying a date/time expressed by a unix timestamp and a format.
sayDigits ( integer $digits ) : Node Loads a prompt message for saying the digits of the given number.
sayNumber ( integer $number ) : Node Loads a prompt message for saying a number.
saySound ( string $filename ) : Node Loads a prompt message for playing an audio file.
setAgiClient ( PAGI\Client\IClient $client ) : Node Sets the pagi client to use by this node.
setName ( string $name ) : Node Gives a name for this node.
unInterruptablePrompts ( ) : Node Make prompt messages not interruptable.
validate ( ) : boolean Calls all validators in order. Will stop when any of them returns false.
validateInputWith ( string $name, Closure $validation, string | null $soundOnError = null ) : Node Add an input validation to this node.
wasCancelled ( ) : boolean True if this node is in CANCEL state.

Protected Methods

Method Description
acceptInput ( string $digit ) : void Process a single digit input by the user. Changes the node state according to the digit entered (CANCEL, COMPLETE).
addClientMethodCall ( ) : void Internally used to execute prompt messages in the agi client.
addPrePromptClientMethodCall ( ) : void Internally used to execute pre prompt messages in the agi client.
appendInput ( string $digit ) : void Appends an input to the node input.
beforeOnInputFailed ( ) : void Convenient hook to execute before calling the onInputFailed callback.
beforeOnValidInput ( ) : void Convenient hook to execute before calling the onValidInput callback.
callClientMethod ( string $name, array $arguments = [] ) : PAGI\Client\Result\IResult Call a specific method on a client.
callClientMethods ( methodInfo[] $methods, Closure $stopWhen = null ) : PAGI\Client\Result\IResult Calls methods in the PAGI client.
clearPrePromptMessages ( ) : void Internally used to clear pre prompt messages after being played.
doInput ( ) : void Internally used to accept input from the user. Plays pre prompt messages, prompt, and waits for a complete input or cancel.
evaluateInput ( string $digit ) : integer Returns the kind of digit entered by the user, CANCEL, END, NORMAL.
inputIsCancel ( string $digit ) : boolean True if the digit matches the cancel digit.
inputIsEnd ( string $digit ) : boolean True if the digit matches the end of input digit.
logDebug ( string $msg ) : void Used internally to log debug messages
playPrePromptMessages ( ) : PAGI\Client\Result\IResult Internally used to play all pre prompt queued messages. Clears the queue after it.
playPromptMessages ( ) : PAGI\Client\Result\IResult | null Plays pre prompt messages, like error messages from validations.
resetInput ( ) : Node Internally used to clear the input per input attempt. Also resets state to TIMEOUT.
stateToString ( integer $state ) : string Maps the current node state to a human readable string.

Method Details

__toString() public method

A classic!
public __toString ( ) : string
return string

acceptInput() protected method

Process a single digit input by the user. Changes the node state according to the digit entered (CANCEL, COMPLETE).
protected acceptInput ( string $digit ) : void
$digit string
return void

addClientMethodCall() protected method

Internally used to execute prompt messages in the agi client.
protected addClientMethodCall ( ) : void
return void

addPrePromptClientMethodCall() protected method

Internally used to execute pre prompt messages in the agi client.
protected addPrePromptClientMethodCall ( ) : void
return void

addPrePromptMessage() public method

Adds a sound file to play as a pre prompt message.
public addPrePromptMessage ( string $filename ) : void
$filename string
return void

appendInput() protected method

Appends an input to the node input.
protected appendInput ( string $digit ) : void
$digit string A single character, one of the DTMF_* constants.
return void

beforeOnInputFailed() protected method

Convenient hook to execute before calling the onInputFailed callback.
protected beforeOnInputFailed ( ) : void
return void

beforeOnValidInput() protected method

Convenient hook to execute before calling the onValidInput callback.
protected beforeOnValidInput ( ) : void
return void

callClientMethod() protected method

Call a specific method on a client.
protected callClientMethod ( string $name, array $arguments = [] ) : PAGI\Client\Result\IResult
$name string
$arguments array
return PAGI\Client\Result\IResult

callClientMethods() protected method

Calls methods in the PAGI client.
protected callClientMethods ( methodInfo[] $methods, Closure $stopWhen = null ) : PAGI\Client\Result\IResult
$methods methodInfo[] Methods to call, an array of arrays. The second array has the method name as key and an array of arguments as value.
$stopWhen Closure If any, this callback is evaluated before returning. Will return when false.
return PAGI\Client\Result\IResult

cancelWith() public method

Configures a specific digit as the cancel digit.
public cancelWith ( string $digit ) : Node
$digit string A single character, one of the DTMF_* constants.
return Node

cancelWithInputRetriesInput() public method

Allow the user to retry input by pressing the cancel digit after entered one or more digits. For example, when entering a 12 number pin, the user might press the cancel digit at the 5th digit to re-enter it. This counts as a failed input, but will not cancel the node. The node will be cancelled only if the user presses the cancel digit with NO input at all.
public cancelWithInputRetriesInput ( ) : Node
return Node

clearPrePromptMessages() protected method

Internally used to clear pre prompt messages after being played.
protected clearPrePromptMessages ( ) : void
return void

clearPromptMessages() public method

Removes prompt messages.
public clearPromptMessages ( ) : Node
return Node

createValidatorInfo() public static method

Given a callback and an optional sound to play on error, this will return a validator information structure to be used with validateInputWith().
public static createValidatorInfo ( Closure $validation, string | null $soundOnError = null ) : validatorInfo
$validation Closure Callback to use as validator
$soundOnError string | null Sound file to play on error
return validatorInfo

delCustomData() public method

Remove a key/value from the registry.
public delCustomData ( string $key ) : Node
$key string
return Node

doInput() protected method

Internally used to accept input from the user. Plays pre prompt messages, prompt, and waits for a complete input or cancel.
protected doInput ( ) : void
return void

dontAcceptPrePromptInputAsInput() public method

Digits entered during the pre prompt messages are not considered as node input.
public dontAcceptPrePromptInputAsInput ( ) : Node
return Node

endInputWith() public method

Configures a specific digit as the end of input digit.
public endInputWith ( string $digit ) : Node
$digit string A single character, one of the DTMF_* constants.
return Node

evaluateInput() protected method

Returns the kind of digit entered by the user, CANCEL, END, NORMAL.
protected evaluateInput ( string $digit ) : integer
$digit string A single character, one of the DTMF_* constants.
return integer

executeAfterFailedValidation() public method

Executes after the 1st failed validation.
public executeAfterFailedValidation ( Closure $callback ) : Node
$callback Closure
return Node

executeAfterRun() public method

Executes after running the node.
public executeAfterRun ( Closure $callback ) : Node
$callback Closure
return Node

executeBeforeRun() public method

Executes before running the node.
public executeBeforeRun ( Closure $callback ) : Node
$callback Closure
return Node

executeOnInputFailed() public method

Executes a callback when the node fails to properly get input from the user (either because of cancel, max attempts reached, timeout).
public executeOnInputFailed ( Closure $callback ) : Node
$callback Closure
return Node

executeOnValidInput() public method

Specify a callback function to invoke when the user entered a valid input.
public executeOnValidInput ( Closure $callback ) : Node
$callback Closure
return Node

expectAtLeast() public method

Configure the node to expect at least this many digits. The input is considered complete when this many digits has been entered. Cancel and end of input digits (if configured) are not taken into account.
public expectAtLeast ( integer $length ) : Node
$length integer
return Node

expectAtMost() public method

Configure the node to expect at most this many digits. The reading loop will try to read this many digits.
public expectAtMost ( integer $length ) : Node
$length integer
return Node

expectExactly() public method

Configure this node to expect at least and at most this many digits.
public expectExactly ( integer $length ) : Node
$length integer
return Node

getClient() public method

Returns the agi client in use.
public getClient ( ) : PAGI\Client\IClient
return PAGI\Client\IClient

getCustomData() public method

Returns the value for the given key in the registry.
public getCustomData ( string $key ) : mixed
$key string
return mixed

getInput() public method

Returns input.
public getInput ( ) : string
return string

getName() public method

Returns the node name.
public getName ( ) : string
return string

getTotalInputAttemptsUsed() public method

Returns the total number of input attempts used by the user.

hasCustomData() public method

True if the given key exists in the registry.
public hasCustomData ( string $key ) : boolean
$key string
return boolean

hasInput() public method

True if this node has at least 1 digit as input, excluding cancel and end of input digits.
public hasInput ( ) : boolean
return boolean

inputIsCancel() protected method

True if the digit matches the cancel digit.
protected inputIsCancel ( string $digit ) : boolean
$digit string A single character, one of the DTMF_* constants.
return boolean

inputIsEnd() protected method

True if the digit matches the end of input digit.
protected inputIsEnd ( string $digit ) : boolean
$digit string A single character, one of the DTMF_* constants.
return boolean

inputLengthIsAtLeast() public method

True if this node has at least this many digits entered.
public inputLengthIsAtLeast ( integer $length ) : boolean
$length integer
return boolean

isComplete() public method

True if this node is in COMPLETE state.
public isComplete ( ) : boolean
return boolean

isTimeout() public method

True if this node is in TIMEOUT state.
public isTimeout ( ) : boolean
return boolean

loadValidatorsFrom() public method

Given an array of validator information structures, this will load all validators into this node.
public loadValidatorsFrom ( array $validatorsInformation ) : Node
$validatorsInformation array
return Node

logDebug() protected method

Used internally to log debug messages
protected logDebug ( string $msg ) : void
$msg string
return void

maxAttemptsForInput() public method

Defaults to 1.
public maxAttemptsForInput ( integer $number ) : Node
$number integer
return Node

maxInputsReached() public method

True if the user reached the maximum allowed attempts for valid input.
public maxInputsReached ( ) : boolean
return boolean

maxTimeBetweenDigits() public method

Configures the maximum time available between digits before a timeout.
public maxTimeBetweenDigits ( integer $milliseconds ) : Node
$milliseconds integer
return Node

maxTotalTimeForInput() public method

Configures the maximum time available for the user to enter valid input per attempt.
public maxTotalTimeForInput ( integer $milliseconds ) : Node
$milliseconds integer
return Node

playNoInputMessageOnLastAttempt() public method

Forces to play "no input" message on last attempt too.
public playNoInputMessageOnLastAttempt ( ) : Node
return Node

playOnMaxValidInputAttempts() public method

Optional message to play when the user exhausted all the available attempts to enter a valid input.
public playOnMaxValidInputAttempts ( string $filename ) : Node
$filename string Sound file to play.
return Node

playOnNoInput() public method

Specify an optional message to play when the user did not enter any input at all. By default, will NOT be played if this happens in the last allowed attempt.
public playOnNoInput ( string $filename ) : Node
$filename string Sound file to play.
return Node

playPrePromptMessages() protected method

Internally used to play all pre prompt queued messages. Clears the queue after it.
protected playPrePromptMessages ( ) : PAGI\Client\Result\IResult
return PAGI\Client\Result\IResult

playPromptMessages() protected method

Plays pre prompt messages, like error messages from validations.
protected playPromptMessages ( ) : PAGI\Client\Result\IResult | null
return PAGI\Client\Result\IResult | null

prePromptMessagesNotInterruptable() public method

Make pre prompt messages not interruptable
public prePromptMessagesNotInterruptable ( ) : Node
return Node

resetInput() protected method

Internally used to clear the input per input attempt. Also resets state to TIMEOUT.
protected resetInput ( ) : Node
return Node

run() public method

Executes this node.
public run ( ) : Node
return Node

saveCustomData() public method

Saves a custom key/value to the registry.
public saveCustomData ( string $key, mixed $value ) : Node
$key string
$value mixed
return Node

sayDateTime() public method

Loads a prompt message for saying a date/time expressed by a unix timestamp and a format.
public sayDateTime ( integer $timestamp, string $format ) : Node
$timestamp integer
$format string
return Node

sayDigits() public method

Loads a prompt message for saying the digits of the given number.
public sayDigits ( integer $digits ) : Node
$digits integer
return Node

sayNumber() public method

Loads a prompt message for saying a number.
public sayNumber ( integer $number ) : Node
$number integer
return Node

saySound() public method

Loads a prompt message for playing an audio file.
public saySound ( string $filename ) : Node
$filename string
return Node

setAgiClient() public method

Sets the pagi client to use by this node.
public setAgiClient ( PAGI\Client\IClient $client ) : Node
$client PAGI\Client\IClient
return Node

setName() public method

Gives a name for this node.
public setName ( string $name ) : Node
$name string
return Node

stateToString() protected method

Maps the current node state to a human readable string.
protected stateToString ( integer $state ) : string
$state integer One of the STATE_* constants.
return string

unInterruptablePrompts() public method

Make prompt messages not interruptable.
public unInterruptablePrompts ( ) : Node
return Node

validate() public method

Calls all validators in order. Will stop when any of them returns false.
public validate ( ) : boolean
return boolean

validateInputWith() public method

Add an input validation to this node.
public validateInputWith ( string $name, Closure $validation, string | null $soundOnError = null ) : Node
$name string A distrinctive name for this validator
$validation Closure Callback to use for validation
$soundOnError string | null Optional sound to play on error
return Node

wasCancelled() public method

True if this node is in CANCEL state.
public wasCancelled ( ) : boolean
return boolean

Property Details

$state protected_oe property

Node state.
protected int $state
return integer