Completed
Push — master ( 9267fe...d7c534 )
by Stefan
02:57
created

CellHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 9
c 4
b 0
f 1
lcom 1
cbo 0
dl 0
loc 73
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B buildCell() 0 15 6
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
     * Wheter a numeric value is written as a number
35
     * or string type cell is determined by type,
36
     * which allows for some control by typecasting.
37
     *
38
     * @param int $rowId
39
     * @param int $cellNo
40
     * @param string $value
41
     * @param int|null $styleId
42
     * @return string
43
     * @throws \InvalidArgumentException
44
     */
45 3
    public static function buildCell($rowId, $cellNo, $value, $styleId = 0)
46
    {
47 3
        if (is_int($value) || is_double($value)) {
48 3
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '"><v>' . $value . '</v></c>';
49 3
        } elseif (ctype_alnum($value) || is_numeric($value)) {
50 3
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '" t="inlineStr"><is><t>'
51 3
            . $value . '</t></is></c>';
52 2
        } elseif (ctype_print($value)) {
53 2
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '" t="inlineStr"><is><t>'
54 2
            . htmlspecialchars($value, ENT_QUOTES) . '</t></is></c>';
55
        } else {
56 1
            return '<c r="' . self::buildId($cellNo, $rowId) . '" s="' . $styleId . '" t="inlineStr"><is><t>'
57 1
            . str_replace(self::$ctrls['from'], self::$ctrls['to'], htmlspecialchars($value)) . '</t></is></c>';
58
        }
59
    }
60
61
    /**
62
     * Turn a integer cell number + row number into a valid cell identifier
63
     * like e.g. A1, Z1, AA1 etc and return it.
64
     *
65
     * @param int $cellNo
66
     * @param int|null $rowIndex
67
     * @return string
68
     */
69 3
    private static function buildId($cellNo, $rowIndex = null)
70
    {
71 3
        if ($cellNo / 26 < 1) {
72 3
            return self::$chars[$cellNo] . $rowIndex;
73
        }
74
75 1
        return self::buildId(floor($cellNo / 26) - 1) . self::$chars[$cellNo % 26] . $rowIndex;
76
    }
77
78
    /**
79
     * Set mapping for control character escaping.
80
     *
81
     * @param array $map
82
     */
83 4
    public static function setCtrlCharacterMap(array $map)
84
    {
85 4
        self::$ctrls = $map;
86 4
    }
87
}
88