Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
7
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
 
10
class CharacterConvert
11
{
12
    use ArrayEnabled;
13
 
14
    /**
15
     * CHAR.
16
     *
17
     * @param mixed $character Integer Value to convert to its character representation
18
     *                              Or can be an array of values
19
     *
20
     * @return array|string The character string
21
     *         If an array of values is passed as the argument, then the returned result will also be an array
22
     *            with the same dimensions
23
     */
24
    public static function character(mixed $character): array|string
25
    {
26
        if (is_array($character)) {
27
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $character);
28
        }
29
 
30
        try {
31
            $character = Helpers::validateInt($character, true);
32
        } catch (CalcExp $e) {
33
            return $e->getMessage();
34
        }
35
 
36
        $min = Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE ? 0 : 1;
37
        if ($character < $min || $character > 255) {
38
            return ExcelError::VALUE();
39
        }
40
        $result = iconv('UCS-4LE', 'UTF-8', pack('V', $character));
41
 
42
        return ($result === false) ? '' : $result;
43
    }
44
 
45
    /**
46
     * CODE.
47
     *
48
     * @param mixed $characters String character to convert to its ASCII value
49
     *                              Or can be an array of values
50
     *
51
     * @return array|int|string A string if arguments are invalid
52
     *         If an array of values is passed as the argument, then the returned result will also be an array
53
     *            with the same dimensions
54
     */
55
    public static function code(mixed $characters): array|string|int
56
    {
57
        if (is_array($characters)) {
58
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $characters);
59
        }
60
 
61
        try {
62
            $characters = Helpers::extractString($characters, true);
63
        } catch (CalcExp $e) {
64
            return $e->getMessage();
65
        }
66
 
67
        if ($characters === '') {
68
            return ExcelError::VALUE();
69
        }
70
 
71
        $character = $characters;
72
        if (mb_strlen($characters, 'UTF-8') > 1) {
73
            $character = mb_substr($characters, 0, 1, 'UTF-8');
74
        }
75
 
76
        return self::unicodeToOrd($character);
77
    }
78
 
79
    private static function unicodeToOrd(string $character): int
80
    {
81
        $retVal = 0;
82
        $iconv = iconv('UTF-8', 'UCS-4LE', $character);
83
        if ($iconv !== false) {
84
            $result = unpack('V', $iconv);
85
            if (is_array($result) && isset($result[1])) {
86
                $retVal = $result[1];
87
            }
88
        }
89
 
90
        return $retVal;
91
    }
92
}