PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/bespoke/application/datamapper/array.php

https://github.com/narendranag/Snippets
PHP | 185 lines | 109 code | 12 blank | 64 comment | 12 complexity | 44a47349cc0c064d8766c258183fa73f 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. * @category DataMapper Extensions
  9. * @author Phil DeJarnett
  10. * @link http://www.overzealous.com/dmz/
  11. * @version 1.0
  12. */
  13. // --------------------------------------------------------------------------
  14. /**
  15. * DMZ_Array Class
  16. */
  17. class DMZ_Array {
  18. /**
  19. * Convert a DataMapper model into an associative array.
  20. *
  21. * @return An associative array.
  22. * @param object $object The DataMapper Object to convert
  23. * @param array $fields[optional] Array of fields to include. If empty, includes all database columns.
  24. */
  25. function to_array($object, $fields = '')
  26. {
  27. // assume all database columns if $fields is not provided.
  28. if(empty($fields))
  29. {
  30. $fields = $object->fields;
  31. }
  32. $result = array();
  33. foreach($fields as $f)
  34. {
  35. // handle related fields
  36. if(array_key_exists($f, $object->has_one) || array_key_exists($f, $object->has_many))
  37. {
  38. // each related item is stored as an array of ids
  39. // Note: this method will NOT get() the related object.
  40. $rels = array();
  41. foreach($object->{$f}->all as $item)
  42. {
  43. $rels[] = $item->id;
  44. }
  45. $result[$f] = $rels;
  46. }
  47. else
  48. {
  49. // just the field.
  50. $result[$f] = $object->{$f};
  51. }
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Convert the entire $object->all array result set into an array of associative arrays.
  57. *
  58. * @return An array of associative arrays.
  59. * @param object $object The DataMapper Object to convert
  60. * @param array $fields[optional] Array of fields to include. If empty, includes all database columns.
  61. */
  62. function all_to_array($object, $fields = '')
  63. {
  64. // loop through each object in the $all array, convert them to
  65. // an array, and add them to a new array.
  66. $result = array();
  67. foreach($object->all as $o)
  68. {
  69. $result[] = $o->to_array($fields);
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Convert an associative array back into a DataMapper model.
  75. *
  76. * If $fields is provided, missing fields are assumed to be empty checkboxes.
  77. *
  78. * @return A list of newly related objects, or the result of the save if $save is TRUE
  79. * @param object $object The DataMapper Object to save to.
  80. * @param string $data A an associative array of fields to convert.
  81. * @param array $fields[optional] Array of 'safe' fields. If empty, only includes the database columns.
  82. */
  83. function from_array($object, $data, $fields = '', $save = FALSE)
  84. {
  85. // keep track of newly related objects
  86. $new_related_objects = array();
  87. // Assume all database columns.
  88. // In this case, simply store $fields that are in the $data array.
  89. if(empty($fields))
  90. {
  91. $fields = $object->fields;
  92. foreach($data as $k => $v) {
  93. if(in_array($k, $fields))
  94. {
  95. $object->{$k} = $v;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. // If $fields is provided, assume all $fields should exist.
  102. foreach($fields as $f)
  103. {
  104. if(array_key_exists($f, $object->has_one))
  105. {
  106. // Store $has_one relationships
  107. $c = get_class($object->{$f});
  108. $rel = new $c();
  109. $id = isset($data[$f]) ? $data[$f] : 0;
  110. $rel->get_by_id($id);
  111. if($rel->exists())
  112. {
  113. // The new relationship exists, save it.
  114. $new_related_objects[$f] = $rel;
  115. }
  116. else
  117. {
  118. // The new relationship does not exist, delete the old one.
  119. $object->delete($object->{$f}->get());
  120. }
  121. }
  122. else if(array_key_exists($f, $object->has_many))
  123. {
  124. // Store $has_many relationships
  125. $c = get_class($object->{$f});
  126. $rels = new $c();
  127. $ids = isset($data[$f]) ? $data[$f] : FALSE;
  128. if(empty($ids))
  129. {
  130. // if no IDs were provided, delete all old relationships.
  131. $object->delete($object->{$f}->select('id')->get()->all);
  132. }
  133. else
  134. {
  135. // Otherwise, get the new ones...
  136. $rels->where_in('id', $ids)->select('id')->get();
  137. // Store them...
  138. $new_related_objects[$f] = $rels->all;
  139. // And delete any old ones that do not exist.
  140. $old_rels = $object->{$f}->where_not_in('id', $ids)->select('id')->get();
  141. $object->delete($old_rels->all);
  142. }
  143. }
  144. else
  145. {
  146. // Otherwise, if the $data was set, store it...
  147. if(isset($data[$f]))
  148. {
  149. $v = $data[$f];
  150. }
  151. else
  152. {
  153. // Or assume it was an unchecked checkbox, and clear it.
  154. $v = FALSE;
  155. }
  156. $object->{$f} = $v;
  157. }
  158. }
  159. }
  160. if($save)
  161. {
  162. // Auto save
  163. return $object->save($new_related_objects);
  164. }
  165. else
  166. {
  167. // return new objects
  168. return $new_related_objects;
  169. }
  170. }
  171. }
  172. /* End of file array.php */
  173. /* Location: ./application/datamapper/array.php */