Passed
Branch interfaces-and-less-statics (28360c)
by Stefan
02:41
created

DefaultCellBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 90
wmc 12
lcom 1
cbo 0
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
B build() 0 14 6
A getCellId() 0 8 2
A escapeControlCharacters() 0 4 1
1
<?php
2
/**
3
 * @author neun
4
 * @since  2016-07-11
5
 */
6
7
namespace OneSheet;
8
9
/**
10
 * Class CellBuilder to build xml cell strings. Wheter a numeric
11
 * value is written as a number or string type cell is simply
12
 * determined by type, to allow for some control by typecasting.
13
 *
14
 * @package OneSheet
15
 */
16
class DefaultCellBuilder implements CellBuilderInterface, NumberCellInterface, StringCellInterface
17
{
18
    /**
19
     * A-Z character array to build cell ids.
20
     *
21
     * @var array
22
     */
23
    private $characters = array();
24
25
    /**
26
     * Array of control characters that should be escaped.
27
     *
28
     * @var array
29
     */
30
    private $controlCharacters = array();
31
32
    /**
33
     * Array of escape characters to replace control characters.
34
     *
35
     * @var array
36
     */
37
    private $escapeCharacters = array();
38
39
    /**
40
     * CellBuilder constructor to instantiate character properties.
41
     */
42 4
    public function __construct()
43
    {
44 4
        $this->characters = range('A', 'Z');
45
46 4
        for ($i = 0; $i <= 255; $i++) {
47 4
            if (ctype_cntrl($char = chr($i))) {
48 4
                $this->controlCharacters[] = $char;
49 4
                $this->escapeCharacters[] = sprintf('_x%04s_', strtoupper(dechex($i)));
50 4
            }
51 4
        }
52 4
    }
53
54
    /**
55
     * Build and return the string for a single cell.
56
     *
57
     * @param int    $rowNumber
58
     * @param int    $cellNumber
59
     * @param string $value
60
     * @param int    $styleId
61
     * @return string
62
     */
63 2
    public function build($rowNumber, $cellNumber, $value, $styleId = 0)
64
    {
65 2
        $cellId = $this->getCellId($cellNumber, $rowNumber);
66
67 2
        if (is_int($value) || is_double($value)) {
68 2
            return sprintf(self::NUMBER_XML, $cellId, $styleId, $value);
69 2
        } elseif (ctype_alnum($value) || is_numeric($value)) {
70 2
            return sprintf(self::STRING_XML, $cellId, $styleId, $value);
71 1
        } elseif (ctype_print($value)) {
72 1
            return sprintf(self::STRING_XML, $cellId, $styleId, htmlspecialchars($value, ENT_QUOTES));
73
        } else {
74 1
            return sprintf(self::STRING_XML, $cellId, $styleId, $this->escapeControlCharacters($value));
75
        }
76
    }
77
78
    /**
79
     * Turn a integer cell number + row number into a valid cell identifier
80
     * like e.g. A1, Z1, AA1 etc and return it.
81
     *
82
     * @param int      $cellNumber
83
     * @param int|null $rowNumber
84
     * @return string
85
     */
86 2
    private function getCellId($cellNumber, $rowNumber = null)
87
    {
88 2
        if ($cellNumber / 26 < 1) {
89 2
            return $this->characters[$cellNumber] . $rowNumber;
90
        }
91
92 1
        return $this->getCellId(floor($cellNumber / 26) - 1) . $this->characters[$cellNumber % 26] . $rowNumber;
93
    }
94
95
    /**
96
     * Replace control characters with escape characters.
97
     *
98
     * @param string $value
99
     * @return string
100
     */
101 1
    private function escapeControlCharacters($value)
102
    {
103 1
        return str_replace($this->controlCharacters, $this->escapeCharacters, htmlspecialchars($value, ENT_QUOTES));
104
    }
105
}