DoctrineORMQuerySourceIterator.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\ORM\Query;
  13. use Exporter\Source\SourceIteratorInterface;
  14. use Symfony\Component\Form\Util\PropertyPath;
  15. class DoctrineORMQuerySourceIterator implements SourceIteratorInterface
  16. {
  17. /**
  18. * @var \Doctrine\ORM\Query
  19. */
  20. protected $query;
  21. /**
  22. * @var \Doctrine\ORM\Internal\Hydration\IterableResult
  23. */
  24. protected $iterator;
  25. protected $propertyPaths;
  26. /**
  27. * @param \Doctrine\ORM\Query $query The Doctrine Query
  28. * @param array $fields Fields to export
  29. */
  30. public function __construct(Query $query, array $fields)
  31. {
  32. $this->query = clone $query;
  33. $this->query->setParameters($query->getParameters());
  34. $this->propertyPaths = array();
  35. foreach ($fields as $name => $field) {
  36. if (is_string($name) && is_string($field)) {
  37. $this->propertyPaths[$name] = new PropertyPath($field);
  38. } else {
  39. $this->propertyPaths[$field] = new PropertyPath($field);
  40. }
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function current()
  47. {
  48. $current = $this->iterator->current();
  49. $data = array();
  50. foreach ($this->propertyPaths as $name => $propertyPath) {
  51. $data[$name] = $this->getValue($propertyPath->getValue($current[0]));
  52. }
  53. $this->query->getEntityManager()->getUnitOfWork()->detach($current[0]);
  54. return $data;
  55. }
  56. /**
  57. * @param $value
  58. *
  59. * @return null|string
  60. */
  61. protected function getValue($value)
  62. {
  63. if (is_array($value) or $value instanceof \Traversable) {
  64. $value = null;
  65. } elseif ($value instanceof \DateTime) {
  66. $value = $value->format('r');
  67. } elseif (is_object($value)) {
  68. $value = (string) $value;
  69. }
  70. return $value;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function next()
  76. {
  77. $this->iterator->next();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function key()
  83. {
  84. return $this->iterator->key();
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function valid()
  90. {
  91. return $this->iterator->valid();
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function rewind()
  97. {
  98. if ($this->iterator) {
  99. throw new InvalidMethodCallException('Cannot rewind a Doctrine\ORM\Query');
  100. }
  101. $this->iterator = $this->query->iterate();
  102. $this->iterator->rewind();
  103. }
  104. }