Completed
Pull Request — master (#7)
by Stefan
03:14
created

CellBuilder::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 3
eloc 5
nc 3
nop 0
crap 3
1
<?php
2
3
namespace OneSheet;
4
5
use OneSheet\Xml\CellXml;
6
7
/**
8
 * Class DefaultCellBuilder to build xml cell strings.
9
 * Wheter a numeric value is written as a number or string type cell
10
 * is simply determined by type, to allow for some control by typecasting.
11
 *
12
 * @package OneSheet
13
 */
14
class CellBuilder
15
{
16
    /**
17
     * Array of control characters that should be escaped.
18
     *
19
     * @var array
20
     */
21
    private $controlCharacters = array();
22
23
    /**
24
     * Array of escape characters to replace control characters.
25
     *
26
     * @var array
27
     */
28
    private $escapeCharacters = array();
29
30
    /**
31
     * CellBuilder constructor to create escaping arrays.
32
     */
33 5
    public function __construct()
34
    {
35 5
        foreach (range(chr(0), chr(31)) as $key => $character) {
36 5
            if (!in_array($character, array("\n", "\r", "\t"))) {
37 5
                $this->controlCharacters[] = $character;
38 5
                $this->escapeCharacters[] = sprintf('_x%04s_', strtoupper(dechex($key)));
39 5
            }
40 5
        }
41 5
    }
42
43
    /**
44
     * Build and return the string for a single cell.
45
     *
46
     * @param int   $rowNumber
47
     * @param int   $cellNumber
48
     * @param mixed $cellValue
49
     * @param int   $styleId
50
     *
51
     * @return string
52
     */
53 2
    public function build($rowNumber, $cellNumber, $cellValue, $styleId = 0)
54
    {
55 2
        $cellId = $this->getCellId($cellNumber, $rowNumber);
56
57 2
        if (is_int($cellValue) || is_double($cellValue)) {
58 1
            return sprintf(CellXml::NUMBER_XML, $cellId, $styleId, $cellValue);
59 2
        } elseif (is_bool($cellValue)) {
60 1
            return sprintf(CellXml::BOOLEAN_XML, $cellId, $styleId, (int)$cellValue);
61 2
        } elseif (is_numeric($cellValue) || 1 != preg_match('~[^\w]~', $cellValue)) {
62 2
            return sprintf(CellXml::STRING_XML, $cellId, $styleId,
63 2
                htmlspecialchars($cellValue, ENT_QUOTES));
64
        }
65
66 1
        return sprintf(CellXml::STRING_XML, $cellId, $styleId, $this->escape($cellValue));
0 ignored issues
show
Bug introduced by
It seems like $cellValue defined by parameter $cellValue on line 53 can also be of type array or null or object; however, OneSheet\CellBuilder::escape() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
67
    }
68
69
    /**
70
     * Turn a integer cell number + row number into a valid cell identifier
71
     * like e.g. A1, Z1, AA1 etc and return it.
72
     *
73
     * @param int      $cellNumber
74
     * @param int|null $rowNumber
75
     *
76
     * @return string
77
     */
78 2
    public function getCellId($cellNumber, $rowNumber = null)
79
    {
80 2
        if ($cellNumber / 26 < 1) {
81 2
            return chr(65 + $cellNumber) . $rowNumber;
82
        }
83
84 1
        return $this->getCellId(floor($cellNumber / 26) - 1) . chr(65 + $cellNumber % 26) . $rowNumber;
85
    }
86
87
    /**
88
     * Escape/replace control characters.
89
     *
90
     * @param string $value
91
     *
92
     * @return string
93
     */
94 1
    private function escape($value)
95
    {
96 1
        return str_replace($this->controlCharacters, $this->escapeCharacters,
97 1
            htmlspecialchars($value, ENT_QUOTES));
98
    }
99
}
100
101