Completed
Push — develop ( 672893...dd9590 )
by Adrien
28:52 queued 21:58
created

DefaultValueBinder::bindValue()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 2
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
crap 5.025
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Cell;
4
5
use DateTime;
6
use PhpOffice\PhpSpreadsheet\Cell;
7
use PhpOffice\PhpSpreadsheet\RichText;
8
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9
10
/**
11
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
12
 *
13
 * This library is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU Lesser General Public
15
 * License as published by the Free Software Foundation; either
16
 * version 2.1 of the License, or (at your option) any later version.
17
 *
18
 * This library is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
 * Lesser General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public
24
 * License along with this library; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
 *
27
 * @category   PhpSpreadsheet
28
 *
29
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
30
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
31
 */
32
class DefaultValueBinder implements IValueBinder
33
{
34
    /**
35
     * Bind value to a cell.
36
     *
37
     * @param Cell $cell Cell to bind value to
38
     * @param mixed $value Value to bind in cell
39
     *
40
     * @return bool
41
     */
42 71
    public function bindValue(Cell $cell, $value = null)
43
    {
44
        // sanitize UTF-8 strings
45 71
        if (is_string($value)) {
46 60
            $value = StringHelper::sanitizeUTF8($value);
47 52
        } elseif (is_object($value)) {
48
            // Handle any objects that might be injected
49 17
            if ($value instanceof DateTime) {
50 1
                $value = $value->format('Y-m-d H:i:s');
51 16
            } elseif (!($value instanceof RichText)) {
52
                $value = (string) $value;
53
            }
54
        }
55
56
        // Set value explicit
57 71
        $cell->setValueExplicit($value, self::dataTypeForValue($value));
58
59
        // Done!
60 71
        return true;
61
    }
62
63
    /**
64
     * DataType for value.
65
     *
66
     * @param mixed $pValue
67
     *
68
     * @return string
69
     */
70 98
    public static function dataTypeForValue($pValue)
71
    {
72
        // Match the value against a few data types
73 98
        if ($pValue === null) {
74 18
            return DataType::TYPE_NULL;
75 96
        } elseif ($pValue === '') {
76 14
            return DataType::TYPE_STRING;
77
        } elseif ($pValue instanceof RichText) {
78 17
            return DataType::TYPE_INLINE;
79 92
        } elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
80 24
            return DataType::TYPE_FORMULA;
81 90
        } elseif (is_bool($pValue)) {
82 10
            return DataType::TYPE_BOOL;
83 86
        } elseif (is_float($pValue) || is_int($pValue)) {
84 47
            return DataType::TYPE_NUMERIC;
85 78
        } elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
86 29
            $tValue = ltrim($pValue, '+-');
87 29
            if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
88 1
                return DataType::TYPE_STRING;
89 28
            } elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
90
                return DataType::TYPE_STRING;
91
            }
92
93 28
            return DataType::TYPE_NUMERIC;
94 69
        } elseif (is_string($pValue)) {
95 69
            $errorCodes = DataType::getErrorCodes();
96 69
            if (isset($errorCodes[$pValue])) {
97 3
                return DataType::TYPE_ERROR;
98
            }
99
        }
100
101 66
        return DataType::TYPE_STRING;
102
    }
103
}
104