/core/lib/db/ORMWrapper.php

https://github.com/zephrax/cotyledon · PHP · 137 lines · 37 code · 11 blank · 89 comment · 2 complexity · d54a07dacc2122baa64894115bc199e4 MD5 · raw file

  1. <?php
  2. namespace DB;
  3. /**
  4. *
  5. * Paris
  6. *
  7. * http://github.com/j4mie/paris/
  8. *
  9. * A simple Active Record implementation built on top of Idiorm
  10. * ( http://github.com/j4mie/idiorm/ ).
  11. *
  12. * You should include Idiorm before you include this file:
  13. * require_once 'your/path/to/idiorm.php';
  14. *
  15. * BSD Licensed.
  16. *
  17. * Copyright (c) 2010, Jamie Matthews
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * * Redistributions of source code must retain the above copyright notice, this
  24. * list of conditions and the following disclaimer.
  25. *
  26. * * Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  34. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  38. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. /**
  43. * Subclass of Idiorm's ORM class that supports
  44. * returning instances of a specified class rather
  45. * than raw instances of the ORM class.
  46. *
  47. * You shouldn't need to interact with this class
  48. * directly. It is used internally by the Model base
  49. * class.
  50. */
  51. class ORMWrapper extends ORM {
  52. /**
  53. * The wrapped find_one and find_many classes will
  54. * return an instance or instances of this class.
  55. */
  56. protected $_class_name;
  57. /**
  58. * Set the name of the class which the wrapped
  59. * methods should return instances of.
  60. */
  61. public function set_class_name($class_name) {
  62. $this->_class_name = $class_name;
  63. }
  64. /**
  65. * Add a custom filter to the method chain specified on the
  66. * model class. This allows custom queries to be added
  67. * to models. The filter should take an instance of the
  68. * ORM wrapper as its first argument and return an instance
  69. * of the ORM wrapper. Any arguments passed to this method
  70. * after the name of the filter will be passed to the called
  71. * filter function as arguments after the ORM class.
  72. */
  73. public function filter() {
  74. $args = func_get_args();
  75. $filter_function = array_shift($args);
  76. array_unshift($args, $this);
  77. if (method_exists($this->_class_name, $filter_function)) {
  78. return call_user_func_array(array($this->_class_name, $filter_function), $args);
  79. }
  80. }
  81. /**
  82. * Factory method, return an instance of this
  83. * class bound to the supplied table name.
  84. */
  85. public static function for_table($table_name) {
  86. self::_setup_db();
  87. return new self($table_name);
  88. }
  89. /**
  90. * Method to create an instance of the model class
  91. * associated with this wrapper and populate
  92. * it with the supplied Idiorm instance.
  93. */
  94. protected function _create_model_instance($orm) {
  95. if ($orm === false) {
  96. return false;
  97. }
  98. $model = new $this->_class_name();
  99. $model->set_orm($orm);
  100. return $model;
  101. }
  102. /**
  103. * Wrap Idiorm's find_one method to return
  104. * an instance of the class associated with
  105. * this wrapper instead of the raw ORM class.
  106. */
  107. public function find_one($id=null) {
  108. return $this->_create_model_instance(parent::find_one($id));
  109. }
  110. /**
  111. * Wrap Idiorm's find_many method to return
  112. * an array of instances of the class associated
  113. * with this wrapper instead of the raw ORM class.
  114. */
  115. public function find_many() {
  116. return array_map(array($this, '_create_model_instance'), parent::find_many());
  117. }
  118. /**
  119. * Wrap Idiorm's create method to return an
  120. * empty instance of the class associated with
  121. * this wrapper instead of the raw ORM class.
  122. */
  123. public function create($data=null) {
  124. return $this->_create_model_instance(parent::create($data));
  125. }
  126. }