Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axTableRowGroupHelper.class.php
Go to the documentation of this file.
00001 <?php
00016 class axTableRowGroupHelper extends axBaseHelper {
00017     
00028     protected $_type;
00029     
00034     protected $_filter = array();
00035     
00041     protected $_callbacks = array();
00042     
00047     protected $_before_content;
00048     
00053     protected $_before_callback;
00054     
00059     protected $_after_content;
00060     
00065     protected $_after_callback;
00066     
00077     public function __construct ($type) {
00078         switch (strtolower($type)) {
00079             case 'head':
00080             case 'thead':
00081                 $this->_type = "head";
00082                 parent::__construct('thead');
00083                 break;
00084                 
00085             case 'foot':
00086             case 'tfoot':
00087                 $this->_type = "foot";
00088                 parent::__construct('tfoot');
00089                 break;
00090                 
00091             case 'body':
00092             case 'tbody':
00093             default:
00094                 $this->_type = "body";
00095                 parent::__construct('tbody');
00096         }
00097     }
00098     
00103     public function getType () {
00104         return $this->_type;
00105     }
00106     
00111     public function getFilter () {
00112         return $this->_filter;
00113     }
00114     
00120     public function setFilter (array $filter) {
00121         $this->_filter = $filter;
00122         return $this;
00123     }
00124     
00129     public function getColumnCallback ($key) {
00130         return isset($this->_callbacks[$key]) ? $this->_callbacks[$key] : null;
00131     }
00132     
00140     public function setColumnCallback ($key, $callback) {
00141         if (is_string($callback) && !is_callable($callback))
00142             $callback = callback($callback);
00143         
00144         if (!is_callable($callback))
00145             throw new InvalidArgumentException("Invalid callback provided");
00146             
00147         $this->_callbacks[$key] = $callback;
00148         return $this;
00149     }
00150     
00156     public function setColumnCallbacks (array $callbacks) {
00157         try {
00158             foreach ($callbacks as $key => $callback)
00159                 $this->setColumnCallback($key, $callback);
00160         }
00161         catch (Exception $e) {
00162             $this->_callbacks = array();
00163             throw new RuntimeException("Cannot set callbacks");
00164         }
00165         return $this;
00166     }
00167     
00185     public function before ($content, $replace_callback = null) {
00186         if ($replace_callback) {
00187             if (is_string($replace_callback) && !is_callable($replace_callback))
00188                 $replace_callback = callback($replace_callback);
00189             
00190             if (!is_callable($replace_callback))
00191                 throw new InvalidArgumentException("Provided callback is invalid");
00192         }
00193         
00194         $this->_before_content = (array)$content;
00195         $this->_before_callback = $replace_callback;
00196         return $this;
00197     }
00198     
00207     public function after ($content, $replace_callback = null) {
00208         if ($replace_callback) {
00209             if (is_string($replace_callback) && !is_callable($replace_callback))
00210                 $replace_callback = callback($replace_callback);
00211             
00212             if (!is_callable($replace_callback))
00213                 throw new InvalidArgumentException("Provided callback is invalid");
00214         }
00215             
00216         $this->_after_content = (array)$content;
00217         $this->_after_callback = $replace_callback;
00218         return $this;
00219     }
00220     
00232     public function addRows ($rows, $cell_type = "auto") {
00233         if (!is_array($rows) && !$rows instanceof Traversable)
00234             throw new InvalidArgumentException("First parameter is expected to be array or Traversable, " . gettype($rows) . " given", 3003);
00235         
00236         if ($cell_type == "auto")
00237             $cell_type = ($this->_type == "thead" || $this->_type == "head") ? "head" : "data";
00238             
00239         foreach ($rows as $row) {
00240             $this->addRow($row, $cell_type);
00241         }
00242         return $this;
00243     }
00244     
00257     public function addRow ($values, $cell_type = "auto") {
00258         if (is_scalar($values))
00259             $values = array($values);
00260         elseif ($values instanceof axModel)
00261             $values = $values->getData();
00262         elseif (!is_array($values) && !$values instanceof Traversable) {
00263             throw new InvalidArgumentException(
00264                 "First parameter is expected to be scalar, array or axModel, ".get_class($values)." given", 3002
00265             );
00266         }
00267             
00268         if ($cell_type == "auto")
00269             $cell_type = ($this->_type == "thead" || $this->_type == "head")  ? "head" : "data";
00270             
00271         if (!empty($this->_filter))
00272             $values = array_intersect_key($values, array_flip($this->_filter));
00273         
00274         foreach (array_intersect_key($values, $this->_callbacks) as $key => $value) {
00275             $values[$key] = $this->_callbacks[$key]($value);
00276         }
00277         
00278         if (!empty($this->_before_content)) {
00279             if (!empty($this->_before_callback)) {
00280                 $alpha = $this->_before_callback;
00281                 $before = (array)$alpha($this->_before_content, $values);
00282             }
00283             else
00284                 $before = $this->_before_content;
00285                 
00286             $values = array_merge($before, array_values($values));
00287         }
00288         
00289         if (!empty($this->_after_content)) {
00290             if (!empty($this->_after_callback)) {
00291                 $alpha = $this->_after_callback;
00292                 $after = (array)$alpha($this->_after_content, $values);
00293             }
00294             else
00295                 $after = $this->_after_content;
00296                 
00297             $values = array_merge(array_values($values), $after);
00298         }
00299         
00300         $this->appendChild(axTableRowHelper::export($values, $cell_type));
00301         return $this;
00302     }
00303     
00307     public function __toString () {
00308         $attr = array();
00309         foreach ($this->_attributes as $name => $value) {
00310             $attr[] = "$name=\"$value\"";
00311         }
00312         $node = "<{$this->_node_name} " . implode(' ', $attr) . ">";
00313 
00314         if (count($this->_children))
00315             $node .= implode($this->_children);
00316 
00317         return $node . "</{$this->_node_name}>";
00318     }
00319     
00326     public static function export ($type) {
00327         return new self ($type);
00328     }
00329 }
 All Data Structures Files Functions Variables