| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
abstract class HC3_Ui_Abstract_Collection |
| 3 |
{ |
| 4 |
protected $uiType = NULL; |
| 5 |
protected $children = array(); |
| 6 |
protected $gutter = 2; |
| 7 |
protected $separated = NULL; |
| 8 |
protected $tags = array(); |
| 9 |
|
| 10 |
public function __construct( $children = array() ) |
| 11 |
{ |
| 12 |
$this->children = $children; |
| 13 |
} |
| 14 |
|
| 15 |
public function tag( $tag, $details = NULL ) |
| 16 |
{ |
| 17 |
$details = ($details === NULL) ? $tag : $details; |
| 18 |
$this->tags[ $tag ] = $details; |
| 19 |
return $this; |
| 20 |
} |
| 21 |
|
| 22 |
public function getTags() |
| 23 |
{ |
| 24 |
return $this->tags; |
| 25 |
} |
| 26 |
|
| 27 |
public function getUiType() |
| 28 |
{ |
| 29 |
return $this->uiType; |
| 30 |
} |
| 31 |
|
| 32 |
public function gutter( $gutter ) |
| 33 |
{ |
| 34 |
$this->gutter = $gutter; |
| 35 |
return $this; |
| 36 |
} |
| 37 |
|
| 38 |
public function separated( $separated = TRUE ) |
| 39 |
{ |
| 40 |
$this->separated = $separated; |
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
public function __toString() |
| 45 |
{ |
| 46 |
return '' . $this->render(); |
| 47 |
} |
| 48 |
|
| 49 |
protected function _findKey() |
| 50 |
{ |
| 51 |
$key = count($this->children); |
| 52 |
while( array_key_exists($key, $this->children) ){ |
| 53 |
$key++; |
| 54 |
} |
| 55 |
return $key; |
| 56 |
} |
| 57 |
|
| 58 |
protected function _insertAtPos( $pos, $key, $child = NULL ) |
| 59 |
{ |
| 60 |
if( $child === NULL ){ |
| 61 |
$child = $key; |
| 62 |
$key = $this->_findKey(); |
| 63 |
} |
| 64 |
|
| 65 |
$this->children = array_merge( |
| 66 |
array_slice( $this->children, 0, $pos ), |
| 67 |
array( $key => $child ), |
| 68 |
array_slice( $this->children, $pos ) |
| 69 |
); |
| 70 |
|
| 71 |
// _print_r( $this->children ); |
| 72 |
return $this; |
| 73 |
} |
| 74 |
|
| 75 |
public function addAfter( $afterKey, $key, $child = NULL ) |
| 76 |
{ |
| 77 |
$index = FALSE; |
| 78 |
if( NULL !== $afterKey ){ |
| 79 |
$keys = array_keys( $this->children ); |
| 80 |
$index = array_search( $afterKey, $keys ); |
| 81 |
} |
| 82 |
|
| 83 |
$pos = FALSE === $index ? count($this->children) : $index + 1; |
| 84 |
$this->_insertAtPos( $pos, $key, $child ); |
| 85 |
|
| 86 |
return $this; |
| 87 |
} |
| 88 |
|
| 89 |
public function addBefore( $beforeKey, $key, $child = NULL ) |
| 90 |
{ |
| 91 |
$index = FALSE; |
| 92 |
if( NULL !== $beforeKey ){ |
| 93 |
$keys = array_keys( $this->children ); |
| 94 |
$index = array_search( $beforeKey, $keys ); |
| 95 |
} |
| 96 |
elseif( '_end_' == $beforeKey ){ |
| 97 |
$index = count( $this->children ); |
| 98 |
} |
| 99 |
|
| 100 |
$pos = FALSE === $index ? 0 : $index; |
| 101 |
$this->_insertAtPos( $pos, $key, $child ); |
| 102 |
|
| 103 |
return $this; |
| 104 |
} |
| 105 |
|
| 106 |
public function add( $key, $child = NULL ) |
| 107 |
{ |
| 108 |
if( $child === NULL ){ |
| 109 |
$child = $key; |
| 110 |
$key = $this->_findKey(); |
| 111 |
} |
| 112 |
|
| 113 |
$this->children[ $key ] = $child; |
| 114 |
|
| 115 |
// _print_r( $this->children ); |
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
public function remove( $child ) |
| 120 |
{ |
| 121 |
$keys = array_keys($this->children); |
| 122 |
foreach( $keys as $k ){ |
| 123 |
if( $this->children[$k] === $child ){ |
| 124 |
unset( $this->children[$k] ); |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
return $this; |
| 129 |
} |
| 130 |
|
| 131 |
public function setChild( $k, $child ) |
| 132 |
{ |
| 133 |
if( array_key_exists($k, $this->children) ){ |
| 134 |
$this->children[$k] = $child; |
| 135 |
} |
| 136 |
return $this; |
| 137 |
} |
| 138 |
|
| 139 |
public function getChildren() |
| 140 |
{ |
| 141 |
return $this->children; |
| 142 |
} |
| 143 |
|
| 144 |
public function render() |
| 145 |
{ |
| 146 |
$return = ''; |
| 147 |
foreach( $this->children as $child ){ |
| 148 |
if( ! is_array($child) ){ |
| 149 |
$return .= '' . $child; |
| 150 |
} |
| 151 |
} |
| 152 |
return $return; |
| 153 |
} |
| 154 |
} |