Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Cell;
4
 
5
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
6
use Stringable;
7
 
8
/**
9
 * @implements AddressRange<int>
10
 */
11
class RowRange implements AddressRange, Stringable
12
{
13
    protected ?Worksheet $worksheet;
14
 
15
    protected int $from;
16
 
17
    protected int $to;
18
 
19
    public function __construct(int $from, ?int $to = null, ?Worksheet $worksheet = null)
20
    {
21
        $this->validateFromTo($from, $to ?? $from);
22
        $this->worksheet = $worksheet;
23
    }
24
 
25
    public function __destruct()
26
    {
27
        $this->worksheet = null;
28
    }
29
 
30
    public static function fromArray(array $array, ?Worksheet $worksheet = null): self
31
    {
32
        [$from, $to] = $array;
33
 
34
        return new self($from, $to, $worksheet);
35
    }
36
 
37
    private function validateFromTo(int $from, int $to): void
38
    {
39
        // Identify actual top and bottom values (in case we've been given bottom and top)
40
        $this->from = min($from, $to);
41
        $this->to = max($from, $to);
42
    }
43
 
44
    public function from(): int
45
    {
46
        return $this->from;
47
    }
48
 
49
    public function to(): int
50
    {
51
        return $this->to;
52
    }
53
 
54
    public function rowCount(): int
55
    {
56
        return $this->to - $this->from + 1;
57
    }
58
 
59
    public function shiftRight(int $offset = 1): self
60
    {
61
        $newFrom = $this->from + $offset;
62
        $newFrom = ($newFrom < 1) ? 1 : $newFrom;
63
 
64
        $newTo = $this->to + $offset;
65
        $newTo = ($newTo < 1) ? 1 : $newTo;
66
 
67
        return new self($newFrom, $newTo, $this->worksheet);
68
    }
69
 
70
    public function shiftLeft(int $offset = 1): self
71
    {
72
        return $this->shiftRight(0 - $offset);
73
    }
74
 
75
    public function toCellRange(): CellRange
76
    {
77
        return new CellRange(
78
            CellAddress::fromColumnAndRow(Coordinate::columnIndexFromString('A'), $this->from, $this->worksheet),
79
            CellAddress::fromColumnAndRow(Coordinate::columnIndexFromString(AddressRange::MAX_COLUMN), $this->to)
80
        );
81
    }
82
 
83
    public function __toString(): string
84
    {
85
        if ($this->worksheet !== null) {
86
            $title = str_replace("'", "''", $this->worksheet->getTitle());
87
 
88
            return "'{$title}'!{$this->from}:{$this->to}";
89
        }
90
 
91
        return "{$this->from}:{$this->to}";
92
    }
93
}