Handler.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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;
  11. use Exporter\Source\SourceIteratorInterface;
  12. use Exporter\Writer\WriterInterface;
  13. class Handler
  14. {
  15. protected $source;
  16. protected $writer;
  17. /**
  18. * @param Source\SourceIteratorInterface $source
  19. * @param Writer\WriterInterface $writer
  20. */
  21. public function __construct(SourceIteratorInterface $source, WriterInterface $writer)
  22. {
  23. $this->source = $source;
  24. $this->writer = $writer;
  25. }
  26. /**
  27. * @return void
  28. */
  29. public function export()
  30. {
  31. $this->writer->open();
  32. foreach ($this->source as $data) {
  33. $this->writer->write($data);
  34. }
  35. $this->writer->close();
  36. }
  37. /**
  38. * @static
  39. *
  40. * @param Source\SourceIteratorInterface $source
  41. * @param Writer\WriterInterface $writer
  42. *
  43. * @return Handler
  44. */
  45. public static function create(SourceIteratorInterface $source, WriterInterface $writer)
  46. {
  47. return new self($source, $writer);
  48. }
  49. }