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

/concrete/libraries/3rdparty/Zend/Db/Table/Rowset/Abstract.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 435 lines | 171 code | 41 blank | 223 comment | 19 complexity | dc1bd46c9ecff20b10897b4b8c393c78 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Table
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Db
  25. * @subpackage Table
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. abstract class Zend_Db_Table_Rowset_Abstract implements SeekableIterator, Countable, ArrayAccess
  30. {
  31. /**
  32. * The original data for each row.
  33. *
  34. * @var array
  35. */
  36. protected $_data = array();
  37. /**
  38. * Zend_Db_Table_Abstract object.
  39. *
  40. * @var Zend_Db_Table_Abstract
  41. */
  42. protected $_table;
  43. /**
  44. * Connected is true if we have a reference to a live
  45. * Zend_Db_Table_Abstract object.
  46. * This is false after the Rowset has been deserialized.
  47. *
  48. * @var boolean
  49. */
  50. protected $_connected = true;
  51. /**
  52. * Zend_Db_Table_Abstract class name.
  53. *
  54. * @var string
  55. */
  56. protected $_tableClass;
  57. /**
  58. * Zend_Db_Table_Row_Abstract class name.
  59. *
  60. * @var string
  61. */
  62. protected $_rowClass = 'Zend_Db_Table_Row';
  63. /**
  64. * Iterator pointer.
  65. *
  66. * @var integer
  67. */
  68. protected $_pointer = 0;
  69. /**
  70. * How many data rows there are.
  71. *
  72. * @var integer
  73. */
  74. protected $_count;
  75. /**
  76. * Collection of instantiated Zend_Db_Table_Row objects.
  77. *
  78. * @var array
  79. */
  80. protected $_rows = array();
  81. /**
  82. * @var boolean
  83. */
  84. protected $_stored = false;
  85. /**
  86. * @var boolean
  87. */
  88. protected $_readOnly = false;
  89. /**
  90. * Constructor.
  91. *
  92. * @param array $config
  93. */
  94. public function __construct(array $config)
  95. {
  96. if (isset($config['table'])) {
  97. $this->_table = $config['table'];
  98. $this->_tableClass = get_class($this->_table);
  99. }
  100. if (isset($config['rowClass'])) {
  101. $this->_rowClass = $config['rowClass'];
  102. }
  103. if (!class_exists($this->_rowClass)) {
  104. require_once 'Zend/Loader.php';
  105. Zend_Loader::loadClass($this->_rowClass);
  106. }
  107. if (isset($config['data'])) {
  108. $this->_data = $config['data'];
  109. }
  110. if (isset($config['readOnly'])) {
  111. $this->_readOnly = $config['readOnly'];
  112. }
  113. if (isset($config['stored'])) {
  114. $this->_stored = $config['stored'];
  115. }
  116. // set the count of rows
  117. $this->_count = count($this->_data);
  118. $this->init();
  119. }
  120. /**
  121. * Store data, class names, and state in serialized object
  122. *
  123. * @return array
  124. */
  125. public function __sleep()
  126. {
  127. return array('_data', '_tableClass', '_rowClass', '_pointer', '_count', '_rows', '_stored',
  128. '_readOnly');
  129. }
  130. /**
  131. * Setup to do on wakeup.
  132. * A de-serialized Rowset should not be assumed to have access to a live
  133. * database connection, so set _connected = false.
  134. *
  135. * @return void
  136. */
  137. public function __wakeup()
  138. {
  139. $this->_connected = false;
  140. }
  141. /**
  142. * Initialize object
  143. *
  144. * Called from {@link __construct()} as final step of object instantiation.
  145. *
  146. * @return void
  147. */
  148. public function init()
  149. {
  150. }
  151. /**
  152. * Return the connected state of the rowset.
  153. *
  154. * @return boolean
  155. */
  156. public function isConnected()
  157. {
  158. return $this->_connected;
  159. }
  160. /**
  161. * Returns the table object, or null if this is disconnected rowset
  162. *
  163. * @return Zend_Db_Table_Abstract
  164. */
  165. public function getTable()
  166. {
  167. return $this->_table;
  168. }
  169. /**
  170. * Set the table object, to re-establish a live connection
  171. * to the database for a Rowset that has been de-serialized.
  172. *
  173. * @param Zend_Db_Table_Abstract $table
  174. * @return boolean
  175. * @throws Zend_Db_Table_Row_Exception
  176. */
  177. public function setTable(Zend_Db_Table_Abstract $table)
  178. {
  179. $this->_table = $table;
  180. $this->_connected = false;
  181. // @todo This works only if we have iterated through
  182. // the result set once to instantiate the rows.
  183. foreach ($this as $row) {
  184. $connected = $row->setTable($table);
  185. if ($connected == true) {
  186. $this->_connected = true;
  187. }
  188. }
  189. return $this->_connected;
  190. }
  191. /**
  192. * Query the class name of the Table object for which this
  193. * Rowset was created.
  194. *
  195. * @return string
  196. */
  197. public function getTableClass()
  198. {
  199. return $this->_tableClass;
  200. }
  201. /**
  202. * Rewind the Iterator to the first element.
  203. * Similar to the reset() function for arrays in PHP.
  204. * Required by interface Iterator.
  205. *
  206. * @return Zend_Db_Table_Rowset_Abstract Fluent interface.
  207. */
  208. public function rewind()
  209. {
  210. $this->_pointer = 0;
  211. return $this;
  212. }
  213. /**
  214. * Return the current element.
  215. * Similar to the current() function for arrays in PHP
  216. * Required by interface Iterator.
  217. *
  218. * @return Zend_Db_Table_Row_Abstract current element from the collection
  219. */
  220. public function current()
  221. {
  222. if ($this->valid() === false) {
  223. return null;
  224. }
  225. // return the row object
  226. return $this->_loadAndReturnRow($this->_pointer);
  227. }
  228. /**
  229. * Return the identifying key of the current element.
  230. * Similar to the key() function for arrays in PHP.
  231. * Required by interface Iterator.
  232. *
  233. * @return int
  234. */
  235. public function key()
  236. {
  237. return $this->_pointer;
  238. }
  239. /**
  240. * Move forward to next element.
  241. * Similar to the next() function for arrays in PHP.
  242. * Required by interface Iterator.
  243. *
  244. * @return void
  245. */
  246. public function next()
  247. {
  248. ++$this->_pointer;
  249. }
  250. /**
  251. * Check if there is a current element after calls to rewind() or next().
  252. * Used to check if we've iterated to the end of the collection.
  253. * Required by interface Iterator.
  254. *
  255. * @return bool False if there's nothing more to iterate over
  256. */
  257. public function valid()
  258. {
  259. return $this->_pointer >= 0 && $this->_pointer < $this->_count;
  260. }
  261. /**
  262. * Returns the number of elements in the collection.
  263. *
  264. * Implements Countable::count()
  265. *
  266. * @return int
  267. */
  268. public function count()
  269. {
  270. return $this->_count;
  271. }
  272. /**
  273. * Take the Iterator to position $position
  274. * Required by interface SeekableIterator.
  275. *
  276. * @param int $position the position to seek to
  277. * @return Zend_Db_Table_Rowset_Abstract
  278. * @throws Zend_Db_Table_Rowset_Exception
  279. */
  280. public function seek($position)
  281. {
  282. $position = (int) $position;
  283. if ($position < 0 || $position >= $this->_count) {
  284. require_once 'Zend/Db/Table/Rowset/Exception.php';
  285. throw new Zend_Db_Table_Rowset_Exception("Illegal index $position");
  286. }
  287. $this->_pointer = $position;
  288. return $this;
  289. }
  290. /**
  291. * Check if an offset exists
  292. * Required by the ArrayAccess implementation
  293. *
  294. * @param string $offset
  295. * @return boolean
  296. */
  297. public function offsetExists($offset)
  298. {
  299. return isset($this->_data[(int) $offset]);
  300. }
  301. /**
  302. * Get the row for the given offset
  303. * Required by the ArrayAccess implementation
  304. *
  305. * @param string $offset
  306. * @return Zend_Db_Table_Row_Abstract
  307. */
  308. public function offsetGet($offset)
  309. {
  310. $offset = (int) $offset;
  311. if ($offset < 0 || $offset >= $this->_count) {
  312. require_once 'Zend/Db/Table/Rowset/Exception.php';
  313. throw new Zend_Db_Table_Rowset_Exception("Illegal index $offset");
  314. }
  315. $this->_pointer = $offset;
  316. return $this->current();
  317. }
  318. /**
  319. * Does nothing
  320. * Required by the ArrayAccess implementation
  321. *
  322. * @param string $offset
  323. * @param mixed $value
  324. */
  325. public function offsetSet($offset, $value)
  326. {
  327. }
  328. /**
  329. * Does nothing
  330. * Required by the ArrayAccess implementation
  331. *
  332. * @param string $offset
  333. */
  334. public function offsetUnset($offset)
  335. {
  336. }
  337. /**
  338. * Returns a Zend_Db_Table_Row from a known position into the Iterator
  339. *
  340. * @param int $position the position of the row expected
  341. * @param bool $seek wether or not seek the iterator to that position after
  342. * @return Zend_Db_Table_Row
  343. * @throws Zend_Db_Table_Rowset_Exception
  344. */
  345. public function getRow($position, $seek = false)
  346. {
  347. try {
  348. $row = $this->_loadAndReturnRow($position);
  349. } catch (Zend_Db_Table_Rowset_Exception $e) {
  350. require_once 'Zend/Db/Table/Rowset/Exception.php';
  351. throw new Zend_Db_Table_Rowset_Exception('No row could be found at position ' . (int) $position, 0, $e);
  352. }
  353. if ($seek == true) {
  354. $this->seek($position);
  355. }
  356. return $row;
  357. }
  358. /**
  359. * Returns all data as an array.
  360. *
  361. * Updates the $_data property with current row object values.
  362. *
  363. * @return array
  364. */
  365. public function toArray()
  366. {
  367. // @todo This works only if we have iterated through
  368. // the result set once to instantiate the rows.
  369. foreach ($this->_rows as $i => $row) {
  370. $this->_data[$i] = $row->toArray();
  371. }
  372. return $this->_data;
  373. }
  374. protected function _loadAndReturnRow($position)
  375. {
  376. if (!isset($this->_data[$position])) {
  377. require_once 'Zend/Db/Table/Rowset/Exception.php';
  378. throw new Zend_Db_Table_Rowset_Exception("Data for provided position does not exist");
  379. }
  380. // do we already have a row object for this position?
  381. if (empty($this->_rows[$position])) {
  382. $this->_rows[$position] = new $this->_rowClass(
  383. array(
  384. 'table' => $this->_table,
  385. 'data' => $this->_data[$position],
  386. 'stored' => $this->_stored,
  387. 'readOnly' => $this->_readOnly
  388. )
  389. );
  390. }
  391. // return the row object
  392. return $this->_rows[$position];
  393. }
  394. }