PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/application/datamapper/array.php

https://bitbucket.org/masnug/tanur2
PHP | 198 lines | 113 code | 12 blank | 73 comment | 12 complexity | f2ebc95dad0cfcd6e0f81f5e16455afe MD5 | raw file
  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 an associative array back into a DataMapper model.
  87. *
  88. * If $fields is provided, missing fields are assumed to be empty checkboxes.
  89. *
  90. * @param DataMapper $object The DataMapper Object to save to.
  91. * @param array $data A an associative array of fields to convert.
  92. * @param array $fields Array of 'safe' fields. If empty, only includes the database columns.
  93. * @param bool $save If TRUE, then attempt to save the object automatically.
  94. * @return array|bool A list of newly related objects, or the result of the save if $save is TRUE
  95. */
  96. function from_array($object, $data, $fields = '', $save = FALSE)
  97. {
  98. // keep track of newly related objects
  99. $new_related_objects = array();
  100. // Assume all database columns.
  101. // In this case, simply store $fields that are in the $data array.
  102. if(empty($fields))
  103. {
  104. $fields = $object->fields;
  105. foreach($data as $k => $v) {
  106. if(in_array($k, $fields))
  107. {
  108. $object->{$k} = $v;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. // If $fields is provided, assume all $fields should exist.
  115. foreach($fields as $f)
  116. {
  117. if(array_key_exists($f, $object->has_one))
  118. {
  119. // Store $has_one relationships
  120. $c = get_class($object->{$f});
  121. $rel = new $c();
  122. $id = isset($data[$f]) ? $data[$f] : 0;
  123. $rel->get_by_id($id);
  124. if($rel->exists())
  125. {
  126. // The new relationship exists, save it.
  127. $new_related_objects[$f] = $rel;
  128. }
  129. else
  130. {
  131. // The new relationship does not exist, delete the old one.
  132. $object->delete($object->{$f}->get());
  133. }
  134. }
  135. else if(array_key_exists($f, $object->has_many))
  136. {
  137. // Store $has_many relationships
  138. $c = get_class($object->{$f});
  139. $rels = new $c();
  140. $ids = isset($data[$f]) ? $data[$f] : FALSE;
  141. if(empty($ids))
  142. {
  143. // if no IDs were provided, delete all old relationships.
  144. $object->delete($object->{$f}->select('id')->get()->all);
  145. }
  146. else
  147. {
  148. // Otherwise, get the new ones...
  149. $rels->where_in('id', $ids)->select('id')->get();
  150. // Store them...
  151. $new_related_objects[$f] = $rels->all;
  152. // And delete any old ones that do not exist.
  153. $old_rels = $object->{$f}->where_not_in('id', $ids)->select('id')->get();
  154. $object->delete($old_rels->all);
  155. }
  156. }
  157. else
  158. {
  159. // Otherwise, if the $data was set, store it...
  160. if(isset($data[$f]))
  161. {
  162. $v = $data[$f];
  163. }
  164. else
  165. {
  166. // Or assume it was an unchecked checkbox, and clear it.
  167. $v = FALSE;
  168. }
  169. $object->{$f} = $v;
  170. }
  171. }
  172. }
  173. if($save)
  174. {
  175. // Auto save
  176. return $object->save($new_related_objects);
  177. }
  178. else
  179. {
  180. // return new objects
  181. return $new_related_objects;
  182. }
  183. }
  184. }
  185. /* End of file array.php */
  186. /* Location: ./application/datamapper/array.php */