Location: PHPKode > scripts > PHP Grammar > php-grammar/createGrammar.php
<?php

require ('grammar.php');

/*
************************************************************

createGRAMMAR SUB CLASS

Use this class to create W3C Grammars in XML format.

Written by Mark Headd, http://www.voiceingov.org
This work is licensed under a Creative Commons License
http://creativecommons.org/licenses/by-nc-sa/2.0/


************************************************************
*/


class createGrammar extends grammar
{

/*
************************************************************
FUNCTION TO SET LANGUAGE ATTRIBUTE
************************************************************
*/
function setLang($lang) 
{
$this->language=$lang;
}

/*
************************************************************
FUNCTION TO SET ROOT RULE NAME (DEFAULT IS "mainRule")
************************************************************
*/
function setRoot($root_name) 
{
$this->rootRule=$root_name;
}

/*
************************************************************
FUNCTION TO SET RULE IDENTIFIER (DEFAULT IS "mainRule")
************************************************************
*/
function setRuleID($ruleid) 
{
$this->rule_id=$ruleid;
}

/*
************************************************************
FUNCTION TO SET SLOT NAME
************************************************************
*/
function setSlot($slot_name)
{
$this->slot=$slot_name;
}

/*
**************************************************
FUNCTION FOR CREATING SIMPLE GRAMMARS

This function takes in 1 or 2 arrays and creates
a simple grammar file.  The first array is used to
create a simple list of alternate choices that
can be matched against a caller utterance. 

The second array is optional, and is used to
indicate values that are returned to the VoiceXML
field utilizing the grammar.  The second array 
should have the same number of elements as the
first. 

**************************************************
*/

function simpleGram($list, $tags="")
{
$this->gramStart();
$this->ruleStart();
$this->oneOfstart();

// Iterate through the $list array and create items for each
reset($list);
$list_value = current($list);

// If return values are passed, 
   if ($tags!="") {
   reset($tags);
   $tags_value = current($tags);
}
while($list_value) {
$this->itemStart();
echo $list_value;
    if ($tags!="") {
    $this->tag($tags_value);
}
$this->itemEnd();
$list_value = next($list);

		if ($tags!="") {
		$tags_value = next($tags);
}
}
$this->oneOfend();
$this->ruleEnd();
$this->gramEnd();
}

/*
**************************************************
FUNCTION FOR CREATING COMPOUND GRAMMARS

This function takes in 1 or 2 arrays and creates
a compound grammar file.  The first array is a 
multidimensional array that can contain two or 
more sub arrays (if you only have one list of 
items that a user will select from, use the 
SIMPLE GRAMMARS function above.

The second array is optional, and is used to
indicate values that are returned to the VoiceXML
field utilizing the grammar.  The second array 
should have the same number of elements as there
are arrays in the first (multidimensional) array. 

**************************************************
*/

function compoundGram($choices, $tags="")
{
// Construct main rule with references to sub rules
$this->gramStart();
$this->ruleStart();
$this->oneOfstart();

if ($tags!="") {
  reset($tags);
  $tags_value = current($tags);
}
for($i=0; $i<count($choices); $i++) {
$this->itemStart();
$rule_num = "#rule_$i";
$this->ruleRef("$rule_num");
if ($tags!="") {
    $this->tag($tags_value);
}
$this->itemEnd();
if ($tags!="") {
	 	$tags_value = next($tags);
}
}
$this->oneOfend();
$this->ruleEnd();

// Construct sub rules using each sub array in $choices array

for($i=0; $i<count($choices); $i++) {
$rule_num = "rule_$i";

$this->ruleStart("private", $rule_num);
$this->oneOfstart();

for($j=0; $j<count($choices[$i]); $j++) {
$this->itemStart();
echo $choices[$i][$j];
$this->itemEnd();
}
$this->oneOfend();
$this->ruleEnd();
}

$this->gramEnd();
}

}

?>
Return current item: PHP Grammar