1<?php
2
3/**
4 * PHPExcel_Cell_DataType
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
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   PHPExcel
23 * @package    PHPExcel_Cell
24 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26 * @version    ##VERSION##, ##DATE##
27 */
28class PHPExcel_Cell_DataType
29{
30    /* Data types */
31    const TYPE_STRING2  = 'str';
32    const TYPE_STRING   = 's';
33    const TYPE_FORMULA  = 'f';
34    const TYPE_NUMERIC  = 'n';
35    const TYPE_BOOL     = 'b';
36    const TYPE_NULL     = 'null';
37    const TYPE_INLINE   = 'inlineStr';
38    const TYPE_ERROR    = 'e';
39
40    /**
41     * List of error codes
42     *
43     * @var array
44     */
45    private static $errorCodes = array(
46        '#NULL!'  => 0,
47        '#DIV/0!' => 1,
48        '#VALUE!' => 2,
49        '#REF!'   => 3,
50        '#NAME?'  => 4,
51        '#NUM!'   => 5,
52        '#N/A'    => 6
53    );
54
55    /**
56     * Get list of error codes
57     *
58     * @return array
59     */
60    public static function getErrorCodes()
61    {
62        return self::$errorCodes;
63    }
64
65    /**
66     * DataType for value
67     *
68     * @deprecated  Replaced by PHPExcel_Cell_IValueBinder infrastructure, will be removed in version 1.8.0
69     * @param       mixed  $pValue
70     * @return      string
71     */
72    public static function dataTypeForValue($pValue = null)
73    {
74        return PHPExcel_Cell_DefaultValueBinder::dataTypeForValue($pValue);
75    }
76
77    /**
78     * Check a string that it satisfies Excel requirements
79     *
80     * @param  mixed  Value to sanitize to an Excel string
81     * @return mixed  Sanitized value
82     */
83    public static function checkString($pValue = null)
84    {
85        if ($pValue instanceof PHPExcel_RichText) {
86            // TODO: Sanitize Rich-Text string (max. character count is 32,767)
87            return $pValue;
88        }
89
90        // string must never be longer than 32,767 characters, truncate if necessary
91        $pValue = PHPExcel_Shared_String::Substring($pValue, 0, 32767);
92
93        // we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
94        $pValue = str_replace(array("\r\n", "\r"), "\n", $pValue);
95
96        return $pValue;
97    }
98
99    /**
100     * Check a value that it is a valid error code
101     *
102     * @param  mixed   Value to sanitize to an Excel error code
103     * @return string  Sanitized value
104     */
105    public static function checkErrorCode($pValue = null)
106    {
107        $pValue = (string) $pValue;
108
109        if (!array_key_exists($pValue, self::$errorCodes)) {
110            $pValue = '#NULL!';
111        }
112
113        return $pValue;
114    }
115}
116