Cell::createFromAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
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 Cell
6
{
7
    protected $row;
8
    protected $col;
9
    protected $rowRel;
10
    protected $colRel;
11
12
    /**
13
     * @param int $row
14
     * @param int $col
15
     * @param bool $rowRel
16
     * @param bool $colRel
17
     *
18
     * @throws \Exception
19
     */
20
    public function __construct($row, $col, $rowRel = true, $colRel = true)
21
    {
22
        $this->validateRowIndex($row);
23
        $this->validateColIndex($col);
24
25
        $this->row = $row;
26
        $this->col = $col;
27
        $this->rowRel = $rowRel;
28
        $this->colRel = $colRel;
29
    }
30
31
    /**
32
     * @param $address
33
     *
34
     * @return Cell
35
     */
36
    public static function createFromAddress($address)
37
    {
38
        list($row, $col, $rowRel, $colRel) = self::addressToRowCol($address);
39
40
        return new self($row, $col, $rowRel, $colRel);
41
    }
42
43
    /**
44
     * @return int
45
     */
46
    public function getRow()
47
    {
48
        return $this->row;
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getCol()
55
    {
56
        return $this->col;
57
    }
58
59
    /**
60
     * Utility function for writing formulas
61
     * Converts a cell's coordinates to the A1 format.
62
     *
63
     * @throws \Exception
64
     * @return string The cell identifier in A1 format
65
     */
66
    public function getAddress()
67
    {
68
        $int = (int)($this->col / 26);
69
        $frac = $this->col % 26;
70
        $chr1 = '';
71
72
        if ($int > 0) {
73
            $chr1 = chr(ord('A') + $int - 1);
74
        }
75
76
        $chr2 = chr(ord('A') + $frac);
77
78
        return $chr1 . $chr2 . ($this->row + 1);
79
    }
80
81
    /**
82
     * Convert an Excel cell reference such as A1 or $B2 or C$3 or $D$4 to a zero
83
     * indexed row and column number. Also returns two (0,1) values to indicate
84
     * whether the row or column are relative references.
85
     *
86
     * @param string $address The Excel cell reference in A1 format.
87
     * @return array
88
     */
89
    public static function addressToRowCol($address)
90
    {
91
        preg_match('/(\$)?([A-Z]+)(\$)?(\d+)/', $address, $match);
92
        // return absolute column if there is a $ in the ref
93
        $colRel = empty($match[1]);
94
        $colRef = $match[2];
95
        $rowRel = empty($match[3]);
96
        $row = $match[4];
97
98
        // Convert base26 column string to a number.
99
        $expn = strlen($colRef) - 1;
100
        $col = 0;
101
        $colRefLength = strlen($colRef);
102
        for ($i = 0; $i < $colRefLength; $i++) {
103
            $col += (ord($colRef{$i}) - ord('A') + 1) * pow(26, $expn);
104
            $expn--;
105
        }
106
107
        // Convert 1-index to zero-index
108
        $row--;
109
        $col--;
110
111
        return array($row, $col, $rowRel, $colRel);
112
    }
113
114
    /**
115
     * @param $row
116
     *
117
     * @throws \Exception
118
     */
119
    protected function validateRowIndex($row)
120
    {
121
        if ($row > Biff8::MAX_ROW_IDX) {
122
            throw new \Exception('Row index is beyond max row number');
123
        }
124
    }
125
126
    /**
127
     * @param $col
128
     *
129
     * @throws \Exception
130
     */
131
    protected function validateColIndex($col)
132
    {
133
        if ($col > Biff8::MAX_COL_IDX) {
134
            throw new \Exception('Col index is beyond max col number');
135
        }
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function isRowRelative()
142
    {
143
        return $this->rowRel;
144
    }
145
146
    /**
147
     * @return bool
148
     */
149
    public function isColRelative()
150
    {
151
        return $this->colRel;
152
    }
153
}
154