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;
6
 
7
use OpenSpout\Common\Entity\Row;
8
use OpenSpout\Common\Exception\IOException;
9
use OpenSpout\Writer\Common\Entity\Sheet;
10
use OpenSpout\Writer\Common\Manager\WorkbookManagerInterface;
11
use OpenSpout\Writer\Exception\SheetNotFoundException;
12
use OpenSpout\Writer\Exception\WriterNotOpenedException;
13
 
14
abstract class AbstractWriterMultiSheets extends AbstractWriter
15
{
16
    private WorkbookManagerInterface $workbookManager;
17
 
18
    /**
19
     * Returns all the workbook's sheets.
20
     *
21
     * @return Sheet[] All the workbook's sheets
22
     *
23
     * @throws WriterNotOpenedException If the writer has not been opened yet
24
     */
25
    final public function getSheets(): array
26
    {
27
        $this->throwIfWorkbookIsNotAvailable();
28
 
29
        $externalSheets = [];
30
        $worksheets = $this->workbookManager->getWorksheets();
31
 
32
        foreach ($worksheets as $worksheet) {
33
            $externalSheets[] = $worksheet->getExternalSheet();
34
        }
35
 
36
        return $externalSheets;
37
    }
38
 
39
    /**
40
     * Creates a new sheet and make it the current sheet. The data will now be written to this sheet.
41
     *
42
     * @return Sheet The created sheet
43
     *
44
     * @throws IOException
45
     * @throws WriterNotOpenedException If the writer has not been opened yet
46
     */
47
    final public function addNewSheetAndMakeItCurrent(): Sheet
48
    {
49
        $this->throwIfWorkbookIsNotAvailable();
50
        $worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent();
51
 
52
        return $worksheet->getExternalSheet();
53
    }
54
 
55
    /**
56
     * Returns the current sheet.
57
     *
58
     * @return Sheet The current sheet
59
     *
60
     * @throws WriterNotOpenedException If the writer has not been opened yet
61
     */
62
    final public function getCurrentSheet(): Sheet
63
    {
64
        $this->throwIfWorkbookIsNotAvailable();
65
 
66
        return $this->workbookManager->getCurrentWorksheet()->getExternalSheet();
67
    }
68
 
69
    /**
70
     * Sets the given sheet as the current one. New data will be written to this sheet.
71
     * The writing will resume where it stopped (i.e. data won't be truncated).
72
     *
73
     * @param Sheet $sheet The sheet to set as current
74
     *
75
     * @throws SheetNotFoundException   If the given sheet does not exist in the workbook
76
     * @throws WriterNotOpenedException If the writer has not been opened yet
77
     */
78
    final public function setCurrentSheet(Sheet $sheet): void
79
    {
80
        $this->throwIfWorkbookIsNotAvailable();
81
        $this->workbookManager->setCurrentSheet($sheet);
82
    }
83
 
84
    abstract protected function createWorkbookManager(): WorkbookManagerInterface;
85
 
86
    protected function openWriter(): void
87
    {
88
        if (!isset($this->workbookManager)) {
89
            $this->workbookManager = $this->createWorkbookManager();
90
            $this->workbookManager->addNewSheetAndMakeItCurrent();
91
        }
92
    }
93
 
94
    /**
95
     * @throws Exception\WriterException
96
     */
97
    protected function addRowToWriter(Row $row): void
98
    {
99
        $this->throwIfWorkbookIsNotAvailable();
100
        $this->workbookManager->addRowToCurrentWorksheet($row);
101
    }
102
 
103
    protected function closeWriter(): void
104
    {
105
        if (isset($this->workbookManager)) {
106
            $this->workbookManager->close($this->filePointer);
107
        }
108
    }
109
 
110
    /**
111
     * Checks if the workbook has been created. Throws an exception if not created yet.
112
     *
113
     * @throws WriterNotOpenedException If the workbook is not created yet
114
     */
115
    private function throwIfWorkbookIsNotAvailable(): void
116
    {
117
        if (!isset($this->workbookManager)) {
118
            throw new WriterNotOpenedException('The writer must be opened before performing this action.');
119
        }
120
    }
121
}