PropelCollection.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Class for iterating over a list of Propel elements
  11. * The collection keys must be integers - no associative array accepted
  12. *
  13. * @author Francois Zaninotto
  14. * @package propel.runtime.collection
  15. */
  16. class PropelCollection extends ArrayObject implements Serializable
  17. {
  18. protected $model = '';
  19. protected $iterator;
  20. protected $formatter;
  21. // Generic Collection methods
  22. /**
  23. * Get the data in the collection
  24. *
  25. * @return array
  26. */
  27. public function getData()
  28. {
  29. return $this->getArrayCopy();
  30. }
  31. /**
  32. * Set the data in the collection
  33. *
  34. * @param array $data
  35. */
  36. public function setData($data)
  37. {
  38. $this->exchangeArray($data);
  39. }
  40. /**
  41. * Gets the position of the internal pointer
  42. * This position can be later used in seek()
  43. *
  44. * @return int
  45. */
  46. public function getPosition()
  47. {
  48. return (int) $this->getInternalIterator()->key();
  49. }
  50. /**
  51. * Move the internal pointer to the beginning of the list
  52. * And get the first element in the collection
  53. *
  54. * @return mixed
  55. */
  56. public function getFirst()
  57. {
  58. $this->getInternalIterator()->rewind();
  59. return $this->getCurrent();
  60. }
  61. /**
  62. * Check whether the internal pointer is at the beginning of the list
  63. *
  64. * @return boolean
  65. */
  66. public function isFirst()
  67. {
  68. return $this->getPosition() == 0;
  69. }
  70. /**
  71. * Move the internal pointer backward
  72. * And get the previous element in the collection
  73. *
  74. * @return mixed
  75. */
  76. public function getPrevious()
  77. {
  78. $pos = $this->getPosition();
  79. if ($pos == 0) {
  80. return null;
  81. } else {
  82. $this->getInternalIterator()->seek($pos - 1);
  83. return $this->getCurrent();
  84. }
  85. }
  86. /**
  87. * Get the current element in the collection
  88. *
  89. * @return mixed
  90. */
  91. public function getCurrent()
  92. {
  93. return $this->getInternalIterator()->current();
  94. }
  95. /**
  96. * Move the internal pointer forward
  97. * And get the next element in the collection
  98. *
  99. * @return mixed
  100. */
  101. public function getNext()
  102. {
  103. $this->getInternalIterator()->next();
  104. return $this->getCurrent();
  105. }
  106. /**
  107. * Move the internal pointer to the end of the list
  108. * And get the last element in the collection
  109. *
  110. * @return mixed
  111. */
  112. public function getLast()
  113. {
  114. $count = $this->count();
  115. if ($count == 0) {
  116. return null;
  117. } else {
  118. $this->getInternalIterator()->seek($count - 1);
  119. return $this->getCurrent();
  120. }
  121. }
  122. /**
  123. * Check whether the internal pointer is at the end of the list
  124. *
  125. * @return boolean
  126. */
  127. public function isLast()
  128. {
  129. $count = $this->count();
  130. if ($count == 0) {
  131. // empty list... so yes, this is the last
  132. return true;
  133. } else {
  134. return $this->getPosition() == $count - 1;
  135. }
  136. }
  137. /**
  138. * Check if the collection is empty
  139. *
  140. * @return boolean
  141. */
  142. public function isEmpty()
  143. {
  144. return $this->count() == 0;
  145. }
  146. /**
  147. * Check if the current index is an odd integer
  148. *
  149. * @return boolean
  150. */
  151. public function isOdd()
  152. {
  153. return (boolean) ($this->getInternalIterator()->key() % 2);
  154. }
  155. /**
  156. * Check if the current index is an even integer
  157. *
  158. * @return boolean
  159. */
  160. public function isEven()
  161. {
  162. return !$this->isOdd();
  163. }
  164. /**
  165. * Get an element from its key
  166. * Alias for ArrayObject::offsetGet()
  167. *
  168. * @param mixed $key
  169. *
  170. * @return mixed The element
  171. */
  172. public function get($key)
  173. {
  174. if (!$this->offsetExists($key)) {
  175. throw new PropelException('Unknown key ' . $key);
  176. }
  177. return $this->offsetGet($key);
  178. }
  179. /**
  180. * Pops an element off the end of the collection
  181. *
  182. * @return mixed The popped element
  183. */
  184. public function pop()
  185. {
  186. if ($this->count() == 0) {
  187. return null;
  188. }
  189. $ret = $this->getLast();
  190. $lastKey = $this->getInternalIterator()->key();
  191. $this->offsetUnset((string) $lastKey);
  192. return $ret;
  193. }
  194. /**
  195. * Pops an element off the beginning of the collection
  196. *
  197. * @return mixed The popped element
  198. */
  199. public function shift()
  200. {
  201. // the reindexing is complicated to deal with through the iterator
  202. // so let's use the simple solution
  203. $arr = $this->getArrayCopy();
  204. $ret = array_shift($arr);
  205. $this->exchangeArray($arr);
  206. return $ret;
  207. }
  208. /**
  209. * Prepend one or more elements to the beginning of the collection
  210. *
  211. * @param mixed $value the element to prepend
  212. *
  213. * @return int The number of new elements in the array
  214. */
  215. public function prepend($value)
  216. {
  217. // the reindexing is complicated to deal with through the iterator
  218. // so let's use the simple solution
  219. $arr = $this->getArrayCopy();
  220. $ret = array_unshift($arr, $value);
  221. $this->exchangeArray($arr);
  222. return $ret;
  223. }
  224. /**
  225. * Add an element to the collection with the given key
  226. * Alias for ArrayObject::offsetSet()
  227. *
  228. * @param mixed $key
  229. * @param mixed $value
  230. */
  231. public function set($key, $value)
  232. {
  233. return $this->offsetSet($key, $value);
  234. }
  235. /**
  236. * Removes a specified collection element
  237. * Alias for ArrayObject::offsetUnset()
  238. *
  239. * @param mixed $key
  240. *
  241. * @return mixed The removed element
  242. */
  243. public function remove($key)
  244. {
  245. if (!$this->offsetExists($key)) {
  246. throw new PropelException('Unknown key ' . $key);
  247. }
  248. return $this->offsetUnset($key);
  249. }
  250. /**
  251. * Clears the collection
  252. *
  253. * @return array The previous collection
  254. */
  255. public function clear()
  256. {
  257. return $this->exchangeArray(array());
  258. }
  259. /**
  260. * Whether or not this collection contains a specified element
  261. *
  262. * @param mixed $element the element
  263. *
  264. * @return boolean
  265. */
  266. public function contains($element)
  267. {
  268. return in_array($element, $this->getArrayCopy(), true);
  269. }
  270. /**
  271. * Search an element in the collection
  272. *
  273. * @param mixed $element
  274. *
  275. * @return mixed Returns the key for the element if it is found in the collection, FALSE otherwise
  276. */
  277. public function search($element)
  278. {
  279. return array_search($element, $this->getArrayCopy(), true);
  280. }
  281. // Serializable interface
  282. public function serialize()
  283. {
  284. $repr = array(
  285. 'data' => $this->getArrayCopy(),
  286. 'model' => $this->model,
  287. );
  288. return serialize($repr);
  289. }
  290. public function unserialize($data)
  291. {
  292. $repr = unserialize($data);
  293. $this->exchangeArray($repr['data']);
  294. $this->model = $repr['model'];
  295. }
  296. // IteratorAggregate method
  297. /**
  298. * Overrides ArrayObject::getIterator() to save the iterator object
  299. * for internal use e.g. getNext(), isOdd(), etc.
  300. */
  301. public function getIterator()
  302. {
  303. $this->iterator = new ArrayIterator($this);
  304. return $this->iterator;
  305. }
  306. public function getInternalIterator()
  307. {
  308. if (null === $this->iterator) {
  309. return $this->getIterator();
  310. }
  311. return $this->iterator;
  312. }
  313. // Propel collection methods
  314. /**
  315. * Set the model of the elements in the collection
  316. *
  317. * @param string $model Name of the Propel object classes stored in the collection
  318. */
  319. public function setModel($model)
  320. {
  321. $this->model = $model;
  322. }
  323. /**
  324. * Get the model of the elements in the collection
  325. *
  326. * @return string Name of the Propel object class stored in the collection
  327. */
  328. public function getModel()
  329. {
  330. return $this->model;
  331. }
  332. /**
  333. * Get the peer class of the elements in the collection
  334. *
  335. * @return string Name of the Propel peer class stored in the collection
  336. */
  337. public function getPeerClass()
  338. {
  339. if ($this->model == '') {
  340. throw new PropelException('You must set the collection model before interacting with it');
  341. }
  342. return constant($this->getModel() . '::PEER');
  343. }
  344. public function setFormatter(PropelFormatter $formatter)
  345. {
  346. $this->formatter = $formatter;
  347. }
  348. public function getFormatter()
  349. {
  350. return $this->formatter;
  351. }
  352. /**
  353. * Get a connection object for the database containing the elements of the collection
  354. *
  355. * @param string $type The connection type (Propel::CONNECTION_READ by default; can be Propel::connection_WRITE)
  356. *
  357. * @return PropelPDO a connection object
  358. */
  359. public function getConnection($type = Propel::CONNECTION_READ)
  360. {
  361. $databaseName = constant($this->getPeerClass() . '::DATABASE_NAME');
  362. return Propel::getConnection($databaseName, $type);
  363. }
  364. }
  365. ?>