<?php
namespace PhpOffice\PhpSpreadsheet\Calculation;
use DateTimeInterface;
< use PhpOffice\PhpSpreadsheet\Shared\Date;
< use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
< use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
> /**
class TextData
> * @deprecated 1.18.0
{
> */
< private static $invalidChars;
<
< private static function unicodeToOrd($character)
< {
< return unpack('V', iconv('UTF-8', 'UCS-4LE', $character))[1];
< }
<
/**
* CHARACTER.
*
> * @deprecated 1.18.0
* @param string $character Value
> * Use the character() method in the TextData\CharacterConvert class instead
*
> * @see TextData\CharacterConvert::character()
* @return string
> *
< * @return string
> * @return array|string
public static function CHARACTER($character)
{
< $character = Functions::flattenSingleValue($character);
<
< if (!is_numeric($character)) {
< return Functions::VALUE();
< }
< $character = (int) $character;
< if ($character < 1 || $character > 255) {
< return Functions::VALUE();
< }
<
< return iconv('UCS-4LE', 'UTF-8', pack('V', $character));
> return TextData\CharacterConvert::character($character);
}
/**
* TRIMNONPRINTABLE.
*
> * @deprecated 1.18.0
* @param mixed $stringValue Value to check
> * Use the nonPrintable() method in the TextData\Trim class instead
*
> * @see TextData\Trim::nonPrintable()
* @return string
> *
< * @return string
> * @return null|array|string
public static function TRIMNONPRINTABLE($stringValue = '')
{
< $stringValue = Functions::flattenSingleValue($stringValue);
<
< if (is_bool($stringValue)) {
< return ($stringValue) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< if (self::$invalidChars === null) {
< self::$invalidChars = range(chr(0), chr(31));
< }
<
< if (is_string($stringValue) || is_numeric($stringValue)) {
< return str_replace(self::$invalidChars, '', trim($stringValue, "\x00..\x1F"));
< }
<
< return null;
> return TextData\Trim::nonPrintable($stringValue);
}
/**
* TRIMSPACES.
*
> * @deprecated 1.18.0
* @param mixed $stringValue Value to check
> * Use the spaces() method in the TextData\Trim class instead
*
> * @see TextData\Trim::spaces()
* @return string
> *
< * @return string
> * @return array|string
public static function TRIMSPACES($stringValue = '')
{
< $stringValue = Functions::flattenSingleValue($stringValue);
< if (is_bool($stringValue)) {
< return ($stringValue) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< if (is_string($stringValue) || is_numeric($stringValue)) {
< return trim(preg_replace('/ +/', ' ', trim($stringValue, ' ')), ' ');
< }
<
< return null;
< }
<
< private static function convertBooleanValue($value)
< {
< if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
< return (int) $value;
< }
<
< return ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
> return TextData\Trim::spaces($stringValue);
}
/**
* ASCIICODE.
*
< * @param string $characters Value
> * @deprecated 1.18.0
> * Use the code() method in the TextData\CharacterConvert class instead
> * @see TextData\CharacterConvert::code()
> *
> * @param array|string $characters Value
*
< * @return int|string A string if arguments are invalid
> * @return array|int|string A string if arguments are invalid
*/
public static function ASCIICODE($characters)
{
< if (($characters === null) || ($characters === '')) {
< return Functions::VALUE();
< }
< $characters = Functions::flattenSingleValue($characters);
< if (is_bool($characters)) {
< $characters = self::convertBooleanValue($characters);
< }
<
< $character = $characters;
< if (mb_strlen($characters, 'UTF-8') > 1) {
< $character = mb_substr($characters, 0, 1, 'UTF-8');
< }
<
< return self::unicodeToOrd($character);
> return TextData\CharacterConvert::code($characters);
}
/**
* CONCATENATE.
*
> * @deprecated 1.18.0
* @return string
> * Use the CONCATENATE() method in the TextData\Concatenate class instead
*/
> * @see TextData\Concatenate::CONCATENATE()
public static function CONCATENATE(...$args)
> *
{
> * @param array $args
$returnValue = '';
> *
< $returnValue = '';
<
< // Loop through arguments
< $aArgs = Functions::flattenArray($args);
< foreach ($aArgs as $arg) {
< if (is_bool($arg)) {
< $arg = self::convertBooleanValue($arg);
< }
< $returnValue .= $arg;
< }
<
< return $returnValue;
> return TextData\Concatenate::CONCATENATE(...$args);
/**
* DOLLAR.
*
* This function converts a number to text using currency format, with the decimals rounded to the specified place.
* The format used is $#,##0.00_);($#,##0.00)..
*
> * @deprecated 1.18.0
* @param float $value The value to format
> * Use the DOLLAR() method in the TextData\Format class instead
* @param int $decimals The number of digits to display to the right of the decimal point.
> * @see TextData\Format::DOLLAR()
* If decimals is negative, number is rounded to the left of the decimal point.
> *
* If you omit decimals, it is assumed to be 2
*
< * @return string
> * @return array|string
*/
public static function DOLLAR($value = 0, $decimals = 2)
{
< $value = Functions::flattenSingleValue($value);
< $decimals = $decimals === null ? 0 : Functions::flattenSingleValue($decimals);
<
< // Validate parameters
< if (!is_numeric($value) || !is_numeric($decimals)) {
< return Functions::VALUE();
< }
< $decimals = floor($decimals);
<
< $mask = '$#,##0';
< if ($decimals > 0) {
< $mask .= '.' . str_repeat('0', $decimals);
< } else {
< $round = 10 ** abs($decimals);
< if ($value < 0) {
< $round = 0 - $round;
< }
< $value = MathTrig::MROUND($value, $round);
< }
< $mask = "$mask;($mask)";
<
< return NumberFormat::toFormattedString($value, $mask);
> return TextData\Format::DOLLAR($value, $decimals);
}
/**
< * SEARCHSENSITIVE.
> * FIND.
*
< * @param string $needle The string to look for
< * @param string $haystack The string in which to look
< * @param int $offset Offset within $haystack
> * @deprecated 1.18.0
> * Use the sensitive() method in the TextData\Search class instead
> * @see TextData\Search::sensitive()
*
< * @return string
> * @param array|string $needle The string to look for
> * @param array|string $haystack The string in which to look
> * @param array|int $offset Offset within $haystack
> *
> * @return array|int|string
*/
public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1)
{
< $needle = Functions::flattenSingleValue($needle);
< $haystack = Functions::flattenSingleValue($haystack);
< $offset = Functions::flattenSingleValue($offset);
<
< if (!is_bool($needle)) {
< if (is_bool($haystack)) {
< $haystack = ($haystack) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< if (($offset > 0) && (StringHelper::countCharacters($haystack) > $offset)) {
< if (StringHelper::countCharacters($needle) === 0) {
< return $offset;
< }
<
< $pos = mb_strpos($haystack, $needle, --$offset, 'UTF-8');
< if ($pos !== false) {
< return ++$pos;
< }
< }
< }
<
< return Functions::VALUE();
> return TextData\Search::sensitive($needle, $haystack, $offset);
}
/**
< * SEARCHINSENSITIVE.
> * SEARCH.
*
< * @param string $needle The string to look for
< * @param string $haystack The string in which to look
< * @param int $offset Offset within $haystack
> * @deprecated 1.18.0
> * Use the insensitive() method in the TextData\Search class instead
> * @see TextData\Search::insensitive()
*
< * @return string
> * @param array|string $needle The string to look for
> * @param array|string $haystack The string in which to look
> * @param array|int $offset Offset within $haystack
> *
> * @return array|int|string
*/
public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1)
{
< $needle = Functions::flattenSingleValue($needle);
< $haystack = Functions::flattenSingleValue($haystack);
< $offset = Functions::flattenSingleValue($offset);
<
< if (!is_bool($needle)) {
< if (is_bool($haystack)) {
< $haystack = ($haystack) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< if (($offset > 0) && (StringHelper::countCharacters($haystack) > $offset)) {
< if (StringHelper::countCharacters($needle) === 0) {
< return $offset;
< }
<
< $pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8');
< if ($pos !== false) {
< return ++$pos;
< }
< }
< }
<
< return Functions::VALUE();
> return TextData\Search::insensitive($needle, $haystack, $offset);
}
/**
* FIXEDFORMAT.
*
> * @deprecated 1.18.0
* @param mixed $value Value to check
> * Use the FIXEDFORMAT() method in the TextData\Format class instead
* @param int $decimals
> * @see TextData\Format::FIXEDFORMAT()
* @param bool $no_commas
> *
*
< * @return string
> * @return array|string
*/
public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false)
{
< $value = Functions::flattenSingleValue($value);
< $decimals = Functions::flattenSingleValue($decimals);
< $no_commas = Functions::flattenSingleValue($no_commas);
<
< // Validate parameters
< if (!is_numeric($value) || !is_numeric($decimals)) {
< return Functions::VALUE();
< }
< $decimals = (int) floor($decimals);
<
< $valueResult = round($value, $decimals);
< if ($decimals < 0) {
< $decimals = 0;
< }
< if (!$no_commas) {
< $valueResult = number_format(
< $valueResult,
< $decimals,
< StringHelper::getDecimalSeparator(),
< StringHelper::getThousandsSeparator()
< );
< }
<
< return (string) $valueResult;
> return TextData\Format::FIXEDFORMAT($value, $decimals, $no_commas);
}
/**
* LEFT.
*
< * @param string $value Value
< * @param int $chars Number of characters
> * @deprecated 1.18.0
> * Use the left() method in the TextData\Extract class instead
> * @see TextData\Extract::left()
*
< * @return string
> * @param array|string $value Value
> * @param array|int $chars Number of characters
> *
> * @return array|string
*/
public static function LEFT($value = '', $chars = 1)
{
< $value = Functions::flattenSingleValue($value);
< $chars = Functions::flattenSingleValue($chars);
<
< if ($chars < 0) {
< return Functions::VALUE();
< }
<
< if (is_bool($value)) {
< $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< return mb_substr($value, 0, $chars, 'UTF-8');
> return TextData\Extract::left($value, $chars);
}
/**
* MID.
*
< * @param string $value Value
< * @param int $start Start character
< * @param int $chars Number of characters
> * @deprecated 1.18.0
> * Use the mid() method in the TextData\Extract class instead
> * @see TextData\Extract::mid()
> *
> * @param array|string $value Value
> * @param array|int $start Start character
> * @param array|int $chars Number of characters
*
< * @return string
> * @return array|string
*/
public static function MID($value = '', $start = 1, $chars = null)
{
< $value = Functions::flattenSingleValue($value);
< $start = Functions::flattenSingleValue($start);
< $chars = Functions::flattenSingleValue($chars);
<
< if (($start < 1) || ($chars < 0)) {
< return Functions::VALUE();
< }
<
< if (is_bool($value)) {
< $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< if (empty($chars)) {
< return '';
< }
<
< return mb_substr($value, --$start, $chars, 'UTF-8');
> return TextData\Extract::mid($value, $start, $chars);
}
/**
* RIGHT.
*
< * @param string $value Value
< * @param int $chars Number of characters
> * @deprecated 1.18.0
> * Use the right() method in the TextData\Extract class instead
> * @see TextData\Extract::right()
*
< * @return string
> * @param array|string $value Value
> * @param array|int $chars Number of characters
> *
> * @return array|string
*/
public static function RIGHT($value = '', $chars = 1)
{
< $value = Functions::flattenSingleValue($value);
< $chars = Functions::flattenSingleValue($chars);
<
< if ($chars < 0) {
< return Functions::VALUE();
< }
<
< if (is_bool($value)) {
< $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
> return TextData\Extract::right($value, $chars);
}
/**
* STRINGLENGTH.
*
> * @deprecated 1.18.0
* @param string $value Value
> * Use the length() method in the TextData\Text class instead
*
> * @see TextData\Text::length()
* @return int
> *
< * @return int
> * @return array|int
public static function STRINGLENGTH($value = '')
{
< $value = Functions::flattenSingleValue($value);
<
< if (is_bool($value)) {
< $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< return mb_strlen($value, 'UTF-8');
> return TextData\Text::length($value);
}
/**
* LOWERCASE.
*
< * Converts a string value to upper case.
> * Converts a string value to lower case.
*
< * @param string $mixedCaseString
> * @deprecated 1.18.0
> * Use the lower() method in the TextData\CaseConvert class instead
> * @see TextData\CaseConvert::lower()
*
< * @return string
> * @param array|string $mixedCaseString
> *
> * @return array|string
*/
public static function LOWERCASE($mixedCaseString)
{
< $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
<
< if (is_bool($mixedCaseString)) {
< $mixedCaseString = ($mixedCaseString) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< return StringHelper::strToLower($mixedCaseString);
> return TextData\CaseConvert::lower($mixedCaseString);
}
/**
* UPPERCASE.
*
* Converts a string value to upper case.
*
> * @deprecated 1.18.0
* @param string $mixedCaseString
> * Use the upper() method in the TextData\CaseConvert class instead
*
> * @see TextData\CaseConvert::upper()
* @return string
> *
< * @return string
> * @return array|string
public static function UPPERCASE($mixedCaseString)
{
< $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
<
< if (is_bool($mixedCaseString)) {
< $mixedCaseString = ($mixedCaseString) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< return StringHelper::strToUpper($mixedCaseString);
> return TextData\CaseConvert::upper($mixedCaseString);
}
/**
* PROPERCASE.
*
< * Converts a string value to upper case.
> * Converts a string value to proper/title case.
*
< * @param string $mixedCaseString
> * @deprecated 1.18.0
> * Use the proper() method in the TextData\CaseConvert class instead
> * @see TextData\CaseConvert::proper()
*
< * @return string
> * @param array|string $mixedCaseString
> *
> * @return array|string
*/
public static function PROPERCASE($mixedCaseString)
{
< $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
<
< if (is_bool($mixedCaseString)) {
< $mixedCaseString = ($mixedCaseString) ? Calculation::getTRUE() : Calculation::getFALSE();
< }
<
< return StringHelper::strToTitle($mixedCaseString);
> return TextData\CaseConvert::proper($mixedCaseString);
}
/**
* REPLACE.
*
> * @deprecated 1.18.0
* @param string $oldText String to modify
> * Use the replace() method in the TextData\Replace class instead
* @param int $start Start character
> * @see TextData\Replace::replace()
* @param int $chars Number of characters
> *
* @param string $newText String to replace in defined position
*
< * @return string
> * @return array|string
*/
public static function REPLACE($oldText, $start, $chars, $newText)
{
< $oldText = Functions::flattenSingleValue($oldText);
< $start = Functions::flattenSingleValue($start);
< $chars = Functions::flattenSingleValue($chars);
< $newText = Functions::flattenSingleValue($newText);
<
< $left = self::LEFT($oldText, $start - 1);
< $right = self::RIGHT($oldText, self::STRINGLENGTH($oldText) - ($start + $chars) + 1);
<
< return $left . $newText . $right;
> return TextData\Replace::replace($oldText, $start, $chars, $newText);
}
/**
* SUBSTITUTE.
*
> * @deprecated 1.18.0
* @param string $text Value
> * Use the substitute() method in the TextData\Replace class instead
* @param string $fromText From Value
> * @see TextData\Replace::substitute()
* @param string $toText To Value
> *
* @param int $instance Instance Number
*
< * @return string
> * @return array|string
*/
public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0)
{
< $text = Functions::flattenSingleValue($text);
< $fromText = Functions::flattenSingleValue($fromText);
< $toText = Functions::flattenSingleValue($toText);
< $instance = floor(Functions::flattenSingleValue($instance));
<
< if ($instance == 0) {
< return str_replace($fromText, $toText, $text);
< }
<
< $pos = -1;
< while ($instance > 0) {
< $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
< if ($pos === false) {
< break;
< }
< --$instance;
< }
<
< if ($pos !== false) {
< return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
< }
<
< return $text;
> return TextData\Replace::substitute($text, $fromText, $toText, $instance);
}
/**
* RETURNSTRING.
*
> * @deprecated 1.18.0
* @param mixed $testValue Value to check
> * Use the test() method in the TextData\Text class instead
*
> * @see TextData\Text::test()
* @return null|string
> *
< * @return null|string
> * @return null|array|string
public static function RETURNSTRING($testValue = '')
{
< $testValue = Functions::flattenSingleValue($testValue);
<
< if (is_string($testValue)) {
< return $testValue;
< }
<
< return null;
> return TextData\Text::test($testValue);
}
/**
* TEXTFORMAT.
*
> * @deprecated 1.18.0
* @param mixed $value Value to check
> * Use the TEXTFORMAT() method in the TextData\Format class instead
* @param string $format Format mask to use
> * @see TextData\Format::TEXTFORMAT()
*
> *
< * @return string
> * @return array|string
*/
public static function TEXTFORMAT($value, $format)
{
< $value = Functions::flattenSingleValue($value);
< $format = Functions::flattenSingleValue($format);
<
< if ((is_string($value)) && (!is_numeric($value)) && Date::isDateTimeFormatCode($format)) {
< $value = DateTime::DATEVALUE($value);
< }
<
< return (string) NumberFormat::toFormattedString($value, $format);
> return TextData\Format::TEXTFORMAT($value, $format);
}
/**
* VALUE.
*
> * @deprecated 1.18.0
* @param mixed $value Value to check
> * Use the VALUE() method in the TextData\Format class instead
*
> * @see TextData\Format::VALUE()
* @return DateTimeInterface|float|int|string A string if arguments are invalid
> *
< * @return DateTimeInterface|float|int|string A string if arguments are invalid
> * @return array|DateTimeInterface|float|int|string A string if arguments are invalid
public static function VALUE($value = '')
{
< $value = Functions::flattenSingleValue($value);
<
< if (!is_numeric($value)) {
< $numberValue = str_replace(
< StringHelper::getThousandsSeparator(),
< '',
< trim($value, " \t\n\r\0\x0B" . StringHelper::getCurrencyCode())
< );
< if (is_numeric($numberValue)) {
< return (float) $numberValue;
< }
<
< $dateSetting = Functions::getReturnDateType();
< Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
<
< if (strpos($value, ':') !== false) {
< $timeValue = DateTime::TIMEVALUE($value);
< if ($timeValue !== Functions::VALUE()) {
< Functions::setReturnDateType($dateSetting);
<
< return $timeValue;
< }
< }
< $dateValue = DateTime::DATEVALUE($value);
< if ($dateValue !== Functions::VALUE()) {
< Functions::setReturnDateType($dateSetting);
<
< return $dateValue;
< }
< Functions::setReturnDateType($dateSetting);
<
< return Functions::VALUE();
< }
<
< return (float) $value;
> return TextData\Format::VALUE($value);
}
/**
* NUMBERVALUE.
*
> * @deprecated 1.18.0
* @param mixed $value Value to check
> * Use the NUMBERVALUE() method in the TextData\Format class instead
* @param string $decimalSeparator decimal separator, defaults to locale defined value
> * @see TextData\Format::NUMBERVALUE()
* @param string $groupSeparator group/thosands separator, defaults to locale defined value
> *
*
< * @return float|string
> * @return array|float|string
*/
public static function NUMBERVALUE($value = '', $decimalSeparator = null, $groupSeparator = null)
{
< $value = Functions::flattenSingleValue($value);
< $decimalSeparator = Functions::flattenSingleValue($decimalSeparator);
< $groupSeparator = Functions::flattenSingleValue($groupSeparator);
<
< if (!is_numeric($value)) {
< $decimalSeparator = empty($decimalSeparator) ? StringHelper::getDecimalSeparator() : $decimalSeparator;
< $groupSeparator = empty($groupSeparator) ? StringHelper::getThousandsSeparator() : $groupSeparator;
<
< $decimalPositions = preg_match_all('/' . preg_quote($decimalSeparator) . '/', $value, $matches, PREG_OFFSET_CAPTURE);
< if ($decimalPositions > 1) {
< return Functions::VALUE();
< }
< $decimalOffset = array_pop($matches[0])[1];
< if (strpos($value, $groupSeparator, $decimalOffset) !== false) {
< return Functions::VALUE();
< }
<
< $value = str_replace([$groupSeparator, $decimalSeparator], ['', '.'], $value);
<
< // Handle the special case of trailing % signs
< $percentageString = rtrim($value, '%');
< if (!is_numeric($percentageString)) {
< return Functions::VALUE();
< }
<
< $percentageAdjustment = strlen($value) - strlen($percentageString);
< if ($percentageAdjustment) {
< $value = (float) $percentageString;
< $value /= 10 ** ($percentageAdjustment * 2);
< }
< }
<
< return (float) $value;
> return TextData\Format::NUMBERVALUE($value, $decimalSeparator, $groupSeparator);
}
/**
* Compares two text strings and returns TRUE if they are exactly the same, FALSE otherwise.
* EXACT is case-sensitive but ignores formatting differences.
* Use EXACT to test text being entered into a document.
*
< * @param $value1
< * @param $value2
> * @deprecated 1.18.0
> * Use the exact() method in the TextData\Text class instead
> * @see TextData\Text::exact()
> *
> * @param mixed $value1
> * @param mixed $value2
*
< * @return bool
> * @return array|bool
*/
public static function EXACT($value1, $value2)
{
< $value1 = Functions::flattenSingleValue($value1);
< $value2 = Functions::flattenSingleValue($value2);
<
< return (string) $value2 === (string) $value1;
> return TextData\Text::exact($value1, $value2);
}
/**
* TEXTJOIN.
*
> * @deprecated 1.18.0
* @param mixed $delimiter
> * Use the TEXTJOIN() method in the TextData\Concatenate class instead
* @param mixed $ignoreEmpty
> * @see TextData\Concatenate::TEXTJOIN()
* @param mixed $args
> *
*
< * @return string
> * @return array|string
*/
public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args)
{
< // Loop through arguments
< $aArgs = Functions::flattenArray($args);
< foreach ($aArgs as $key => &$arg) {
< if ($ignoreEmpty && trim($arg) == '') {
< unset($aArgs[$key]);
< } elseif (is_bool($arg)) {
< $arg = self::convertBooleanValue($arg);
< }
> return TextData\Concatenate::TEXTJOIN($delimiter, $ignoreEmpty, ...$args);
}
< return implode($delimiter, $aArgs);
> /**
> * REPT.
> *
> * Returns the result of builtin function repeat after validating args.
> *
> * @deprecated 1.18.0
> * Use the builtinREPT() method in the TextData\Concatenate class instead
> * @see TextData\Concatenate::builtinREPT()
> *
> * @param array|string $str Should be numeric
> * @param mixed $number Should be int
> *
> * @return array|string
> */
> public static function builtinREPT($str, $number)
> {
> return TextData\Concatenate::builtinREPT($str, $number);
}
}