Completed
Push — master ( 4d99f9...0234dd )
by Stefan
02:22
created

CellHelper::buildCell()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 9
cts 11
cp 0.8182
rs 9.2
cc 4
eloc 12
nc 4
nop 4
crap 4.0961
1
<?php
2
/**
3
 * @author neun
4
 * @since  2016-07-03
5
 */
6
7
namespace OneSheet;
8
9
/**
10
 * Static class to build cell strings.
11
 *
12
 * Class CellHelper
13
 * @package OneSheet
14
 */
15
class CellHelper
16
{
17
    /**
18
     * Fixed character array to build cell ids, because chr(65+n)
19
     * eats to much performance.
20
     *
21
     * @var array
22
     */
23
    private static $chars = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
24
25
    /**
26
     * Control character map for escaping.
27
     *
28
     * @var array(array())
29
     */
30
    private static $ctrls = array();
31
32
    /**
33
     * Build and return the string for a single cell.
34
     *
35
     * @param int $rowId
36
     * @param int $cellNo
37
     * @param string $value
38
     * @param int|null $styleId
39
     * @return string
40
     * @throws \InvalidArgumentException
41
     */
42 3
    public static function buildCell($rowId, $cellNo, $value, $styleId = 0)
43
    {
44 3
        if (is_numeric($value)) {
45 3
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '"><v>' . $value . '</v></c>';
46 3
        } elseif (ctype_alnum($value)) {
47 2
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '" t="inlineStr"><is><t>'
48 2
            . $value . '</t></is></c>';
49 2
        } elseif (ctype_print($value)) {
50 2
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '" t="inlineStr"><is><t>'
51 2
            . htmlspecialchars($value, ENT_QUOTES) . '</t></is></c>';
52
        } else {
53
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '" t="inlineStr"><is><t>'
54
            . str_replace(self::$ctrls['from'], self::$ctrls['to'], htmlspecialchars($value)) . '</t></is></c>';
55
        }
56
    }
57
58
    /**
59
     * Turn a integer cell number + row number into a valid cell identifier
60
     * like e.g. A1, Z1, AA1 etc and return it.
61
     *
62
     * @param int $cellNo
63
     * @param int|null $rowIndex
64
     * @return string
65
     */
66 3
    private static function buildId($cellNo, $rowIndex = null)
67
    {
68 3
        if ($cellNo / 26 < 1) {
69 3
            return self::$chars[$cellNo] . $rowIndex;
70
        }
71
72 1
        return self::buildId(floor($cellNo / 26) - 1) . self::$chars[$cellNo % 26] . $rowIndex;
73
    }
74
75
    /**
76
     * Set mapping for control character escaping.
77
     *
78
     * @param array $map
79
     */
80 3
    public static function setCtrlCharacterMap(array $map)
81
    {
82 3
        self::$ctrls = $map;
83 3
    }
84
}
85