PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Nano/library/Nano/Element.php

http://mvh-source.googlecode.com/
PHP | 250 lines | 188 code | 48 blank | 14 comment | 30 complexity | ed3dda5c900d1caf21dc69c9587fc516 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. class Nano_Element{
  3. const DEFAULT_DECORATOR = 'Nano_Element_Decorator';
  4. const DEFAULT_TYPE = 'div';
  5. protected $type;
  6. protected $decorator;
  7. private $parent;
  8. private $children;
  9. private $attributes;
  10. private $content;
  11. private $isVertile;
  12. public function __construct( $type = null, array $attributes = null, $content = null ){
  13. $this->setAttributes( (array) $attributes )
  14. ->setContent( $content )
  15. ->setType( $type );
  16. }
  17. public final function setVertile( $vertile = true ){
  18. $this->isVertile = $vertile;
  19. return $this;
  20. }
  21. public final function vertile(){
  22. return (bool) $this->isVertile;
  23. }
  24. public function setType( $type ){
  25. $this->type = $type;
  26. return $this;
  27. }
  28. public function getType(){
  29. if( null == $this->type ){
  30. $this->setType( self::DEFAULT_TYPE );
  31. }
  32. return $this->type;
  33. }
  34. public function __toString(){
  35. return (string) $this->getDecorator();
  36. }
  37. protected function getDecorator(){
  38. if( null == $this->decorator ){
  39. $this->setDecorator( self::DEFAULT_DECORATOR );
  40. }
  41. return new $this->decorator( $this );
  42. }
  43. protected function setDecorator( $decorator ){
  44. $this->decorator = $decorator;
  45. }
  46. public function getContent(){
  47. return $this->content;
  48. }
  49. public function setContent( $value = null ){
  50. if( $value instanceof Nano_Element ){
  51. $this->addChild( $value );
  52. }
  53. else if( null != $value ){
  54. $this->content = new Nano_Collection();
  55. $this->addContent( $value );
  56. }
  57. return $this;
  58. }
  59. public function addContent( $value ){
  60. if( null === $value ){
  61. return $this;
  62. }
  63. if( null === $this->getContent() ){
  64. $this->setContent( $value );
  65. }
  66. else{
  67. $content = $this->getContent();
  68. $content[] = $value;
  69. //$this->getContent()->push( $value );
  70. }
  71. return $this;
  72. }
  73. public function addChild( $args ){
  74. $children = $this->getChildren();
  75. $args = func_get_args();
  76. $element = array_shift( $args );
  77. if( ! $element instanceof Nano_Element ){
  78. $attributes = array_shift( $args );
  79. $content = array_shift( $args );
  80. $element = new Nano_Element( $element, $attributes, $content );
  81. }
  82. $children[] = $element;
  83. return $this;
  84. }
  85. /**
  86. * Factory function: accepts an array as constructor options for Nano_Elements
  87. * $attributes may contain a special key 'content'
  88. *
  89. * @param array $children Array of type => attributes
  90. * @return $this liquid
  91. */
  92. public function addChildren( $children ){
  93. if( ! is_array( $children ) ){
  94. return;
  95. }
  96. foreach( $children as $type => $attributes ){
  97. $config = (object) array_merge(array(
  98. 'children' => null,
  99. 'content' => null
  100. ), $attributes );
  101. unset( $attributes['children'] );
  102. unset( $attributes['content'] );
  103. $child = new Nano_Element( $type, $attributes, $config->content );
  104. $child->addChildren( $config->children );
  105. $this->addChild( $child );
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Collect children of this element defined by $select
  111. * Note that this function returns any match!
  112. *
  113. * @param array $select Select may contain 'type', or any other attribute
  114. */
  115. public function findChildren( array $select = array() ){
  116. $children = $this->getChildren();
  117. if( count( $select ) == 0 ){
  118. return $children;
  119. }
  120. $config = (object) array_merge( array(
  121. 'type' => null,
  122. 'id' => null,
  123. 'className' => null,
  124. 'attributes' => null
  125. ), $select );
  126. unset( $select['type'] );
  127. unset( $select['id'] );
  128. unset( $select['id'] );
  129. $config->attributes = $select;
  130. $collect = array();
  131. foreach( $children as $child ){
  132. if( ( $config->type && $child->getType() == $config->type )
  133. || ($config->id && $child->getAttribute('id') == $config->id)
  134. || ($config->className && $child->getAttribute('class') == $config->clasName )
  135. ){
  136. $collect[] = $child;
  137. }
  138. else if( count($config->attributes) > 0 ){
  139. foreach( $config->attributes as $key => $value ){
  140. if( $child->getAttribute( $key ) == $value ){
  141. $collect[] = $child;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. return $collect;
  148. }
  149. public function recursivelyFindChildren( array $select = array() ){
  150. $collect = $this->findChildren( $select );
  151. foreach( $this->getChildren() as $child ){
  152. array_merge( $collect, $child->recursivelyFindChildren($select));
  153. }
  154. return $collect;
  155. }
  156. public function setParent( Nano_Element $element ){
  157. $this->parent = $element;
  158. return $this;
  159. }
  160. public function getParent(){
  161. return $this->parent;
  162. }
  163. public function getChildren(){
  164. if( null === $this->children ){
  165. $this->children = new Nano_Collection();
  166. }
  167. return $this->children;
  168. }
  169. public function getAttributes(){
  170. if( null === $this->attributes ){
  171. $this->attributes = new Nano_Collection();
  172. }
  173. return $this->attributes;
  174. }
  175. public function setAttributes( array $attributes ){
  176. foreach( $attributes as $key => $value ){
  177. $this->setAttribute( $key, $value );
  178. }
  179. return $this;
  180. }
  181. public function setAttribute( $name, $value ){
  182. $attributes = $this->getAttributes();
  183. $attributes[$name] = $value;
  184. return $this;
  185. }
  186. public function getAttribute( $name ){
  187. return $this->getAttributes()->$name;
  188. }
  189. public function removeAttribute( $name ){
  190. $attr = $this->getAttributes();
  191. $value = $attr->$name;
  192. $attr->$name = null;
  193. return $value;
  194. }
  195. public function hasChildren(){
  196. return true;
  197. if( count( $this->getChildren() ) > 0 ){
  198. return true;
  199. }
  200. return false;
  201. }
  202. }