You are here

private function PHPExcel_Calculation::_convertMatrixReferences in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation.php \PHPExcel_Calculation::_convertMatrixReferences()
1 call to PHPExcel_Calculation::_convertMatrixReferences()
PHPExcel_Calculation::_parseFormula in vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation.php

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation.php, line 2652

Class

PHPExcel_Calculation
PHPExcel_Calculation (Multiton)

Code

private function _convertMatrixReferences($formula) {
  static $matrixReplaceFrom = array(
    '{',
    ';',
    '}',
  );
  static $matrixReplaceTo = array(
    'MKMATRIX(MKMATRIX(',
    '),MKMATRIX(',
    '))',
  );

  //	Convert any Excel matrix references to the MKMATRIX() function
  if (strpos($formula, '{') !== FALSE) {

    //	If there is the possibility of braces within a quoted string, then we don't treat those as matrix indicators
    if (strpos($formula, '"') !== FALSE) {

      //	So instead we skip replacing in any quoted strings by only replacing in every other array element after we've exploded
      //		the formula
      $temp = explode('"', $formula);

      //	Open and Closed counts used for trapping mismatched braces in the formula
      $openCount = $closeCount = 0;
      $i = FALSE;
      foreach ($temp as &$value) {

        //	Only count/replace in alternating array entries
        if ($i = !$i) {
          $openCount += substr_count($value, '{');
          $closeCount += substr_count($value, '}');
          $value = str_replace($matrixReplaceFrom, $matrixReplaceTo, $value);
        }
      }
      unset($value);

      //	Then rebuild the formula string
      $formula = implode('"', $temp);
    }
    else {

      //	If there's no quoted strings, then we do a simple count/replace
      $openCount = substr_count($formula, '{');
      $closeCount = substr_count($formula, '}');
      $formula = str_replace($matrixReplaceFrom, $matrixReplaceTo, $formula);
    }

    //	Trap for mismatched braces and trigger an appropriate error
    if ($openCount < $closeCount) {
      if ($openCount > 0) {
        return $this
          ->_raiseFormulaError("Formula Error: Mismatched matrix braces '}'");
      }
      else {
        return $this
          ->_raiseFormulaError("Formula Error: Unexpected '}' encountered");
      }
    }
    elseif ($openCount > $closeCount) {
      if ($closeCount > 0) {
        return $this
          ->_raiseFormulaError("Formula Error: Mismatched matrix braces '{'");
      }
      else {
        return $this
          ->_raiseFormulaError("Formula Error: Unexpected '{' encountered");
      }
    }
  }
  return $formula;
}