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\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
    /**
52
     * @return Cell[] $cells
53
     */
54
    public function getCells(): array
55
    {
56
        return $this->cells;
57
    }
58
 
59
    /**
60
     * @param Cell[] $cells
61
     */
62
    public function setCells(array $cells): self
63
    {
64
        $this->cells = [];
65
        foreach ($cells as $cell) {
66
            $this->addCell($cell);
67
        }
68
 
69
        return $this;
70
    }
71
 
72
    public function setCellAtIndex(Cell $cell, int $cellIndex): self
73
    {
74
        $this->cells[$cellIndex] = $cell;
75
 
76
        return $this;
77
    }
78
 
79
    public function getCellAtIndex(int $cellIndex): ?Cell
80
    {
81
        return $this->cells[$cellIndex] ?? null;
82
    }
83
 
84
    public function addCell(Cell $cell): self
85
    {
86
        $this->cells[] = $cell;
87
 
88
        return $this;
89
    }
90
 
91
    public function getNumCells(): int
92
    {
93
        // When using "setCellAtIndex", it's possible to
94
        // have "$this->cells" contain holes.
95
        if ([] === $this->cells) {
96
            return 0;
97
        }
98
 
99
        return max(array_keys($this->cells)) + 1;
100
    }
101
 
102
    public function getStyle(): Style
103
    {
104
        return $this->style;
105
    }
106
 
107
    public function setStyle(?Style $style): self
108
    {
109
        $this->style = $style ?? new Style();
110
 
111
        return $this;
112
    }
113
 
114
    /**
115
     * Set row height.
116
     */
117
    public function setHeight(float $height): self
118
    {
119
        $this->height = $height;
120
 
121
        return $this;
122
    }
123
 
124
    /**
125
     * Returns row height.
126
     */
127
    public function getHeight(): float
128
    {
129
        return $this->height;
130
    }
131
 
132
    /**
133
     * @return list<null|bool|DateInterval|DateTimeInterface|float|int|string> The row values, as array
134
     */
135
    public function toArray(): array
136
    {
137
        return array_map(static function (Cell $cell): null|bool|DateInterval|DateTimeInterface|float|int|string {
138
            return $cell->getValue();
139
        }, $this->cells);
140
    }
141
 
142
    /**
143
     * Detect whether a row is considered empty.
144
     * An empty row has all of its cells empty.
145
     */
146
    public function isEmpty(): bool
147
    {
148
        foreach ($this->cells as $cell) {
149
            if (!$cell instanceof Cell\EmptyCell) {
150
                return false;
151
            }
152
        }
153
 
154
        return true;
155
    }
156
}