Completed
Push — master ( 2fafb6...179ab4 )
by Adrien
02:46
created

CellHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 81
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCellIndexFromColumnIndex() 0 23 3
A isEmpty() 0 4 2
A isNonEmptyString() 0 4 2
A isNumeric() 0 5 2
A isBoolean() 0 4 1
1
<?php
2
3
namespace Box\Spout\Writer\Common\Helper;
4
5
/**
6
 * Class CellHelper
7
 * This class provides helper functions when working with cells
8
 *
9
 * @package Box\Spout\Writer\Common\Helper
10
 */
11
class CellHelper
12
{
13
    /** @var array Cache containing the mapping column index => cell index */
14
    private static $columnIndexToCellIndexCache = [];
15
16
    /**
17
     * Returns the cell index (base 26) associated to the base 10 column index.
18
     * Excel uses A to Z letters for column indexing, where A is the 1st column,
19
     * Z is the 26th and AA is the 27th.
20
     * The mapping is zero based, so that 0 maps to A, B maps to 1, Z to 25 and AA to 26.
21
     *
22
     * @param int $columnIndex The Excel column index (0, 42, ...)
23
     * @return string The associated cell index ('A', 'BC', ...)
24
     */
25 105
    public static function getCellIndexFromColumnIndex($columnIndex)
26
    {
27 105
        $originalColumnIndex = $columnIndex;
28
29
        // Using isset here because it is way faster than array_key_exists...
30 105
        if (!isset(self::$columnIndexToCellIndexCache[$originalColumnIndex])) {
31 21
            $cellIndex = '';
32 21
            $capitalAAsciiValue = ord('A');
33
34
            do {
35 21
                $modulus = $columnIndex % 26;
36 21
                $cellIndex = chr($capitalAAsciiValue + $modulus) . $cellIndex;
37
38
                // substracting 1 because it's zero-based
39 21
                $columnIndex = intval($columnIndex / 26) - 1;
40
41 21
            } while ($columnIndex >= 0);
42
43 21
            self::$columnIndexToCellIndexCache[$originalColumnIndex] = $cellIndex;
44 21
        }
45
46 105
        return self::$columnIndexToCellIndexCache[$originalColumnIndex];
47
    }
48
49
    /**
50
     * @param $value
51
     * @return bool Whether the given value is considered "empty"
52
     */
53 45
    public static function isEmpty($value)
54
    {
55 45
        return ($value === null || $value === '');
56
    }
57
58
    /**
59
     * @param $value
60
     * @return bool Whether the given value is a non empty string
61
     */
62 177
    public static function isNonEmptyString($value)
63
    {
64 177
        return (gettype($value) === 'string' && $value !== '');
65
    }
66
67
    /**
68
     * Returns whether the given value is numeric.
69
     * A numeric value is from type "integer" or "double" ("float" is not returned by gettype).
70
     *
71
     * @param $value
72
     * @return bool Whether the given value is numeric
73
     */
74 30
    public static function isNumeric($value)
75
    {
76 30
        $valueType = gettype($value);
77 30
        return ($valueType === 'integer' || $valueType === 'double');
78
    }
79
80
    /**
81
     * Returns whether the given value is boolean.
82
     * "true"/"false" and 0/1 are not booleans.
83
     *
84
     * @param $value
85
     * @return bool Whether the given value is boolean
86
     */
87 33
    public static function isBoolean($value)
88
    {
89 33
        return gettype($value) === 'boolean';
90
    }
91
}
92