Platform  3.1
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  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * A two dim struct
9  *
10  * This struct gives some helpers to manipulate rows and columns (addRow, addColumn, insertColumn etc...)
11  *
12  * @author anakeen
13  *
14  */
16 {
17 
18  private $x = array();
19  private $y = array();
20 
21  private $data = array();
22 
23  private $emptyValue = "";
24  private $errorMessage = array();
25  /**
26  * Construct a two dim object
27  *
28  * @param array $data two dim array to init the object
29  *
30  * @return TwoDimensionStruct
31  */
32  public function __construct(Array $data = null)
33  {
34  if (!is_null($data)) {
35  foreach ($data as $y => $column) {
36  if (is_array($column)) {
37  foreach ($column as $x => $value) {
38  $this->setValue($x, $y, $value, true);
39  }
40  } else {
41  $this->addErrorMessage(sprintf("The array have to be a two dimensional array for all the column"));
42  }
43  }
44  }
45  return $this;
46  }
47  /**
48  * Get the x row
49  *
50  * @param int $x number of the row
51  *
52  * @return array|NULL
53  */
54  public function getRow($x)
55  {
56  if (isset($this->y[$x])) {
57  $xUUID = $this->x[$x];
58  $row = array();
59  foreach ($this->y as $key => $yUUID) {
60  if (isset($this->data[$xUUID]) && isset($this->data[$xUUID][$yUUID])) {
61  $row[$key] = $this->data[$xUUID][$yUUID];
62  } else {
63  $row[$key] = $this->emptyValue;
64  }
65  }
66  return $row;
67  } else {
68  $this->addErrorMessage(sprintf("Unable to get row $s out of the border", $x));
69  return null;
70  }
71  }
72  /**
73  * Set the values for a row
74  *
75  * @param int $x number of the row
76  * @param array $row values
77  * @param boolean $force force to add if the row is bigger than the column size or $x > the row number
78  *
79  * @return TwoDimensionStruct|NULL
80  */
81  public function setRow($x, Array $row, $force = false)
82  {
83  if ((count($row) < count($this->y)) || $force == true) {
84  foreach ($row as $key => $value) {
85  if (is_null($this->setValue($x, $key, $value, $force))) {
86  return null;
87  }
88  }
89  return $this;
90  } else {
91  $this->addErrorMessage(sprintf("Unable to set a row bigger than the column size (%s < %s)", count($this->y) , count($row)));
92  return null;
93  }
94  }
95  /**
96  * Insert a row at $x
97  *
98  * @param int $x number of the row
99  * @param array $row values
100  * @param boolean $force force to add if the row is bigger than the column size
101  *
102  * @return TwoDimensionStruct|NULL
103  */
104  public function insertRow($x, Array $row = array() , $force = false)
105  {
106  if ($x < count($this->x) && ((count($row) < count($this->y)) || $force == true)) {
107  array_splice($this->x, $x, 0, array(
108  uniqid()
109  ));
110  $this->x = array_values($this->x);
111  return $this->setRow($x, $row, $force);
112  } else {
113  $this->addErrorMessage(sprintf("Unable to set a row bigger than the column size (%s < %s)", count($this->y) , count($row)));
114  return null;
115  }
116  }
117  /**
118  * Add a row at the end of the struct or
119  *
120  * @param array $row values
121  * @param int $x number of the row
122  *
123  * @return TwoDimensionStruct|NULL
124  */
125  public function addRow(Array $row = array() , $x = null)
126  {
127  if (is_null($x)) {
128  return $this->setRow(count($this->x) , $row, true);
129  } elseif ($x >= count($this->x)) {
130  return $this->setRow($x, $row, true);
131  } else {
132  $this->addErrorMessage(sprintf("Use insert row to insert a row"));
133  return null;
134  }
135  }
136  /**
137  * delete a row
138  *
139  * @param int $x number of the row
140  *
141  * @return TwoDimensionStruct|NULL
142  */
143  public function deleteRow($x)
144  {
145  if (isset($this->x[$x])) {
146  unset($this->x[$x]);
147  $this->x = array_values($this->x);
148  return $this;
149  } else {
150  $this->addErrorMessage(sprintf("Unable to delete row $x out of the border", $x));
151  return null;
152  }
153  }
154  /**
155  * Set a column values
156  *
157  * @param int $y number of the column
158  * @param array $column values
159  * @param boolean $force force to add if the column is bigger than the row size
160  *
161  * @return TwoDimensionStruct|NULL
162  */
163  public function setColumn($y, Array $column, $force = false)
164  {
165  if ((count($column) < count($this->x)) || $force == true) {
166  foreach ($column as $key => $value) {
167  if (is_null($this->setValue($key, $y, $value, $force))) {
168  return null;
169  }
170  }
171  return $this;
172  } else {
173  $this->addErrorMessage(sprintf("Unable to set a column bigger than the row size (%s < %s)", count($this->x) , count($column)));
174  return null;
175  }
176  }
177  /**
178  * Insert a column
179  *
180  * @param int $y number of the column
181  * @param array $column values
182  * @param boolean $force force the add
183  *
184  * @return TwoDimensionStruct|NULL
185  */
186  public function insertColumn($y, Array $column = array() , $force = false)
187  {
188  if ($y < count($this->y)) {
189  array_splice($this->y, $y, 0, array(
190  uniqid()
191  ));
192  $this->y = array_values($this->y);
193  return $this->setColumn($y, $column, $force);
194  } else {
195  $this->addErrorMessage(sprintf("Unable to set a row bigger than the column size (%s < %s)", count($this->y) , count($row)));
196  return null;
197  }
198  }
199  /**
200  * Add a column
201  *
202  * @param array $column values
203  * @param int $y number of the column
204  *
205  * @return TwoDimensionStruct|NULL
206  */
207  public function addColumn(Array $column = array() , $y = null)
208  {
209  if (is_null($y)) {
210  return $this->setColumn(count($this->y) , $column, true);
211  } elseif ($y >= count($this->y)) {
212  return $this->setColumn($y, $column, true);
213  } else {
214  $this->addErrorMessage(sprintf("Use insert column to insert a column"));
215  return null;
216  }
217  }
218  /**
219  * Delete the column
220  *
221  * @param int $y number of the column
222  *
223  * @return boolean|NULL
224  */
225  public function deleteColumn($y)
226  {
227  if (isset($this->y[$y])) {
228  unset($this->y[$y]);
229  $this->y = array_values($this->y);
230  return true;
231  } else {
232  $this->addErrorMessage(sprintf("Unable to delete column $y out of the border", $y));
233  return null;
234  }
235  }
236  /**
237  * get a column
238  *
239  * @param int $y number of the column
240  *
241  * @return array|NULL
242  */
243  public function getColumn($y)
244  {
245  if (isset($this->y[$y])) {
246  $yUUID = $this->y[$y];
247  $column = array();
248  foreach ($this->x as $key => $xUUID) {
249  if (isset($this->data[$xUUID]) && isset($this->data[$xUUID][$yUUID])) {
250  $column[$key] = $this->data[$xUUID][$yUUID];
251  } else {
252  $column[$key] = $this->emptyValue;
253  }
254  }
255  return $column;
256  } else {
257  $this->addErrorMessage(sprintf("Unable to get column $y out of the border", $y));
258  return null;
259  }
260  }
261  /**
262  * Set a value
263  *
264  * @param int $x number of the row
265  * @param int $y number of the column
266  * @param string $value value to set
267  * @param boolean $force force the add if x and y are outside the array
268  *
269  * @return NULL|TwoDimensionStruct
270  */
271  public function setValue($x, $y, $value, $force = false)
272  {
273  if (isset($this->y[$y]) && isset($this->x[$x])) {
274  $this->data[$this->x[$x]][$this->y[$y]] = $value;
275  } elseif ($force) {
276  if (!isset($this->x[$x])) {
277  for ($i = count($this->x); $i <= $x; $i++) {
278  $this->x[$i] = uniqid();
279  }
280  }
281  if (!isset($this->y[$y])) {
282  for ($i = count($this->y); $i <= $y; $i++) {
283  $this->y[$i] = uniqid();
284  }
285  }
286  $this->data[$this->x[$x]][$this->y[$y]] = $value;
287  } else {
288  $this->addErrorMessage(sprintf("Unable to set x : $s, y :$s, value : $s", $x, $y, $value));
289  return null;
290  }
291  return $this;
292  }
293  /**
294  * Get a value
295  *
296  * @param int $x number of the row
297  * @param int $y number of the column
298  *
299  * @return string|NULL
300  */
301  public function getValue($x, $y)
302  {
303  if (isset($this->y[$y]) && isset($this->x[$x])) {
304  $value = $this->data[$this->x[$x]][$this->y[$y]];
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 the $emptyValue
336  */
337  public function getEmptyValue()
338  {
339  return $this->emptyValue;
340  }
341  /**
342  * Set a default emptyValue
343  *
344  * @param field_type $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 ?>
← centre documentaire © anakeen - published under CC License - Dynacase