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\Reader\CSV;
6
 
7
use OpenSpout\Common\Exception\IOException;
8
use OpenSpout\Common\Helper\EncodingHelper;
9
use OpenSpout\Reader\AbstractReader;
10
 
11
/**
12
 * @extends AbstractReader<SheetIterator>
13
 */
14
final class Reader extends AbstractReader
15
{
16
    /** @var resource Pointer to the file to be written */
17
    private $filePointer;
18
 
19
    /** @var SheetIterator To iterator over the CSV unique "sheet" */
20
    private SheetIterator $sheetIterator;
21
 
22
    private readonly Options $options;
23
    private readonly EncodingHelper $encodingHelper;
24
 
25
    public function __construct(
26
        ?Options $options = null,
27
        ?EncodingHelper $encodingHelper = null
28
    ) {
29
        $this->options = $options ?? new Options();
30
        $this->encodingHelper = $encodingHelper ?? EncodingHelper::factory();
31
    }
32
 
33
    public function getSheetIterator(): SheetIterator
34
    {
35
        $this->ensureStreamOpened();
36
 
37
        return $this->sheetIterator;
38
    }
39
 
40
    /**
41
     * Returns whether stream wrappers are supported.
42
     */
43
    protected function doesSupportStreamWrapper(): bool
44
    {
45
        return true;
46
    }
47
 
48
    /**
49
     * Opens the file at the given path to make it ready to be read.
50
     * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
51
     *
52
     * @param string $filePath Path of the CSV file to be read
53
     *
54
     * @throws IOException
55
     */
56
    protected function openReader(string $filePath): void
57
    {
58
        $resource = fopen($filePath, 'r');
59
        \assert(false !== $resource);
60
        $this->filePointer = $resource;
61
 
62
        $this->sheetIterator = new SheetIterator(
63
            new Sheet(
64
                new RowIterator(
65
                    $this->filePointer,
66
                    $this->options,
67
                    $this->encodingHelper
68
                )
69
            )
70
        );
71
    }
72
 
73
    /**
74
     * Closes the reader. To be used after reading the file.
75
     */
76
    protected function closeReader(): void
77
    {
78
        fclose($this->filePointer);
79
    }
80
}