ResultSetDecorator.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Datasource;
  16. use Cake\Datasource\ResultSetInterface;
  17. use Cake\Collection\Collection;
  18. /**
  19. * Generic ResultSet decorator. This will make any traversable object appear to
  20. * be a database result
  21. *
  22. * @return void
  23. */
  24. class ResultSetDecorator extends Collection implements ResultSetInterface {
  25. /**
  26. * Make this object countable.
  27. *
  28. * Part of the Countable interface. Calling this method
  29. * will convert the underlying traversable object into an array and
  30. * get the count of the underlying data.
  31. *
  32. * @return int
  33. */
  34. public function count() {
  35. if ($this->getInnerIterator() instanceof Countable) {
  36. return $this->getInnerIterator()->count();
  37. }
  38. return count($this->toArray());
  39. }
  40. /**
  41. * Serialize a resultset.
  42. *
  43. * Part of Serializable interface.
  44. *
  45. * @return string Serialized object
  46. */
  47. public function serialize() {
  48. return serialize($this->toArray());
  49. }
  50. /**
  51. * Unserialize a resultset.
  52. *
  53. * Part of Serializable interface.
  54. *
  55. * @param string $serialized Serialized object
  56. * @return void
  57. */
  58. public function unserialize($serialized) {
  59. parent::__construct(unserialize($serialized));
  60. }
  61. }