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

CellHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 1
cbo 0
dl 0
loc 70
ccs 16
cts 18
cp 0.8889
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildCell() 0 15 4
A buildId() 0 8 2
A setCtrlCharacterMap() 0 4 1
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