Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   9  use PhpOffice\PhpSpreadsheet\Cell\DataType;
  10  use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  11  
  12  class Concatenate
  13  {
  14      use ArrayEnabled;
  15  
  16      /**
  17       * CONCATENATE.
  18       *
  19       * @param array $args
  20       */
  21      public static function CONCATENATE(...$args): string
  22      {
  23          $returnValue = '';
  24  
  25          // Loop through arguments
  26          $aArgs = Functions::flattenArray($args);
  27  
  28          foreach ($aArgs as $arg) {
  29              $value = Helpers::extractString($arg);
  30              if (ErrorValue::isError($value)) {
  31                  $returnValue = $value;
  32  
  33                  break;
  34              }
  35              $returnValue .= Helpers::extractString($arg);
  36              if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
  37                  $returnValue = ExcelError::CALC();
  38  
  39                  break;
  40              }
  41          }
  42  
  43          return $returnValue;
  44      }
  45  
  46      /**
  47       * TEXTJOIN.
  48       *
  49       * @param mixed $delimiter The delimter to use between the joined arguments
  50       *                         Or can be an array of values
  51       * @param mixed $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped
  52       *                         Or can be an array of values
  53       * @param mixed $args The values to join
  54       *
  55       * @return array|string The joined string
  56       *         If an array of values is passed for the $delimiter or $ignoreEmpty arguments, then the returned result
  57       *            will also be an array with matching dimensions
  58       */
  59      public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args)
  60      {
  61          if (is_array($delimiter) || is_array($ignoreEmpty)) {
  62              return self::evaluateArrayArgumentsSubset(
  63                  [self::class, __FUNCTION__],
  64                  2,
  65                  $delimiter,
  66                  $ignoreEmpty,
  67                  ...$args
  68              );
  69          }
  70  
  71          // Loop through arguments
  72          $aArgs = Functions::flattenArray($args);
  73          $returnValue = '';
  74          foreach ($aArgs as $key => &$arg) {
  75              $value = Helpers::extractString($arg);
  76              if (ErrorValue::isError($value)) {
  77                  $returnValue = $value;
  78  
  79                  break;
  80              }
  81              if ($ignoreEmpty === true && is_string($arg) && trim($arg) === '') {
  82                  unset($aArgs[$key]);
  83              } elseif (is_bool($arg)) {
  84                  $arg = Helpers::convertBooleanValue($arg);
  85              }
  86          }
  87  
  88          $returnValue = ($returnValue !== '') ? $returnValue : implode($delimiter, $aArgs);
  89          if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
  90              $returnValue = ExcelError::CALC();
  91          }
  92  
  93          return $returnValue;
  94      }
  95  
  96      /**
  97       * REPT.
  98       *
  99       * Returns the result of builtin function round after validating args.
 100       *
 101       * @param mixed $stringValue The value to repeat
 102       *                         Or can be an array of values
 103       * @param mixed $repeatCount The number of times the string value should be repeated
 104       *                         Or can be an array of values
 105       *
 106       * @return array|string The repeated string
 107       *         If an array of values is passed for the $stringValue or $repeatCount arguments, then the returned result
 108       *            will also be an array with matching dimensions
 109       */
 110      public static function builtinREPT($stringValue, $repeatCount)
 111      {
 112          if (is_array($stringValue) || is_array($repeatCount)) {
 113              return self::evaluateArrayArguments([self::class, __FUNCTION__], $stringValue, $repeatCount);
 114          }
 115  
 116          $stringValue = Helpers::extractString($stringValue);
 117  
 118          if (!is_numeric($repeatCount) || $repeatCount < 0) {
 119              $returnValue = ExcelError::VALUE();
 120          } elseif (ErrorValue::isError($stringValue)) {
 121              $returnValue = $stringValue;
 122          } else {
 123              $returnValue = str_repeat($stringValue, (int) $repeatCount);
 124              if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
 125                  $returnValue = ExcelError::VALUE(); // note VALUE not CALC
 126              }
 127          }
 128  
 129          return $returnValue;
 130      }
 131  }