Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace OpenSpout\Writer\CSV;
6
 
1441 ariadna 7
use Exception;
1 efrain 8
use OpenSpout\Common\Entity\Cell;
9
use OpenSpout\Common\Entity\Row;
10
use OpenSpout\Common\Exception\IOException;
11
use OpenSpout\Common\Helper\EncodingHelper;
12
use OpenSpout\Writer\AbstractWriter;
13
 
14
final class Writer extends AbstractWriter
15
{
16
    /** @var string Content-Type value for the header */
17
    protected static string $headerContentType = 'text/csv; charset=UTF-8';
18
 
19
    private readonly Options $options;
20
 
21
    private int $lastWrittenRowIndex = 0;
22
 
23
    public function __construct(?Options $options = null)
24
    {
25
        $this->options = $options ?? new Options();
26
    }
27
 
28
    public function getOptions(): Options
29
    {
30
        return $this->options;
31
    }
32
 
1441 ariadna 33
    public function setCreator(string $creator): void
34
    {
35
        throw new Exception('Method unsopported for CSV documents');
36
    }
37
 
1 efrain 38
    /**
39
     * Opens the CSV streamer and makes it ready to accept data.
40
     */
41
    protected function openWriter(): void
42
    {
43
        if ($this->options->SHOULD_ADD_BOM) {
44
            // Adds UTF-8 BOM for Unicode compatibility
45
            fwrite($this->filePointer, EncodingHelper::BOM_UTF8);
46
        }
47
    }
48
 
49
    /**
50
     * Adds a row to the currently opened writer.
51
     *
52
     * @param Row $row The row containing cells and styles
53
     *
54
     * @throws IOException If unable to write data
55
     */
56
    protected function addRowToWriter(Row $row): void
57
    {
58
        $cells = array_map(static function (Cell\BooleanCell|Cell\DateIntervalCell|Cell\DateTimeCell|Cell\EmptyCell|Cell\FormulaCell|Cell\NumericCell|Cell\StringCell $value): string {
59
            if ($value instanceof Cell\BooleanCell) {
60
                return (string) (int) $value->getValue();
61
            }
62
            if ($value instanceof Cell\DateTimeCell) {
63
                return $value->getValue()->format(DATE_ATOM);
64
            }
65
            if ($value instanceof Cell\DateIntervalCell) {
66
                return $value->getValue()->format('P%yY%mM%dDT%hH%iM%sS%fF');
67
            }
68
 
69
            return (string) $value->getValue();
70
        }, $row->getCells());
71
 
72
        $wasWriteSuccessful = fputcsv(
73
            $this->filePointer,
74
            $cells,
75
            $this->options->FIELD_DELIMITER,
76
            $this->options->FIELD_ENCLOSURE,
77
            ''
78
        );
79
        if (false === $wasWriteSuccessful) {
80
            throw new IOException('Unable to write data'); // @codeCoverageIgnore
81
        }
82
 
83
        ++$this->lastWrittenRowIndex;
84
        if (0 === $this->lastWrittenRowIndex % $this->options->FLUSH_THRESHOLD) {
85
            fflush($this->filePointer);
86
        }
87
    }
88
 
89
    /**
90
     * Closes the CSV streamer, preventing any additional writing.
91
     * If set, sets the headers and redirects output to the browser.
92
     */
93
    protected function closeWriter(): void
94
    {
95
        $this->lastWrittenRowIndex = 0;
96
    }
97
}