Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 15... Línea 15...
15
use OpenSpout\Reader\XLSX\Manager\StyleManagerInterface;
15
use OpenSpout\Reader\XLSX\Manager\StyleManagerInterface;
Línea 16... Línea 16...
16
 
16
 
17
/**
17
/**
18
 * This class provides helper functions to format cell values.
18
 * This class provides helper functions to format cell values.
19
 */
19
 */
20
final class CellValueFormatter
20
final readonly class CellValueFormatter
21
{
21
{
22
    /**
22
    /**
23
     * Definition of all possible cell types.
23
     * Definition of all possible cell types.
24
     */
24
     */
Línea 47... Línea 47...
47
     * Constants used for date formatting.
47
     * Constants used for date formatting.
48
     */
48
     */
49
    public const NUM_SECONDS_IN_ONE_DAY = 86400;
49
    public const NUM_SECONDS_IN_ONE_DAY = 86400;
Línea 50... Línea 50...
50
 
50
 
51
    /** @var SharedStringsManager Manages shared strings */
51
    /** @var SharedStringsManager Manages shared strings */
Línea 52... Línea 52...
52
    private readonly SharedStringsManager $sharedStringsManager;
52
    private SharedStringsManager $sharedStringsManager;
53
 
53
 
Línea 54... Línea 54...
54
    /** @var StyleManagerInterface Manages styles */
54
    /** @var StyleManagerInterface Manages styles */
55
    private readonly StyleManagerInterface $styleManager;
55
    private StyleManagerInterface $styleManager;
Línea 56... Línea 56...
56
 
56
 
57
    /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */
57
    /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */
Línea 58... Línea 58...
58
    private readonly bool $shouldFormatDates;
58
    private bool $shouldFormatDates;
59
 
59
 
Línea 60... Línea 60...
60
    /** @var bool Whether date/time values should use a calendar starting in 1904 instead of 1900 */
60
    /** @var bool Whether date/time values should use a calendar starting in 1904 instead of 1900 */
61
    private readonly bool $shouldUse1904Dates;
61
    private bool $shouldUse1904Dates;
62
 
62
 
63
    /** @var XLSX Used to unescape XML data */
63
    /** @var XLSX Used to unescape XML data */
Línea 94... Línea 94...
94
        if ('' === $cellType) {
94
        if ('' === $cellType) {
95
            $cellType = self::CELL_TYPE_NUMERIC;
95
            $cellType = self::CELL_TYPE_NUMERIC;
96
        }
96
        }
97
        $vNodeValue = $this->getVNodeValue($node);
97
        $vNodeValue = $this->getVNodeValue($node);
Línea 98... Línea -...
98
 
-
 
99
        if (self::CELL_TYPE_NUMERIC === $cellType) {
98
 
100
            $fNodeValue = $node->getElementsByTagName(self::XML_NODE_FORMULA)->item(0)?->nodeValue;
99
        $fNodeValue = $node->getElementsByTagName(self::XML_NODE_FORMULA)->item(0)?->nodeValue;
101
            if (null !== $fNodeValue) {
100
        if (null !== $fNodeValue) {
102
                $computedValue = $this->formatNumericCellValue($vNodeValue, (int) $node->getAttribute(self::XML_ATTRIBUTE_STYLE_ID));
101
            $computedValue = $this->formatRawValueForCellType($cellType, $node, $vNodeValue);
-
 
102
 
-
 
103
            return new Cell\FormulaCell(
-
 
104
                '='.$fNodeValue,
103
 
105
                null,
104
                return new Cell\FormulaCell('='.$fNodeValue, null, $computedValue);
106
                $computedValue instanceof Cell\ErrorCell ? null : $computedValue
105
            }
107
            );
Línea 106... Línea 108...
106
        }
108
        }
107
 
109
 
108
        if ('' === $vNodeValue && self::CELL_TYPE_INLINE_STRING !== $cellType) {
110
        if ('' === $vNodeValue && self::CELL_TYPE_INLINE_STRING !== $cellType) {
Línea 109... Línea -...
109
            return Cell::fromValue($vNodeValue);
-
 
110
        }
-
 
111
 
-
 
112
        $rawValue = match ($cellType) {
-
 
113
            self::CELL_TYPE_INLINE_STRING => $this->formatInlineStringCellValue($node),
-
 
114
            self::CELL_TYPE_SHARED_STRING => $this->formatSharedStringCellValue($vNodeValue),
-
 
115
            self::CELL_TYPE_STR => $this->formatStrCellValue($vNodeValue),
111
            return Cell::fromValue($vNodeValue);
116
            self::CELL_TYPE_BOOLEAN => $this->formatBooleanCellValue($vNodeValue),
-
 
117
            self::CELL_TYPE_NUMERIC => $this->formatNumericCellValue($vNodeValue, (int) $node->getAttribute(self::XML_ATTRIBUTE_STYLE_ID)),
-
 
Línea 118... Línea 112...
118
            self::CELL_TYPE_DATE => $this->formatDateCellValue($vNodeValue),
112
        }
119
            default => new Cell\ErrorCell($vNodeValue, null),
113
 
120
        };
114
        $rawValue = $this->formatRawValueForCellType($cellType, $node, $vNodeValue);
Línea 326... Línea 320...
326
            return new Cell\ErrorCell($nodeValue, null);
320
            return new Cell\ErrorCell($nodeValue, null);
327
        }
321
        }
Línea 328... Línea 322...
328
 
322
 
329
        return $cellValue;
323
        return $cellValue;
-
 
324
    }
-
 
325
 
-
 
326
    private function formatRawValueForCellType(
-
 
327
        string $cellType,
-
 
328
        DOMElement $node,
-
 
329
        string $vNodeValue
-
 
330
    ): bool|Cell\ErrorCell|DateInterval|DateTimeImmutable|float|int|string {
-
 
331
        return match ($cellType) {
-
 
332
            self::CELL_TYPE_INLINE_STRING => $this->formatInlineStringCellValue($node),
-
 
333
            self::CELL_TYPE_SHARED_STRING => $this->formatSharedStringCellValue($vNodeValue),
-
 
334
            self::CELL_TYPE_STR => $this->formatStrCellValue($vNodeValue),
-
 
335
            self::CELL_TYPE_BOOLEAN => $this->formatBooleanCellValue($vNodeValue),
-
 
336
            self::CELL_TYPE_NUMERIC => $this->formatNumericCellValue(
-
 
337
                $vNodeValue,
-
 
338
                (int) $node->getAttribute(self::XML_ATTRIBUTE_STYLE_ID)
-
 
339
            ),
-
 
340
            self::CELL_TYPE_DATE => $this->formatDateCellValue($vNodeValue),
-
 
341
            default => new Cell\ErrorCell($vNodeValue, null),
-
 
342
        };
330
    }
343
    }