ResultSet.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Db\ResultSet;
  10. use ArrayObject;
  11. class ResultSet extends AbstractResultSet
  12. {
  13. const TYPE_ARRAYOBJECT = 'arrayobject';
  14. const TYPE_ARRAY = 'array';
  15. /**
  16. * Allowed return types
  17. *
  18. * @var array
  19. */
  20. protected $allowedReturnTypes = [
  21. self::TYPE_ARRAYOBJECT,
  22. self::TYPE_ARRAY,
  23. ];
  24. /**
  25. * @var ArrayObject
  26. */
  27. protected $arrayObjectPrototype = null;
  28. /**
  29. * Return type to use when returning an object from the set
  30. *
  31. * @var ResultSet::TYPE_ARRAYOBJECT|ResultSet::TYPE_ARRAY
  32. */
  33. protected $returnType = self::TYPE_ARRAYOBJECT;
  34. /**
  35. * Constructor
  36. *
  37. * @param string $returnType
  38. * @param null|ArrayObject $arrayObjectPrototype
  39. */
  40. public function __construct($returnType = self::TYPE_ARRAYOBJECT, $arrayObjectPrototype = null)
  41. {
  42. if (in_array($returnType, $this->allowedReturnTypes, true)) {
  43. $this->returnType = $returnType;
  44. } else {
  45. $this->returnType = self::TYPE_ARRAYOBJECT;
  46. }
  47. if ($this->returnType === self::TYPE_ARRAYOBJECT) {
  48. $this->setArrayObjectPrototype(($arrayObjectPrototype) ?: new ArrayObject([], ArrayObject::ARRAY_AS_PROPS));
  49. }
  50. }
  51. /**
  52. * Set the row object prototype
  53. *
  54. * @param ArrayObject $arrayObjectPrototype
  55. * @return self Provides a fluent interface
  56. * @throws Exception\InvalidArgumentException
  57. */
  58. public function setArrayObjectPrototype($arrayObjectPrototype)
  59. {
  60. if (! is_object($arrayObjectPrototype)
  61. || (
  62. ! $arrayObjectPrototype instanceof ArrayObject
  63. && ! method_exists($arrayObjectPrototype, 'exchangeArray')
  64. )
  65. ) {
  66. throw new Exception\InvalidArgumentException(
  67. 'Object must be of type ArrayObject, or at least implement exchangeArray'
  68. );
  69. }
  70. $this->arrayObjectPrototype = $arrayObjectPrototype;
  71. return $this;
  72. }
  73. /**
  74. * Get the row object prototype
  75. *
  76. * @return ArrayObject
  77. */
  78. public function getArrayObjectPrototype()
  79. {
  80. return $this->arrayObjectPrototype;
  81. }
  82. /**
  83. * Get the return type to use when returning objects from the set
  84. *
  85. * @return string
  86. */
  87. public function getReturnType()
  88. {
  89. return $this->returnType;
  90. }
  91. /**
  92. * @return array|\ArrayObject|null
  93. */
  94. public function current()
  95. {
  96. $data = parent::current();
  97. if ($this->returnType === self::TYPE_ARRAYOBJECT && is_array($data)) {
  98. /** @var $ao ArrayObject */
  99. $ao = clone $this->arrayObjectPrototype;
  100. if ($ao instanceof ArrayObject || method_exists($ao, 'exchangeArray')) {
  101. $ao->exchangeArray($data);
  102. }
  103. return $ao;
  104. }
  105. return $data;
  106. }
  107. }