block.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Tree data structure, used for rendering purposes.
  4. *
  5. * @package pQuery
  6. * @author Taddeus Kroes
  7. * @version 1.0
  8. */
  9. class Block {
  10. /**
  11. *
  12. *
  13. * @var int
  14. */
  15. static $count = 0;
  16. /**
  17. * The unique id of this block.
  18. *
  19. * @var int
  20. */
  21. var $id;
  22. /**
  23. * The block's name.
  24. *
  25. * @var string
  26. */
  27. var $name;
  28. /**
  29. * An optional parent block.
  30. *
  31. * If NULL, this block is the root of the data tree.
  32. *
  33. * @var Block
  34. */
  35. var $parent;
  36. /**
  37. * Child blocks.
  38. *
  39. * @var array
  40. */
  41. var $children = array();
  42. /**
  43. * Variables in this block.
  44. *
  45. * All variables in a block are also available in its descendants through {@link get()}.
  46. *
  47. * @var array
  48. */
  49. var $vars = array();
  50. /**
  51. * Constructor.
  52. *
  53. * The id of the block is determined by the block counter.
  54. *
  55. * @param string $name The block's name.
  56. * @param Block &$parent A parent block (optional).
  57. * @see id, name, parent
  58. * @uses $count
  59. */
  60. function __construct($name=null, &$parent=null) {
  61. $this->id = ++self::$count;
  62. $this->name = $name;
  63. $this->parent = $parent;
  64. }
  65. /**
  66. * Set the value of one or more variables in the block.
  67. *
  68. * @param string|array $vars Either a single variable name, or a set of name/value pairs.
  69. * @param mixed $value The value of the single variable to set.
  70. * @returns Block This block.
  71. */
  72. function set($name, $value=null) {
  73. if( is_array($name) ) {
  74. foreach( $name as $var => $val )
  75. $this->vars[$var] = $val;
  76. } else
  77. $this->vars[$name] = $value;
  78. return $this;
  79. }
  80. /**
  81. * Get the value of a variable.
  82. *
  83. * @param string $name The name of the variable to get the value of.
  84. * @return mixed The value of the variable if it exists, NULL otherwise.
  85. */
  86. function get($name) {
  87. // Variable inside this block
  88. if( isset($this->vars[$name]) )
  89. return $this->vars[$name];
  90. // Variable in one of parents
  91. if( $this->parent !== null )
  92. return $this->parent->get($name);
  93. // If the tree's root block does not have the variable, it does not exist
  94. return null;
  95. }
  96. /**
  97. * Get the value of a variable.
  98. *
  99. * This method is an equivalent of {@link get()}.
  100. *
  101. * @param string $name The name of the variable to get the value of.
  102. * @return mixed The value of the variable if it exists, NULL otherwise.
  103. */
  104. function __get($name) {
  105. return $this->get($name);
  106. }
  107. /**
  108. * Add a child block.
  109. *
  110. * @param string $name The name of the block to add.
  111. * @param array $data Data to add to the created block (optional).
  112. * @returns Block The created block.
  113. */
  114. function add($name, $data=array()) {
  115. array_push($this->children, $block = new self($name, $this));
  116. return $block->set($data);
  117. }
  118. /**
  119. * Find all child blocks with a specified name.
  120. *
  121. * @param string $name The name of the blocks to find.
  122. * @returns array The positively matched blocks.
  123. */
  124. function find($name) {
  125. return array_filter($this->children,
  126. create_function('$c', 'return $c->name === "'.$name.'";'));
  127. }
  128. /**
  129. * Remove a child block.
  130. *
  131. * @param Block &$child The block to remove.
  132. * @returns Block This block.
  133. */
  134. function remove_child(&$child) {
  135. foreach( $this->children as $i => $block ) {
  136. if( $block->id == $child->id ) {
  137. array_splice($this->children, $i, 1);
  138. $block->parent = null;
  139. }
  140. }
  141. return $this;
  142. }
  143. /**
  144. * Remove this block from its parent.
  145. *
  146. * @returns Block The removed block.
  147. */
  148. function remove() {
  149. !is_null($this->parent) && $this->parent->remove_child($this);
  150. return $this;
  151. }
  152. }
  153. ?>