Completed
Pull Request — master (#538)
by Derek Stephen
05:36
created

CellHelper::isBoolean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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 36
    public static function getCellIndexFromColumnIndex($columnIndex)
26
    {
27 36
        $originalColumnIndex = $columnIndex;
28
29
        // Using isset here because it is way faster than array_key_exists...
30 36
        if (!isset(self::$columnIndexToCellIndexCache[$originalColumnIndex])) {
31 7
            $cellIndex = '';
32 7
            $capitalAAsciiValue = ord('A');
33
34
            do {
35 7
                $modulus = $columnIndex % 26;
36 7
                $cellIndex = chr($capitalAAsciiValue + $modulus) . $cellIndex;
37
38
                // substracting 1 because it's zero-based
39 7
                $columnIndex = intval($columnIndex / 26) - 1;
40
41 7
            } while ($columnIndex >= 0);
42
43 7
            self::$columnIndexToCellIndexCache[$originalColumnIndex] = $cellIndex;
44
        }
45
46 36
        return self::$columnIndexToCellIndexCache[$originalColumnIndex];
47
    }
48
49
    /**
50
     * @param $value
51
     * @return bool Whether the given value is considered "empty"
52
     */
53 16
    public static function isEmpty($value)
54
    {
55 16
        return ($value === null || $value === '');
56
    }
57
58
    /**
59
     * @param $value
60
     * @return bool Whether the given value is a non empty string
61
     */
62 61
    public static function isNonEmptyString($value)
63
    {
64 61
        if (is_object($value) && method_exists($value, '__toString')) {
65
            $value = $value->__toString();
66
        }
67 61
        return (gettype($value) === 'string' && $value !== '');
68
    }
69
70
    /**
71
     * Returns whether the given value is numeric.
72
     * A numeric value is from type "integer" or "double" ("float" is not returned by gettype).
73
     *
74
     * @param $value
75
     * @return bool Whether the given value is numeric
76
     */
77 10
    public static function isNumeric($value)
78
    {
79 10
        $valueType = gettype($value);
80 10
        return ($valueType === 'integer' || $valueType === 'double');
81
    }
82
83
    /**
84
     * Returns whether the given value is boolean.
85
     * "true"/"false" and 0/1 are not booleans.
86
     *
87
     * @param $value
88
     * @return bool Whether the given value is boolean
89
     */
90 11
    public static function isBoolean($value)
91
    {
92 11
        return gettype($value) === 'boolean';
93
    }
94
}
95