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:
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:
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:
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.