Source for file SimpleTableRow.php

Documentation is available at SimpleTableRow.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23.  
  24. import('sylar.common.system.ExceptionInSylar');
  25. import('sylar.common.system.ConfigBox');
  26. import('sylar.common.system.Logger');
  27. import('sylar.common.data.DataContainer');
  28. import('sylar.common.data.SimpleTableHeader');
  29.  
  30. /**
  31. * Sylar_SimpleTableRow
  32. *
  33. * it define the structure and methods of a simple row of Sylar_SimpleTable container.
  34. * the row must be an array indexed whit same header. For example
  35. *
  36. * Table name: test
  37. * TbHeader:
  38. * name surname phone email
  39. * TbRow 0 Nik Mallet 090909 nik@nik.ir
  40. * TbRow 1 Bob Bruk 0877689 bon@bob.com
  41. * TbRow ...
  42. * TbRow n Tim Ork 078 778 tim@tim.xx
  43. *
  44. * Header info must be specified only one time and every row must be a Sylar_SimpleTableRow with an array
  45. *
  46. * @see Sylar_SimpleTable
  47. *
  48. * @package Sylar
  49. * @version 1.0
  50. * @since 22/feb/08
  51. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  52. * @copyright Sylar Development Team
  53. */
  54. class Sylar_SimpleTableRow extends Sylar_DataContainer{
  55. private $arrayData;
  56. private $columns;
  57. /**
  58. * Constructor
  59. * reset data and set the number of columns defined
  60. *
  61. * @since 22/feb/08
  62. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  63. *
  64. * @param array $arrayDataRow
  65. * @param int $columns specify number of columns of the row, if false then it calculates it from dataArray
  66. */
  67. function __construct($arrayDataRow, $columns=false){
  68. // If not specified it counts columns from data array
  69. if(!$columns){
  70. $columns = count($arrayDataRow);
  71. }
  72. $this->setColumns($columns);
  73. $this->resetArrayData();
  74. $this->fillRowData($arrayDataRow);
  75. }
  76. function __destruct(){
  77. // nothing to do at the moment
  78. }
  79. // Getter and Setter
  80. // --------------------------------
  81. public function setColumns($iColumns){
  82. $this->columns = $iColumns;
  83. }
  84. public function getColumns(){
  85. return $this->columns;
  86. }
  87. // Public methods
  88. // --------------------------------
  89. /**
  90. * Get data from Row
  91. * Return the array that contains data
  92. *
  93. * @since 22/feb/08
  94. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  95. *
  96. * @todo to be done
  97. *
  98. * @return array
  99. */
  100. public function getData(){
  101. return $this->arrayData;
  102. }
  103. /**
  104. * Check data
  105. * Return tru if row contains data, false otherwise
  106. *
  107. * @since 03/2008
  108. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  109. *
  110. * @return boolean
  111. */
  112. public function hasData(){
  113. if($this->isRowDataSet() && $this->getColumns()>0){
  114. return true;
  115. }else{
  116. return false;
  117. }
  118. }
  119. /**
  120. * fill data into the row
  121. * it also compares the lenght of array with the number of column declared into the table, if needed.
  122. * return true if all is ok, false on error
  123. *
  124. * @since 22/feb/08
  125. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  126. *
  127. * @return boolean
  128. * @param array $arrayDataRow
  129. */
  130. public function fillRowData($arrayDataRow){
  131. if(!$this->isRowDataSet()){
  132. throw new ExceptionInSylar("The array provided to fill TableRow is not an array. [Sylar_SimpleTableRow::fillRowData]");
  133. return false;
  134. }
  135. if($this->isColumnsNumControlEnabled() && $this->getColumns()!=count($arrayDataRow)){
  136. throw new ExceptionInSylar("The number of columns in array is different from TableRow columns. [Sylar_SimpleTableRow::fillRowData]");
  137. return false;
  138. }
  139. // If we are here is all ok!
  140. $this->arrayData = $arrayDataRow;
  141. return true;
  142. }
  143. /**
  144. * it returns the value at column $columnId of the TableRow
  145. *
  146. * @since 22/feb/08
  147. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  148. *
  149. * @param int $columnId
  150. * @return mixed
  151. */
  152. public function getColumnValue($columnId){
  153. if(!$this->isRowDataSet()){
  154. throw new ExceptionInSylar("The TableRow is not Set. [Sylar_SimpleTableRow::getColumnValue]");
  155. return false;
  156. }
  157. if(!array_key_exists($columnId, $this->arrayData) ){
  158. throw new ExceptionInSylar("The number of columns ".$columnId." does not exists. [Sylar_SimpleTableRow::fillRowData]");
  159. return false;
  160. }
  161. return $this->arrayData[$columnId];
  162. }
  163. // Private Methods
  164. // --------------------------------
  165. /**
  166. * reset data container
  167. * If is set destroy and reset the data container array
  168. *
  169. * @since 22/feb/08
  170. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  171. *
  172. * @return void
  173. */
  174. private function resetArrayData(){
  175. if(isset($this->arrayData)){
  176. unset($this->arrayData);
  177. }
  178. $this->arrayData = array();
  179. }
  180. /**
  181. * Check Columns control
  182. * It verify if the control on columns number is on return true, false otherwise.
  183. *
  184. * @since 22/feb/08
  185. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  186. *
  187. * @return boolean
  188. */
  189. private function isColumnsNumControlEnabled(){
  190. if(!$this->getColumns()){
  191. return false;
  192. }
  193. return true;
  194. }
  195. /**
  196. * Check data container
  197. * it verify that the container of data is set correctly and return true, return false otherwise
  198. *
  199. * @since 22/feb/08
  200. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  201. *
  202. * @return boolean
  203. */
  204. private function isRowDataSet(){
  205. if(!is_array($this->arrayData)){
  206. return false;
  207. }
  208. return true;
  209. }
  210. }
  211. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:21 +0200 by phpDocumentor 1.3.0RC3