/Solar/Sql/Model/Params.php

http://github.com/solarphp/core · PHP · 200 lines · 77 code · 12 blank · 111 comment · 7 complexity · 48a4d970894c6e1c7676b4d1b26c9e1f MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * A value-object to represent the various parameters for specifying a model
  5. * fetch() call.
  6. *
  7. * @category Solar
  8. *
  9. * @package Solar_Sql_Model
  10. *
  11. * @author Paul M. Jones <pmjones@solarphp.com>
  12. *
  13. * @license http://opensource.org/licenses/bsd-license.php BSD
  14. *
  15. * @version $Id$
  16. *
  17. */
  18. abstract class Solar_Sql_Model_Params extends Solar_Struct {
  19. /**
  20. *
  21. * Default data array.
  22. *
  23. * @var array
  24. *
  25. */
  26. protected $_data = array(
  27. 'cols' => array(),
  28. 'eager' => array(),
  29. 'alias' => null,
  30. );
  31. /**
  32. *
  33. * Performs a "deep" clone of objects in the data.
  34. *
  35. * @return void
  36. *
  37. */
  38. public function __clone()
  39. {
  40. // do a "deep" clone of the objects
  41. foreach ($this->_data['eager'] as $name => $eager) {
  42. $clone = clone($eager);
  43. $this->_data[$name] = $clone;
  44. }
  45. }
  46. /**
  47. *
  48. * Adds new columns to the existing list of columns.
  49. *
  50. * @param array $list The new columns to add to the existing ones.
  51. *
  52. * @return Solar_Sql_Model_Params
  53. *
  54. */
  55. public function cols($list)
  56. {
  57. $list = array_merge(
  58. (array) $this->_data['cols'],
  59. (array) $list
  60. );
  61. $this->_data['cols'] = array_unique($list);
  62. return $this;
  63. }
  64. /**
  65. *
  66. * Adds a new related eager-fetch (with options) to the params.
  67. *
  68. * @param string $name The name of the related to eager-fetch.
  69. *
  70. * @param array $opts Options for the eager-fetch; cf.
  71. * [[Solar_Sql_Model_Params_Eager]].
  72. *
  73. * @return Solar_Sql_Model_Params
  74. *
  75. */
  76. public function eager($name, $opts = null)
  77. {
  78. // BC-helping logic
  79. if (is_int($name) && is_string($opts)) {
  80. $name = $opts;
  81. $opts = null;
  82. }
  83. // now the real logic
  84. if (empty($this->_data['eager'][$name])) {
  85. $eager = Solar::factory('Solar_Sql_Model_Params_Eager');
  86. $this->_data['eager'][$name] = $eager;
  87. }
  88. $this->_data['eager'][$name]->load($opts);
  89. return $this;
  90. }
  91. /**
  92. *
  93. * Sets the alias to use for this eager or fetch.
  94. *
  95. * @param string $val The alias name.
  96. *
  97. * @return Solar_Sql_Model_Params
  98. *
  99. */
  100. public function alias($val)
  101. {
  102. $this->_data['alias'] = (string) $val;
  103. }
  104. /**
  105. *
  106. * Loads this params object with an array or struct.
  107. *
  108. * @param array|Solar_Struct $spec The data to load.
  109. *
  110. * @return Solar_Sql_Model_Params
  111. *
  112. * @see _load()
  113. *
  114. */
  115. public function load($spec)
  116. {
  117. parent::load($spec);
  118. return $this;
  119. }
  120. /**
  121. *
  122. * Loads this params object with an array of data using support methods.
  123. *
  124. * @param array $data The data to load.
  125. *
  126. * @return Solar_Sql_Model_Params
  127. *
  128. * @see _loadOne()
  129. *
  130. * @see _loadTwo()
  131. *
  132. */
  133. protected function _load($data)
  134. {
  135. $this->_loadOne($data, array('cols', 'alias'));
  136. $this->_loadTwo($data, array('eager'));
  137. }
  138. /**
  139. *
  140. * Calls one-argment methods to load $data elements.
  141. *
  142. * @param array $data The data to load.
  143. *
  144. * @param array $list Which data elements to load using one-argument
  145. * methods.
  146. *
  147. * @return void
  148. *
  149. */
  150. protected function _loadOne($data, $list)
  151. {
  152. foreach ($list as $prop => $func) {
  153. if (is_int($prop)) {
  154. $prop = $func;
  155. }
  156. if (array_key_exists($prop, $data)) {
  157. foreach ((array) $data[$prop] as $val) {
  158. $this->$func($val);
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. *
  165. * Calls two-argment methods to load $data elements.
  166. *
  167. * @param array $data The data to load.
  168. *
  169. * @param array $list Which data elements to load using two-argument
  170. * methods.
  171. *
  172. * @return void
  173. *
  174. */
  175. protected function _loadTwo($data, $list)
  176. {
  177. foreach ($list as $prop => $func) {
  178. if (is_int($prop)) {
  179. $prop = $func;
  180. }
  181. if (array_key_exists($prop, $data)) {
  182. foreach ((array) $data[$prop] as $key => $val) {
  183. $this->$func($key, $val);
  184. }
  185. }
  186. }
  187. }
  188. }