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 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
   6  use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
   7  use PhpOffice\PhpSpreadsheet\Cell\CellRange;
   8  
   9  class Validations
  10  {
  11      /**
  12       * Validate a cell address.
  13       *
  14       * @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
  15       *               or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
  16       */
  17      public static function validateCellAddress($cellAddress): string
  18      {
  19          if (is_string($cellAddress)) {
  20              [$worksheet, $address] = Worksheet::extractSheetTitle($cellAddress, true);
  21  //            if (!empty($worksheet) && $worksheet !== $this->getTitle()) {
  22  //                throw new Exception('Reference is not for this worksheet');
  23  //            }
  24  
  25              return empty($worksheet) ? strtoupper($address) : $worksheet . '!' . strtoupper($address);
  26          }
  27  
  28          if (is_array($cellAddress)) {
  29              $cellAddress = CellAddress::fromColumnRowArray($cellAddress);
  30          }
  31  
  32          return (string) $cellAddress;
  33      }
  34  
  35      /**
  36       * Validate a cell address or cell range.
  37       *
  38       * @param AddressRange|array<int>|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
  39       *               or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
  40       *               or as a CellAddress or AddressRange object.
  41       */
  42      public static function validateCellOrCellRange($cellRange): string
  43      {
  44          if (is_string($cellRange) || is_numeric($cellRange)) {
  45              // Convert a single column reference like 'A' to 'A:A',
  46              //    a single row reference like '1' to '1:1'
  47              $cellRange = (string) preg_replace('/^([A-Z]+|\d+)$/', '$1}:$1}', (string) $cellRange);
  48          } elseif (is_object($cellRange) && $cellRange instanceof CellAddress) {
  49              $cellRange = new CellRange($cellRange, $cellRange);
  50          }
  51  
  52          return self::validateCellRange($cellRange);
  53      }
  54  
  55      /**
  56       * Validate a cell range.
  57       *
  58       * @param AddressRange|array<int>|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
  59       *               or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
  60       *               or as an AddressRange object.
  61       */
  62      public static function validateCellRange($cellRange): string
  63      {
  64          if (is_string($cellRange)) {
  65              [$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);
  66  
  67              // Convert Column ranges like 'A:C' to 'A1:C1048576'
  68              //      or Row ranges like '1:3' to 'A1:XFD3'
  69              $addressRange = (string) preg_replace(
  70                  ['/^([A-Z]+):([A-Z]+)$/i', '/^(\\d+):(\\d+)$/'],
  71                  ['$1}1:$2}1048576', 'A$1}:XFD$2}'],
  72                  $addressRange
  73              );
  74  
  75              return empty($worksheet) ? strtoupper($addressRange) : $worksheet . '!' . strtoupper($addressRange);
  76          }
  77  
  78          if (is_array($cellRange)) {
  79              [$from, $to] = array_chunk($cellRange, 2);
  80              $cellRange = new CellRange(CellAddress::fromColumnRowArray($from), CellAddress::fromColumnRowArray($to));
  81          }
  82  
  83          return (string) $cellRange;
  84      }
  85  
  86      public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string
  87      {
  88          // Uppercase coordinate
  89          $coordinate = strtoupper($coordinate);
  90          // Eliminate leading equal sign
  91          $testCoordinate = (string) preg_replace('/^=/', '', $coordinate);
  92          $defined = $worksheet->getParent()->getDefinedName($testCoordinate, $worksheet);
  93          if ($defined !== null) {
  94              if ($defined->getWorksheet() === $worksheet && !$defined->isFormula()) {
  95                  $coordinate = (string) preg_replace('/^=/', '', $defined->getValue());
  96              }
  97          }
  98  
  99          return $coordinate;
 100      }
 101  }