5.5.5. Handling OPT attributes

The OPT attributes are redirected to the processAttribute() method, which you have to extend in order to capture them. The method takes one parameter: the object of optBlock class with the attribute data.

Example 5.10. Handling OPT attributes

<?php
	class optFirst extends optInstruction // 1
	{
		public function configure()
		{
			return array( // 2
				// processor name
				0 => 'first',
				// instructions
				'first' => OPT_MASTER,
				'/first' => OPT_ENDER,
				'attribute' => OPT_ATTRIBUTE,
				'attr2' => OPT_ATTRIBUTE
			);
		} // end configure();
		
		public function instructionNodeProcess(ioptNode $node)
		{
			foreach($node as $block) // 3
			{
				switch($block -> getName()) // 4
				{
				case 'first':
					$this -> firstBegin($block-> getAttributes());
					$this -> defaultTreeProcess($block); // 5
					break;						
				case '/first':
					$this -> firstEnd();
					break;
				}			
			}		
		} // end process();
		
		private function firstBegin($attributes)
		{
			$this -> compiler -> out('Beginning tag<br/>', true); // 6
		} // end firstBegin();
		
		private function firstEnd()
		{
			$this -> compiler -> out('Ending tag<br/>', true);
		} // end firstEnd();
					
		public function processAttribute(optBlock $block)
		{
			switch($block -> getName())
			{
				case 'attribute':
					$this -> compiler -> out(' echo \'Im an attribute!\'; ');
					break;	
				case 'attr2':
					$this -> compiler -> out(' echo \'Im another attribute!\'; ');
					break;
			}					
		} // end processAttribute();
	}
?>

The attributes are first registered in the configure() method and then we choose, what to do with them, with a simple switch statement. The method must generate some PHP code for the compiler.