PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/Opl/Opt/Instruction/Foreach.php

https://bitbucket.org/kandsten/hitta.sverok.se
PHP | 115 lines | 65 code | 11 blank | 39 comment | 3 complexity | d47120cf36189c698c5e1f2992f05d46 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. /*
  3. * OPEN POWER LIBS <http://www.invenzzia.org>
  4. *
  5. * This file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE. It is also available through
  7. * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
  8. *
  9. * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
  10. * and other contributors. See website for details.
  11. *
  12. * $Id: Foreach.php 231 2009-09-19 07:13:14Z zyxist $
  13. */
  14. /**
  15. * This processor implements the opt:foreach instruction.
  16. */
  17. class Opt_Instruction_Foreach extends Opt_Instruction_Loop
  18. {
  19. /**
  20. * The processor name
  21. * @var String
  22. */
  23. protected $_name = 'foreach';
  24. /**
  25. * The current nesting level of "opt:for"
  26. * @var Integer
  27. */
  28. protected $_nesting = 0;
  29. /**
  30. * Configures the processor.
  31. */
  32. public function configure()
  33. {
  34. $this->_addInstructions(array('opt:foreach', 'opt:foreachelse'));
  35. } // end configure();
  36. /**
  37. * Processes the "opt:foreach" node.
  38. * @param Opt_Xml_Node $node The node found by the compiler
  39. */
  40. public function processNode(Opt_Xml_Node $node)
  41. {
  42. switch($node->getName())
  43. {
  44. case 'foreach':
  45. $params = array(
  46. 'array' => array(0 => self::REQUIRED, self::EXPRESSION),
  47. 'value' => array(0 => self::REQUIRED, self::ID),
  48. 'index' => array(0 => self::OPTIONAL, self::ID, null),
  49. 'separator' => array(0 => self::OPTIONAL, self::EXPRESSION, null)
  50. );
  51. $this->_extractAttributes($node, $params);
  52. $this->_nesting++;
  53. $node->sort(array('*' => 0, 'opt:foreachelse' => 1));
  54. $list = $node->getElementsByTagNameNS('opt', 'foreachelse', false);
  55. // Determine, if we are using opt:foreachelse, because it requires a bit different code from us.
  56. $codeBegin = ' foreach('.$params['array'].' as '.(!is_null($params['index']) ? '$__fe'.$this->_nesting.'_idx => ' : '').'$__fe'.$this->_nesting.'_val){ ';
  57. switch(sizeof($list))
  58. {
  59. case 0:
  60. break;
  61. case 1:
  62. $codeBegin = 'if(sizeof('.$params['array'].') > 0){ '.$codeBegin;
  63. break;
  64. default:
  65. throw new Opt_InstructionTooManyItems_Exception('opt:foreachelse', $node->getXmlName());
  66. }
  67. // Register everything in the buffer.
  68. $node->addBefore(Opt_Xml_Buffer::TAG_BEFORE, $codeBegin);
  69. $node->addAfter(Opt_Xml_Buffer::TAG_AFTER, ' } ');
  70. $this->_compiler->setConversion('##var_'.$params['value'], '$__fe'.$this->_nesting.'_val');
  71. if(!is_null($params['index']))
  72. {
  73. $this->_compiler->setConversion('##var_'.$params['index'], '$__fe'.$this->_nesting.'_idx');
  74. }
  75. // This instruction supports separators.
  76. $this->processSeparator('$__foreach_'.$this->_nesting, $params['separator'], $node);
  77. $node->set('postprocess', true);
  78. $this->_process($node);
  79. $node->set('params', $params);
  80. break;
  81. case 'foreachelse':
  82. if($node->getParent()->getName() != 'foreach')
  83. {
  84. throw new Opt_InstructionInvalidParent_Exception($node->getXmlName(), 'opt:foreach');
  85. }
  86. $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, '} } else { ');
  87. $this->_process($node);
  88. break;
  89. }
  90. } // end processNode();
  91. /**
  92. * In the postprocessing, we decrement the nesting level and unregister
  93. * the conversions set in the processing stage.
  94. *
  95. * @param Opt_Xml_Node $node The node found by the compiler.
  96. */
  97. public function postprocessNode(Opt_Xml_Node $node)
  98. {
  99. $params = $node->get('params');
  100. $this->_compiler->unsetConversion('##var_'.$params['value']);
  101. $this->_compiler->unsetConversion('##var_'.$params['index']);
  102. $this->_nesting--;
  103. } // end postprocessNode();
  104. } // end Opt_Instruction_Foreach;