DoctrineODMQuerySourceIterator.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\ODM\MongoDB\Query\Query;
  13. use Exporter\Source\SourceIteratorInterface;
  14. use Symfony\Component\Form\Util\PropertyPath;
  15. class DoctrineODMQuerySourceIterator 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\ODM\MongoDB\Query\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->propertyPaths = array();
  34. foreach ($fields as $name => $field) {
  35. if (is_string($name) && is_string($field)) {
  36. $this->propertyPaths[$name] = new PropertyPath($field);
  37. } else {
  38. $this->propertyPaths[$field] = new PropertyPath($field);
  39. }
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function current()
  46. {
  47. $current = $this->iterator->current();
  48. $data = array();
  49. foreach ($this->propertyPaths as $name => $propertyPath) {
  50. $data[$name] = $this->getValue($propertyPath->getValue($current));
  51. }
  52. $this->query->getDocumentManager()->getUnitOfWork()->detach($current);
  53. return $data;
  54. }
  55. /**
  56. * @param $value
  57. *
  58. * @return null|string
  59. */
  60. protected function getValue($value)
  61. {
  62. if (is_array($value) or $value instanceof \Traversable) {
  63. $value = null;
  64. } elseif ($value instanceof \DateTime) {
  65. $value = $value->format('r');
  66. } elseif (is_object($value)) {
  67. $value = (string) $value;
  68. }
  69. return $value;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function next()
  75. {
  76. $this->iterator->next();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function key()
  82. {
  83. return $this->iterator->key();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function valid()
  89. {
  90. return $this->iterator->valid();
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function rewind()
  96. {
  97. if ($this->iterator) {
  98. throw new InvalidMethodCallException('Cannot rewind a Doctrine\ODM\Query');
  99. }
  100. $this->iterator = $this->query->iterate();
  101. $this->iterator->rewind();
  102. }
  103. }