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
use PhpOffice\PhpSpreadsheet\Cell\DataType;
10
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
11
 
12
class Replace
13
{
14
    use ArrayEnabled;
15
 
16
    /**
17
     * REPLACE.
18
     *
19
     * @param mixed $oldText The text string value to modify
20
     *                         Or can be an array of values
21
     * @param mixed $start Integer offset for start character of the replacement
22
     *                         Or can be an array of values
23
     * @param mixed $chars Integer number of characters to replace from the start offset
24
     *                         Or can be an array of values
25
     * @param mixed $newText String to replace in the defined position
26
     *                         Or can be an array of values
27
     *
28
     * @return array|string If an array of values is passed for either of the arguments, then the returned result
29
     *            will also be an array with matching dimensions
30
     */
31
    public static function replace(mixed $oldText, mixed $start, mixed $chars, mixed $newText): array|string
32
    {
33
        if (is_array($oldText) || is_array($start) || is_array($chars) || is_array($newText)) {
34
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $oldText, $start, $chars, $newText);
35
        }
36
 
37
        try {
38
            $start = Helpers::extractInt($start, 1, 0, true);
39
            $chars = Helpers::extractInt($chars, 0, 0, true);
40
            $oldText = Helpers::extractString($oldText, true);
41
            $newText = Helpers::extractString($newText, true);
42
            $left = StringHelper::substring($oldText, 0, $start - 1);
43
 
44
            $right = StringHelper::substring($oldText, $start + $chars - 1, null);
45
        } catch (CalcExp $e) {
46
            return $e->getMessage();
47
        }
48
        $returnValue = $left . $newText . $right;
49
        if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
50
            $returnValue = ExcelError::VALUE();
51
        }
52
 
53
        return $returnValue;
54
    }
55
 
56
    /**
57
     * SUBSTITUTE.
58
     *
59
     * @param mixed $text The text string value to modify
60
     *                         Or can be an array of values
61
     * @param mixed $fromText The string value that we want to replace in $text
62
     *                         Or can be an array of values
63
     * @param mixed $toText The string value that we want to replace with in $text
64
     *                         Or can be an array of values
65
     * @param mixed $instance Integer instance Number for the occurrence of frmText to change
66
     *                         Or can be an array of values
67
     *
68
     * @return array|string If an array of values is passed for either of the arguments, then the returned result
69
     *            will also be an array with matching dimensions
70
     */
71
    public static function substitute(mixed $text = '', mixed $fromText = '', mixed $toText = '', mixed $instance = null): array|string
72
    {
73
        if (is_array($text) || is_array($fromText) || is_array($toText) || is_array($instance)) {
74
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $text, $fromText, $toText, $instance);
75
        }
76
 
77
        try {
78
            $text = Helpers::extractString($text, true);
79
            $fromText = Helpers::extractString($fromText, true);
80
            $toText = Helpers::extractString($toText, true);
81
            if ($instance === null) {
82
                $returnValue = str_replace($fromText, $toText, $text);
83
            } else {
84
                if (is_bool($instance)) {
85
                    if ($instance === false || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE) {
86
                        return ExcelError::Value();
87
                    }
88
                    $instance = 1;
89
                }
90
                $instance = Helpers::extractInt($instance, 1, 0, true);
91
                $returnValue = self::executeSubstitution($text, $fromText, $toText, $instance);
92
            }
93
        } catch (CalcExp $e) {
94
            return $e->getMessage();
95
        }
96
        if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
97
            $returnValue = ExcelError::VALUE();
98
        }
99
 
100
        return $returnValue;
101
    }
102
 
103
    private static function executeSubstitution(string $text, string $fromText, string $toText, int $instance): string
104
    {
105
        $pos = -1;
106
        while ($instance > 0) {
107
            $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
108
            if ($pos === false) {
109
                return $text;
110
            }
111
            --$instance;
112
        }
113
 
114
        return Functions::scalar(self::REPLACE($text, ++$pos, StringHelper::countCharacters($fromText), $toText));
115
    }
116
}