Range::setColTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Xls;
4
5
class Range
6
{
7
    protected $colFrom;
8
    protected $colTo;
9
    protected $rowFrom;
10
    protected $rowTo;
11
12
    /**
13
     * @param int  $rowFrom
14
     * @param int  $colFrom
15
     * @param int $rowTo
16
     * @param int $colTo
17
     * @param bool $normalize
18
     */
19
    public function __construct(
20
        $rowFrom = 0,
21
        $colFrom = 0,
22
        $rowTo = null,
23
        $colTo = null,
24
        $normalize = true
25
    ) {
26
        if (!isset($rowTo)) {
27
            $rowTo = $rowFrom; // Last row in reference
28
        }
29
30
        if (!isset($colTo)) {
31
            $colTo = $colFrom; // Last col in reference
32
        }
33
34
        $this->colFrom = $colFrom;
35
        $this->colTo = $colTo;
36
        $this->rowFrom = $rowFrom;
37
        $this->rowTo = $rowTo;
38
39
        if ($normalize) {
40
            $this->normalize();
41
        }
42
    }
43
44
    /**
45
     * Swap last row/col for first row/col as necessary
46
     */
47
    protected function normalize()
48
    {
49
        if ($this->rowFrom > $this->rowTo) {
50
            $tmp = $this->rowFrom;
51
            $this->rowTo = $this->rowFrom;
52
            $this->rowFrom = $tmp;
53
        }
54
55
        if ($this->colFrom > $this->colTo) {
56
            $tmp = $this->colFrom;
57
            $this->colTo = $this->colFrom;
58
            $this->colFrom = $tmp;
59
        }
60
    }
61
62
    /**
63
     * @return integer
64
     */
65
    public function getColFrom()
66
    {
67
        return $this->colFrom;
68
    }
69
70
    /**
71
     * @return integer
72
     */
73
    public function getColTo()
74
    {
75
        return $this->colTo;
76
    }
77
78
    /**
79
     * @return integer
80
     */
81
    public function getRowFrom()
82
    {
83
        return $this->rowFrom;
84
    }
85
86
    /**
87
     * @return integer
88
     */
89
    public function getRowTo()
90
    {
91
        return $this->rowTo;
92
    }
93
94
    /**
95
     * Include specified cell
96
     * @param Cell $cell
97
     * @return Range
98
     */
99
    public function expand(Cell $cell)
100
    {
101
        if ($cell->getRow() > $this->rowTo) {
102
            $this->setRowTo($cell->getRow());
103
        }
104
105
        if ($cell->getCol() > $this->colTo) {
106
            $this->setColTo($cell->getCol());
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return Cell
114
     */
115
    public function getStartCell()
116
    {
117
        return new Cell($this->rowFrom, $this->colFrom);
118
    }
119
120
    /**
121
     * @param int $colFrom
122
     * @return Range
123
     */
124
    public function setColFrom($colFrom)
125
    {
126
        $this->colFrom = $colFrom;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @param int|null $colTo
133
     * @return Range
134
     */
135
    public function setColTo($colTo)
136
    {
137
        $this->colTo = $colTo;
138
139
        return $this;
140
    }
141
142
    /**
143
     * @param int $rowFrom
144
     * @return Range
145
     */
146
    public function setRowFrom($rowFrom)
147
    {
148
        $this->rowFrom = $rowFrom;
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param int|null $rowTo
155
     * @return Range
156
     */
157
    public function setRowTo($rowTo)
158
    {
159
        $this->rowTo = $rowTo;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function isEmpty()
168
    {
169
        return is_null($this->getRowFrom()) && is_null($this->getColFrom());
170
    }
171
}
172