PageRenderTime 65ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/application/datamapper/array.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 290 lines | 167 code | 21 blank | 102 comment | 18 complexity | 65b0799bafdf7a82473848a82251b10f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Array Extension for DataMapper classes.
  4. *
  5. * Quickly convert DataMapper models to-and-from PHP arrays.
  6. *
  7. * @license MIT License
  8. * @package DMZ-Included-Extensions
  9. * @category DMZ
  10. * @author Phil DeJarnett
  11. * @link http://www.overzealous.com/dmz/pages/extensions/array.html
  12. * @version 1.0
  13. */
  14. // --------------------------------------------------------------------------
  15. /**
  16. * DMZ_Array Class
  17. *
  18. * @package DMZ-Included-Extensions
  19. */
  20. class DMZ_Array {
  21. /**
  22. * Convert a DataMapper model into an associative array.
  23. * If the specified fields includes a related object, the ids from the
  24. * objects are collected into an array and stored on that key.
  25. * This method does not recursively add objects.
  26. *
  27. * @param DataMapper $object The DataMapper Object to convert
  28. * @param array $fields Array of fields to include. If empty, includes all database columns.
  29. * @return array An associative array of the requested fields and related object ids.
  30. */
  31. function to_array($object, $fields = '')
  32. {
  33. // assume all database columns if $fields is not provided.
  34. if(empty($fields))
  35. {
  36. $fields = $object->fields;
  37. }
  38. else
  39. {
  40. $fields = (array) $fields;
  41. }
  42. $result = array();
  43. foreach($fields as $f)
  44. {
  45. // handle related fields
  46. if(array_key_exists($f, $object->has_one) || array_key_exists($f, $object->has_many))
  47. {
  48. // each related item is stored as an array of ids
  49. // Note: this method will NOT get() the related object.
  50. $rels = array();
  51. foreach($object->{$f} as $item)
  52. {
  53. $rels[] = $item->id;
  54. }
  55. $result[$f] = $rels;
  56. }
  57. else
  58. {
  59. // just the field.
  60. $result[$f] = $object->{$f};
  61. }
  62. }
  63. return $result;
  64. }
  65. /**
  66. * Convert the entire $object->all array result set into an array of
  67. * associative arrays.
  68. *
  69. * @see to_array
  70. * @param DataMapper $object The DataMapper Object to convert
  71. * @param array $fields Array of fields to include. If empty, includes all database columns.
  72. * @return array An array of associative arrays.
  73. */
  74. function all_to_array($object, $fields = '')
  75. {
  76. // loop through each object in the $all array, convert them to
  77. // an array, and add them to a new array.
  78. $result = array();
  79. foreach($object as $o)
  80. {
  81. $result[] = $o->to_array($fields);
  82. }
  83. return $result;
  84. }
  85. /**
  86. * Convert a single field from the entire $object->all array result set into an a single array
  87. * with the objects' id field as key
  88. *
  89. * @param DataMapper $object The DataMapper Object to convert
  90. * @param string $field to include
  91. * @return array An array of associative arrays.
  92. */
  93. function all_to_single_array($object, $field = '')
  94. {
  95. // loop through each object in the $all array, convert them to
  96. // an array, and add them to a new array.
  97. $result = array();
  98. if ( ! empty($field) )
  99. {
  100. foreach($object as $o)
  101. {
  102. isset($o->{$field}) and $result[$o->id] = $o->{$field};
  103. }
  104. }
  105. return $result;
  106. }
  107. /**
  108. * Convert an associative array back into a DataMapper model.
  109. *
  110. * If $fields is provided, missing fields are assumed to be empty checkboxes.
  111. *
  112. * @param DataMapper $object The DataMapper Object to save to.
  113. * @param array $data A an associative array of fields to convert.
  114. * @param array $fields Array of 'safe' fields. If empty, only includes the database columns.
  115. * @param bool $save If TRUE, then attempt to save the object automatically.
  116. * @return array|bool A list of newly related objects, or the result of the save if $save is TRUE
  117. */
  118. function from_array($object, $data, $fields = '', $save = FALSE)
  119. {
  120. // clear the current object
  121. //$object->clear();
  122. // keep track of newly related objects
  123. $new_related_objects = array();
  124. // Assume all database columns.
  125. // In this case, simply store $fields that are in the $data array.
  126. if(empty($fields))
  127. {
  128. $fields = $object->fields;
  129. foreach($data as $k => $v) {
  130. if(in_array($k, $fields))
  131. {
  132. $object->{$k} = $v;
  133. }
  134. }
  135. }
  136. else
  137. {
  138. // If $fields is provided, assume all $fields should exist.
  139. foreach($fields as $f)
  140. {
  141. if(array_key_exists($f, $object->has_one))
  142. {
  143. // Store $has_one relationships
  144. $c = get_class($object->{$f});
  145. $rel = new $c();
  146. $id = isset($data[$f]) ? $data[$f] : 0;
  147. $rel->get_by_id($id);
  148. if($rel->exists())
  149. {
  150. // The new relationship exists, save it.
  151. $new_related_objects[$f] = $rel;
  152. }
  153. else
  154. {
  155. // The new relationship does not exist, delete the old one.
  156. $object->delete($object->{$f}->get());
  157. }
  158. }
  159. else if(array_key_exists($f, $object->has_many))
  160. {
  161. // Store $has_many relationships
  162. $c = get_class($object->{$f});
  163. $rels = new $c();
  164. $ids = isset($data[$f]) ? $data[$f] : FALSE;
  165. if(empty($ids))
  166. {
  167. // if no IDs were provided, delete all old relationships.
  168. $object->delete($object->{$f}->select('id')->get()->all);
  169. }
  170. else
  171. {
  172. // Otherwise, get the new ones...
  173. $rels->where_in('id', $ids)->select('id')->get();
  174. // Store them...
  175. $new_related_objects[$f] = $rels->all;
  176. // And delete any old ones that do not exist.
  177. $old_rels = $object->{$f}->where_not_in('id', $ids)->select('id')->get();
  178. $object->delete($old_rels->all);
  179. }
  180. }
  181. else
  182. {
  183. // Otherwise, if the $data was set, store it...
  184. if(isset($data[$f]))
  185. {
  186. $v = $data[$f];
  187. }
  188. else
  189. {
  190. // Or assume it was an unchecked checkbox, and clear it.
  191. $v = FALSE;
  192. }
  193. $object->{$f} = $v;
  194. }
  195. }
  196. }
  197. if($save)
  198. {
  199. // Auto save
  200. return $object->save($new_related_objects);
  201. }
  202. else
  203. {
  204. // return new objects
  205. return $new_related_objects;
  206. }
  207. }
  208. /**
  209. * Convert an associative multi_level array back into a DataMapper model.
  210. *
  211. * If $fields is provided, missing fields are assumed to be empty checkboxes.
  212. *
  213. * @param DataMapper $object The DataMapper Object to save to.
  214. * @param array $data A an associative array of fields to convert.
  215. * @param array $fields Array of 'safe' fields. If empty, only includes the database columns.
  216. * @return array|bool A list of newly related objects, or the result of the save if $save is TRUE
  217. */
  218. function all_from_array($object, $data, $fields = '')
  219. {
  220. //echo 'heelo';
  221. //echo 'All_from_array<hr><pre>'.print_r($data, true).'</pre><hr>';
  222. // clear the current object
  223. $object->clear();
  224. // get the objects class name, we need it to construct copies
  225. $class = get_class($object);
  226. $first = true;
  227. // loop over the data array
  228. foreach($data as $row)
  229. {
  230. // create an object for this row
  231. if ($first)
  232. {
  233. $object->from_array($row, $fields);
  234. }
  235. else
  236. {
  237. $new = new $class;
  238. $new->from_array($row, $fields);
  239. }
  240. // and store it in the object
  241. if ($object->all_array_uses_ids && isset($row['id']))
  242. {
  243. if ($first)
  244. {
  245. $object->all[$row['id']] =& $object;
  246. }
  247. else
  248. {
  249. $object->all[$row['id']] = $new;
  250. }
  251. }
  252. else
  253. {
  254. if ($first)
  255. {
  256. $object->all[] =& $object;
  257. }
  258. else
  259. {
  260. $object->all[] = $new;
  261. }
  262. }
  263. $first = FALSE;
  264. }
  265. return $object;
  266. }
  267. }
  268. /* End of file array.php */
  269. /* Location: ./application/datamapper/array.php */