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\Common\Entity;
6
 
7
use DateInterval;
8
use DateTimeInterface;
9
use OpenSpout\Common\Entity\Style\Style;
10
 
11
final class Row
12
{
13
    /**
14
     * The cells in this row.
15
     *
16
     * @var Cell[]
17
     */
18
    private array $cells = [];
19
 
20
    /** The row style. */
21
    private Style $style;
22
 
23
    /** Row height. */
24
    private float $height = 0;
25
 
26
    /**
27
     * Row constructor.
28
     *
29
     * @param Cell[] $cells
30
     */
31
    public function __construct(array $cells, ?Style $style = null)
32
    {
33
        $this
34
            ->setCells($cells)
35
            ->setStyle($style)
36
        ;
37
    }
38
 
39
    /**
40
     * @param list<null|bool|DateInterval|DateTimeInterface|float|int|string> $cellValues
41
     */
42
    public static function fromValues(array $cellValues = [], ?Style $rowStyle = null): self
43
    {
44
        $cells = array_map(static function (null|bool|DateInterval|DateTimeInterface|float|int|string $cellValue): Cell {
45
            return Cell::fromValue($cellValue);
46
        }, $cellValues);
47
 
48
        return new self($cells, $rowStyle);
49
    }
50
 
51
    /**
1441 ariadna 52
     * @param array<array-key, null|bool|DateInterval|DateTimeInterface|float|int|string> $cellValues
53
     * @param array<array-key, Style>                                                     $columnStyles
54
     */
55
    public static function fromValuesWithStyles(array $cellValues = [], ?Style $rowStyle = null, array $columnStyles = []): self
56
    {
57
        $cells = array_map(static function (null|bool|DateInterval|DateTimeInterface|float|int|string $cellValue, int|string $key) use ($columnStyles): Cell {
58
            return Cell::fromValue($cellValue, $columnStyles[$key] ?? null);
59
        }, $cellValues, array_keys($cellValues));
60
 
61
        return new self($cells, $rowStyle);
62
    }
63
 
64
    /**
1 efrain 65
     * @return Cell[] $cells
66
     */
67
    public function getCells(): array
68
    {
69
        return $this->cells;
70
    }
71
 
72
    /**
73
     * @param Cell[] $cells
74
     */
75
    public function setCells(array $cells): self
76
    {
77
        $this->cells = [];
78
        foreach ($cells as $cell) {
79
            $this->addCell($cell);
80
        }
81
 
82
        return $this;
83
    }
84
 
85
    public function setCellAtIndex(Cell $cell, int $cellIndex): self
86
    {
87
        $this->cells[$cellIndex] = $cell;
88
 
89
        return $this;
90
    }
91
 
92
    public function getCellAtIndex(int $cellIndex): ?Cell
93
    {
94
        return $this->cells[$cellIndex] ?? null;
95
    }
96
 
97
    public function addCell(Cell $cell): self
98
    {
99
        $this->cells[] = $cell;
100
 
101
        return $this;
102
    }
103
 
104
    public function getNumCells(): int
105
    {
106
        // When using "setCellAtIndex", it's possible to
107
        // have "$this->cells" contain holes.
108
        if ([] === $this->cells) {
109
            return 0;
110
        }
111
 
112
        return max(array_keys($this->cells)) + 1;
113
    }
114
 
115
    public function getStyle(): Style
116
    {
117
        return $this->style;
118
    }
119
 
120
    public function setStyle(?Style $style): self
121
    {
122
        $this->style = $style ?? new Style();
123
 
124
        return $this;
125
    }
126
 
127
    /**
128
     * Set row height.
129
     */
130
    public function setHeight(float $height): self
131
    {
132
        $this->height = $height;
133
 
134
        return $this;
135
    }
136
 
137
    /**
138
     * Returns row height.
139
     */
140
    public function getHeight(): float
141
    {
142
        return $this->height;
143
    }
144
 
145
    /**
146
     * @return list<null|bool|DateInterval|DateTimeInterface|float|int|string> The row values, as array
147
     */
148
    public function toArray(): array
149
    {
150
        return array_map(static function (Cell $cell): null|bool|DateInterval|DateTimeInterface|float|int|string {
151
            return $cell->getValue();
152
        }, $this->cells);
153
    }
154
 
155
    /**
156
     * Detect whether a row is considered empty.
157
     * An empty row has all of its cells empty.
158
     */
159
    public function isEmpty(): bool
160
    {
161
        foreach ($this->cells as $cell) {
162
            if (!$cell instanceof Cell\EmptyCell) {
163
                return false;
164
            }
165
        }
166
 
167
        return true;
168
    }
169
}