Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.twoDimensionalArray.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * A two dim struct
8  *
9  * This struct gives some helpers to manipulate rows and columns (addRow, addColumn, insertColumn etc...)
10  *
11  * @author Anakeen
12  *
13  */
15 {
16 
17  private $x = array();
18  private $y = array();
19 
20  private $data = array();
21 
22  private $emptyValue = "";
23  private $errorMessage = array();
24  /**
25  * Construct a two dim object
26  *
27  * @param array $data two dim array to init the object
28  *
29  * @return TwoDimensionStruct
30  */
31  public function __construct(Array $data = null)
32  {
33  if (!is_null($data)) {
34  foreach ($data as $y => $column) {
35  if (is_array($column)) {
36  foreach ($column as $x => $value) {
37  $this->setValue($x, $y, $value, true);
38  }
39  } else {
40  $this->addErrorMessage(sprintf("The array have to be a two dimensional array for all the column"));
41  }
42  }
43  }
44  return $this;
45  }
46  /**
47  * Get the x row
48  *
49  * @param int $x number of the row
50  *
51  * @return array|NULL
52  */
53  public function getRow($x)
54  {
55  if (isset($this->y[$x])) {
56  $xUUID = $this->x[$x];
57  $row = array();
58  foreach ($this->y as $key => $yUUID) {
59  if (isset($this->data[$xUUID]) && isset($this->data[$xUUID][$yUUID])) {
60  $row[$key] = $this->data[$xUUID][$yUUID];
61  } else {
62  $row[$key] = $this->emptyValue;
63  }
64  }
65  return $row;
66  } else {
67  $this->addErrorMessage(sprintf("Unable to get row %s out of the border", $x));
68  return null;
69  }
70  }
71  /**
72  * Set the values for a row
73  *
74  * @param int $x number of the row
75  * @param array $row values
76  * @param boolean $force force to add if the row is bigger than the column size or $x > the row number
77  *
78  * @return TwoDimensionStruct|NULL
79  */
80  public function setRow($x, Array $row, $force = false)
81  {
82  if ((count($row) < count($this->y)) || $force == true) {
83  foreach ($row as $key => $value) {
84  if (is_null($this->setValue($x, $key, $value, $force))) {
85  return null;
86  }
87  }
88  return $this;
89  } else {
90  $this->addErrorMessage(sprintf("Unable to set a row bigger than the column size (%s < %s)", count($this->y) , count($row)));
91  return null;
92  }
93  }
94  /**
95  * Insert a row at $x
96  *
97  * @param int $x number of the row
98  * @param array $row values
99  * @param boolean $force force to add if the row is bigger than the column size
100  *
101  * @return TwoDimensionStruct|NULL
102  */
103  public function insertRow($x, Array $row = array() , $force = false)
104  {
105  if ($x < count($this->x) && ((count($row) < count($this->y)) || $force == true)) {
106  array_splice($this->x, $x, 0, array(
107  uniqid()
108  ));
109  $this->x = array_values($this->x);
110  return $this->setRow($x, $row, $force);
111  } else {
112  $this->addErrorMessage(sprintf("Unable to set a row bigger than the column size (%s < %s)", count($this->y) , count($row)));
113  return null;
114  }
115  }
116  /**
117  * Add a row at the end of the struct or
118  *
119  * @param array $row values
120  * @param int $x number of the row
121  *
122  * @return TwoDimensionStruct|NULL
123  */
124  public function addRow(Array $row = array() , $x = null)
125  {
126  if (is_null($x)) {
127  return $this->setRow(count($this->x) , $row, true);
128  } elseif ($x >= count($this->x)) {
129  return $this->setRow($x, $row, true);
130  } else {
131  $this->addErrorMessage(sprintf("Use insert row to insert a row"));
132  return null;
133  }
134  }
135  /**
136  * delete a row
137  *
138  * @param int $x number of the row
139  *
140  * @return TwoDimensionStruct|NULL
141  */
142  public function deleteRow($x)
143  {
144  if (isset($this->x[$x])) {
145  unset($this->x[$x]);
146  $this->x = array_values($this->x);
147  return $this;
148  } else {
149  $this->addErrorMessage(sprintf("Unable to delete row $x out of the border", $x));
150  return null;
151  }
152  }
153  /**
154  * Set a column values
155  *
156  * @param int $y number of the column
157  * @param array $column values
158  * @param boolean $force force to add if the column is bigger than the row size
159  *
160  * @return TwoDimensionStruct|NULL
161  */
162  public function setColumn($y, Array $column, $force = false)
163  {
164  if ((count($column) < count($this->x)) || $force == true) {
165  foreach ($column as $key => $value) {
166  if (is_null($this->setValue($key, $y, $value, $force))) {
167  return null;
168  }
169  }
170  return $this;
171  } else {
172  $this->addErrorMessage(sprintf("Unable to set a column bigger than the row size (%s < %s)", count($this->x) , count($column)));
173  return null;
174  }
175  }
176  /**
177  * Insert a column
178  *
179  * @param int $y number of the column
180  * @param array $column values
181  * @param boolean $force force the add
182  *
183  * @return TwoDimensionStruct|NULL
184  */
185  public function insertColumn($y, Array $column = array() , $force = false)
186  {
187  if ($y < count($this->y)) {
188  array_splice($this->y, $y, 0, array(
189  uniqid()
190  ));
191  $this->y = array_values($this->y);
192  return $this->setColumn($y, $column, $force);
193  } else {
194  $this->addErrorMessage(sprintf("Unable to set a row bigger than the column size (%s < %s)", count($this->y) , $y));
195  return null;
196  }
197  }
198  /**
199  * Add a column
200  *
201  * @param array $column values
202  * @param int $y number of the column
203  *
204  * @return TwoDimensionStruct|NULL
205  */
206  public function addColumn(Array $column = array() , $y = null)
207  {
208  if (is_null($y)) {
209  return $this->setColumn(count($this->y) , $column, true);
210  } elseif ($y >= count($this->y)) {
211  return $this->setColumn($y, $column, true);
212  } else {
213  $this->addErrorMessage(sprintf("Use insert column to insert a column"));
214  return null;
215  }
216  }
217  /**
218  * Delete the column
219  *
220  * @param int $y number of the column
221  *
222  * @return boolean|NULL
223  */
224  public function deleteColumn($y)
225  {
226  if (isset($this->y[$y])) {
227  unset($this->y[$y]);
228  $this->y = array_values($this->y);
229  return true;
230  } else {
231  $this->addErrorMessage(sprintf("Unable to delete column $y out of the border", $y));
232  return null;
233  }
234  }
235  /**
236  * get a column
237  *
238  * @param int $y number of the column
239  *
240  * @return array|NULL
241  */
242  public function getColumn($y)
243  {
244  if (isset($this->y[$y])) {
245  $yUUID = $this->y[$y];
246  $column = array();
247  foreach ($this->x as $key => $xUUID) {
248  if (isset($this->data[$xUUID]) && isset($this->data[$xUUID][$yUUID])) {
249  $column[$key] = $this->data[$xUUID][$yUUID];
250  } else {
251  $column[$key] = $this->emptyValue;
252  }
253  }
254  return $column;
255  } else {
256  $this->addErrorMessage(sprintf("Unable to get column $y out of the border", $y));
257  return null;
258  }
259  }
260  /**
261  * Set a value
262  *
263  * @param int $x number of the row
264  * @param int $y number of the column
265  * @param string $value value to set
266  * @param boolean $force force the add if x and y are outside the array
267  *
268  * @return NULL|TwoDimensionStruct
269  */
270  public function setValue($x, $y, $value, $force = false)
271  {
272  if (isset($this->y[$y]) && isset($this->x[$x])) {
273  $this->data[$this->x[$x]][$this->y[$y]] = $value;
274  } elseif ($force) {
275  if (!isset($this->x[$x])) {
276  for ($i = count($this->x); $i <= $x; $i++) {
277  $this->x[$i] = uniqid();
278  }
279  }
280  if (!isset($this->y[$y])) {
281  for ($i = count($this->y); $i <= $y; $i++) {
282  $this->y[$i] = uniqid();
283  }
284  }
285  $this->data[$this->x[$x]][$this->y[$y]] = $value;
286  } else {
287  $this->addErrorMessage(sprintf("Unable to set x : %s, y :%s, value : %s", $x, $y, $value));
288  return null;
289  }
290  return $this;
291  }
292  /**
293  * Get a value
294  *
295  * @param int $x number of the row
296  * @param int $y number of the column
297  *
298  * @return string|NULL
299  */
300  public function getValue($x, $y)
301  {
302  if (isset($this->y[$y]) && isset($this->x[$x])) {
303  if (isset($this->data[$this->x[$x]][$this->y[$y]])) $value = $this->data[$this->x[$x]][$this->y[$y]];
304  else $value = null;
305  return is_null($value) ? $this->emptyValue : $value;
306  } elseif (count($x) >= $x && count($y) >= $y) {
307  return $this->emptyValue;
308  } else {
309  $this->addErrorMessage(sprintf("Unable to get x : %s, y :%s out of the border", $x, $y));
310  return null;
311  }
312  }
313  /**
314  * Get the two dim array
315  *
316  * @return array
317  */
318  public function getArray()
319  {
320  $nbX = count($this->x);
321  $nbY = count($this->y);
322 
323  $returnArray = array();
324  for ($x = 0; $x < $nbX; $x++) {
325  $returnArray[$x] = array();
326  for ($y = 0; $y < $nbY; $y++) {
327  $returnArray[$x][$y] = $this->getValue($x, $y);
328  }
329  }
330  return $returnArray;
331  }
332  /**
333  * Get the default empty value
334  *
335  * @return string the $emptyValue
336  */
337  public function getEmptyValue()
338  {
339  return $this->emptyValue;
340  }
341  /**
342  * Set a default emptyValue
343  *
344  * @param string $emptyValue
345  *
346  * @return TwoDimensionStruct
347  */
348  public function setEmptyValue($emptyValue)
349  {
350  $this->emptyValue = $emptyValue;
351  return $this;
352  }
353  /**
354  * Add an error message
355  *
356  * @param string $error
357  */
358  private function addErrorMessage($error)
359  {
360  $this->errorMessage[] = $error;
361  }
362  /**
363  * Get the last error message
364  *
365  * @return string|NULL
366  */
367  public function getLastErrorMessage()
368  {
369  if (count($this->errorMessage)) {
370  $lastMessage = end($this->errorMessage);
371  reset($this->errorMessage);
372  return $lastMessage;
373  }
374  return null;
375  }
376  /**
377  * Get all the array message
378  *
379  * @return array
380  */
381  public function getAllErrorMessages()
382  {
383  return $this->errorMessage;
384  }
385 }
386 ?>
insertRow($x, Array $row=array(), $force=false)
addRow(Array $row=array(), $x=null)
addColumn(Array $column=array(), $y=null)
setColumn($y, Array $column, $force=false)
insertColumn($y, Array $column=array(), $force=false)
$force
setRow($x, Array $row, $force=false)
setValue($x, $y, $value, $force=false)
$value
← centre documentaire © anakeen