PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/Zend/Db/Table/Rowset/Abstract.php

https://gitlab.com/Ltaimao/wecenter
PHP | 448 lines | 177 code | 43 blank | 228 comment | 24 complexity | 78ebf83ab0e9bbb27ccd5b96fa3827d6 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Db
  25. * @subpackage Table
  26. * @copyright Copyright (c) 2005-2015 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. $this->rewind();
  190. return $this->_connected;
  191. }
  192. /**
  193. * Query the class name of the Table object for which this
  194. * Rowset was created.
  195. *
  196. * @return string
  197. */
  198. public function getTableClass()
  199. {
  200. return $this->_tableClass;
  201. }
  202. /**
  203. * Rewind the Iterator to the first element.
  204. * Similar to the reset() function for arrays in PHP.
  205. * Required by interface Iterator.
  206. *
  207. * @return Zend_Db_Table_Rowset_Abstract Fluent interface.
  208. */
  209. public function rewind()
  210. {
  211. $this->_pointer = 0;
  212. return $this;
  213. }
  214. /**
  215. * Return the current element.
  216. * Similar to the current() function for arrays in PHP
  217. * Required by interface Iterator.
  218. *
  219. * @return Zend_Db_Table_Row_Abstract current element from the collection
  220. */
  221. public function current()
  222. {
  223. if ($this->valid() === false) {
  224. return null;
  225. }
  226. // return the row object
  227. return $this->_loadAndReturnRow($this->_pointer);
  228. }
  229. /**
  230. * Return the identifying key of the current element.
  231. * Similar to the key() function for arrays in PHP.
  232. * Required by interface Iterator.
  233. *
  234. * @return int
  235. */
  236. public function key()
  237. {
  238. return $this->_pointer;
  239. }
  240. /**
  241. * Move forward to next element.
  242. * Similar to the next() function for arrays in PHP.
  243. * Required by interface Iterator.
  244. *
  245. * @return void
  246. */
  247. public function next()
  248. {
  249. ++$this->_pointer;
  250. }
  251. /**
  252. * Check if there is a current element after calls to rewind() or next().
  253. * Used to check if we've iterated to the end of the collection.
  254. * Required by interface Iterator.
  255. *
  256. * @return bool False if there's nothing more to iterate over
  257. */
  258. public function valid()
  259. {
  260. return $this->_pointer >= 0 && $this->_pointer < $this->_count;
  261. }
  262. /**
  263. * Returns the number of elements in the collection.
  264. *
  265. * Implements Countable::count()
  266. *
  267. * @return int
  268. */
  269. public function count()
  270. {
  271. return $this->_count;
  272. }
  273. /**
  274. * Take the Iterator to position $position
  275. * Required by interface SeekableIterator.
  276. *
  277. * @param int $position the position to seek to
  278. * @return Zend_Db_Table_Rowset_Abstract
  279. * @throws Zend_Db_Table_Rowset_Exception
  280. */
  281. public function seek($position)
  282. {
  283. $position = (int) $position;
  284. if ($position < 0 || $position >= $this->_count) {
  285. //require_once 'Zend/Db/Table/Rowset/Exception.php';
  286. throw new Zend_Db_Table_Rowset_Exception("Illegal index $position");
  287. }
  288. $this->_pointer = $position;
  289. return $this;
  290. }
  291. /**
  292. * Check if an offset exists
  293. * Required by the ArrayAccess implementation
  294. *
  295. * @param string $offset
  296. * @return boolean
  297. */
  298. public function offsetExists($offset)
  299. {
  300. return isset($this->_data[(int) $offset]);
  301. }
  302. /**
  303. * Get the row for the given offset
  304. * Required by the ArrayAccess implementation
  305. *
  306. * @param string $offset
  307. * @return Zend_Db_Table_Row_Abstract
  308. */
  309. public function offsetGet($offset)
  310. {
  311. $offset = (int) $offset;
  312. if ($offset < 0 || $offset >= $this->_count) {
  313. //require_once 'Zend/Db/Table/Rowset/Exception.php';
  314. throw new Zend_Db_Table_Rowset_Exception("Illegal index $offset");
  315. }
  316. $this->_pointer = $offset;
  317. return $this->current();
  318. }
  319. /**
  320. * Does nothing
  321. * Required by the ArrayAccess implementation
  322. *
  323. * @param string $offset
  324. * @param mixed $value
  325. */
  326. public function offsetSet($offset, $value)
  327. {
  328. }
  329. /**
  330. * Does nothing
  331. * Required by the ArrayAccess implementation
  332. *
  333. * @param string $offset
  334. */
  335. public function offsetUnset($offset)
  336. {
  337. }
  338. /**
  339. * Returns a Zend_Db_Table_Row from a known position into the Iterator
  340. *
  341. * @param int $position the position of the row expected
  342. * @param bool $seek wether or not seek the iterator to that position after
  343. * @return Zend_Db_Table_Row
  344. * @throws Zend_Db_Table_Rowset_Exception
  345. */
  346. public function getRow($position, $seek = false)
  347. {
  348. try {
  349. $row = $this->_loadAndReturnRow($position);
  350. } catch (Zend_Db_Table_Rowset_Exception $e) {
  351. //require_once 'Zend/Db/Table/Rowset/Exception.php';
  352. throw new Zend_Db_Table_Rowset_Exception('No row could be found at position ' . (int) $position, 0, $e);
  353. }
  354. if ($seek == true) {
  355. $this->seek($position);
  356. }
  357. return $row;
  358. }
  359. /**
  360. * Returns all data as an array.
  361. *
  362. * Updates the $_data property with current row object values.
  363. *
  364. * @return array
  365. */
  366. public function toArray()
  367. {
  368. // @todo This works only if we have iterated through
  369. // the result set once to instantiate the rows.
  370. foreach ($this->_rows as $i => $row) {
  371. $this->_data[$i] = $row->toArray();
  372. }
  373. return $this->_data;
  374. }
  375. protected function _loadAndReturnRow($position)
  376. {
  377. if (!isset($this->_data[$position])) {
  378. //require_once 'Zend/Db/Table/Rowset/Exception.php';
  379. throw new Zend_Db_Table_Rowset_Exception("Data for provided position does not exist");
  380. }
  381. // do we already have a row object for this position?
  382. if (empty($this->_rows[$position])) {
  383. $this->_rows[$position] = new $this->_rowClass(
  384. array(
  385. 'table' => $this->_table,
  386. 'data' => $this->_data[$position],
  387. 'stored' => $this->_stored,
  388. 'readOnly' => $this->_readOnly
  389. )
  390. );
  391. if ( $this->_table instanceof Zend_Db_Table_Abstract ) {
  392. $info = $this->_table->info();
  393. if ( $this->_rows[$position] instanceof Zend_Db_Table_Row_Abstract ) {
  394. if ($info['cols'] == array_keys($this->_data[$position])) {
  395. $this->_rows[$position]->setTable($this->getTable());
  396. }
  397. }
  398. } else {
  399. $this->_rows[$position]->setTable(null);
  400. }
  401. }
  402. // return the row object
  403. return $this->_rows[$position];
  404. }
  405. }