Proyectos de Subversion Moodle

Rev

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