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