5.5.3. Parameter parsing

This is a guide to the parameter parsing in OPT. As we mentioned, the compiler does not do it automatically, because you have to tell it, what you want to get and how to parse it. In the code above, the block methods receive the $attributes parameter, which contains the whole attribute string. You have two choices now: to parse it manually, or to use the parametrize() method. This is a sample code from one of the built-in instructions:

Example 5.6. Sample parameter parser configuration

$params = array(
	'name' => array(OPT_PARAM_REQUIRED, OPT_PARAM_ID),
	'order' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_STRING, NULL),
	'state' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_EXPRESSION, NULL),
	'datasource' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_EXPRESSION, NULL)
);
$this -> compiler -> parametrize('section', $attributes, $params);

We build an assotiative array, where we match another arrays to the indexes representing the parameter names. These arrays contain two or three elements:

  1. Whether it is optional or not: OPT_PARAM_REQUIRED or OPT_PARAM_OPTIONAL.
  2. The type of the data:
    • OPT_PARAM_ID - an identifier containing letters, numbers and the underline.
    • OPT_PARAM_STRING - a string
    • OPT_PARAM_NUMBER - a number
    • OPT_PARAM_VARIABLE - an OPT variable (it must begin with @).
    • OPT_PARAM_EXPRESSION - an OPT expression (assignment operator not allowed)
    • OPT_PARAM_ASSIGN_EXPR - an OPT expression (assignment operator allowed).
  3. Optional value (set only, if you have chosen OPT_PARAM_OPTIONAL in the first item).

Remember that the required parameters must be specified before the optional ones! After creating the array, you call the parametrize() method with the following parameters:

  1. The instruction name (only for debug purposes).
  2. The attribute string
  3. The array (note this parameter requires a reference).

The method will process the string and replace the values in the $params array with the compiled content. Now we can use $params['name'], when we want to put the name parameter somewhere.

To show it in practise, we are going to modify the instruction from the previous example. Now it will take a block or variable, and make the code inside a bit dynamic: if the block is not set, the content between the tags will not be displayed.

Example 5.7. Parameter parsing

<?php
	class optFirst extends optInstruction
	{
		public function configure()
		{
			return array(
				// processor name
				0 => 'first',
				// instructions
				'first' => OPT_MASTER,
				'/first' => OPT_ENDER
			);
		} // end configure();
		
		public function instructionNodeProcess(ioptNode $node)
		{
			foreach($node as $block)
			{
				switch($block -> getName())
				{
				case 'first':
					$this -> firstBegin($block -> getAttributes());
					$this -> defaultTreeProcess($block);
					break;						
				case '/first':
					$this -> firstEnd();
					break;
				}			
			}		
		} // end process();
		
		private function firstBegin($attributes)
		{
			$params = array(
				'test' => array(OPT_PARAM_REQUIRED,
					 OPT_PARAM_EXPRESSION)
			);
			$this -> compiler -> parametrize('first',
				$attributes, $params);
			// A dynamic code
			$this -> compiler -> out(' if(isset('.$params['test'].')){ ');
		} // end firstBegin();
		
		private function firstEnd()
		{
			$this -> compiler -> out(' } ');
		} // end firstEnd();
	}	
?>

The OPT parameter parser can do more than we have already shown. There are some extra issues:

  1. By default, OPT supports both of the parameter styles: =value1; value2; value3; and name1="value" name2="value" name3="value". However, you can force using only one of them with the fourth, optional parameter of the parametrize() method. It takes the following values: OPT_STYLE_BOTH - both styles accepted; OPT_STYLE_OPT - only the OPT style; OPT_STYLE_XML - only the HTML/XML style.
  2. This method can also handle unlimited number of parameters. To describe, how to parse undefined parameters, add a special parameter named __UNKNOWN__. All the parameters not appearing on the list will be now parsed using this pattern and returned by the method as a separate array:

Example 5.8. Undefined parameters

$params = array(
	'__UNKNOWN__' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_EXPRESSION, NULL)
);
$undef = $this -> compiler -> parametrize('include', $attributes, $params);

The undefined parameters are kept in $undef assotiative array, whereas the defined ones - in $params.