DoctrineDBALConnectionSourceIterator.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Exporter\Source;
  11. use Exporter\Exception\InvalidMethodCallException;
  12. use Doctrine\DBAL\Driver\Connection;
  13. use Doctrine\DBAL\Driver\Statement;
  14. class DoctrineDBALConnectionSourceIterator implements SourceIteratorInterface
  15. {
  16. protected $connection;
  17. protected $query;
  18. protected $parameters;
  19. protected $current;
  20. protected $position;
  21. /**
  22. * @var Statement
  23. */
  24. protected $statement;
  25. /**
  26. * @param Connection $statement
  27. */
  28. public function __construct(Connection $connection, $query, array $parameters = array())
  29. {
  30. $this->connection = $connection;
  31. $this->query = $query;
  32. $this->parameters = $parameters;
  33. $this->position = 0;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function current()
  39. {
  40. return $this->current;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function next()
  46. {
  47. $this->current = $this->statement->fetch(\PDO::FETCH_ASSOC);
  48. $this->position++;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function key()
  54. {
  55. return $this->position;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function valid()
  61. {
  62. return is_array($this->current);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function rewind()
  68. {
  69. if ($this->statement) {
  70. throw new InvalidMethodCallException('Cannot rewind a PDOStatement');
  71. }
  72. $this->statement = $this->connection->prepare($this->query);
  73. $this->statement->execute($this->parameters);
  74. $this->next();
  75. }
  76. }