ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DataValidator.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
13 {
21  public function isValid(Cell $cell)
22  {
23  if (!$cell->hasDataValidation()) {
24  return true;
25  }
26 
27  $cellValue = $cell->getValue();
28  $dataValidation = $cell->getDataValidation();
29 
30  if (!$dataValidation->getAllowBlank() && ($cellValue === null || $cellValue === '')) {
31  return false;
32  }
33 
34  // TODO: write check on all cases
35  switch ($dataValidation->getType()) {
37  return $this->isValueInList($cell);
38  }
39 
40  return false;
41  }
42 
50  private function isValueInList(Cell $cell)
51  {
52  $cellValue = $cell->getValue();
53  $dataValidation = $cell->getDataValidation();
54 
55  $formula1 = $dataValidation->getFormula1();
56  if (!empty($formula1)) {
57  // inline values list
58  if ($formula1[0] === '"') {
59  return in_array(strtolower($cellValue), explode(',', strtolower(trim($formula1, '"'))), true);
60  } elseif (strpos($formula1, ':') > 0) {
61  // values list cells
62  $matchFormula = '=MATCH(' . $cell->getCoordinate() . ', ' . $formula1 . ', 0)';
63  $calculation = Calculation::getInstance($cell->getWorksheet()->getParent());
64 
65  try {
66  $result = $calculation->calculateFormula($matchFormula, $cell->getCoordinate(), $cell);
67 
68  return $result !== Functions::NA();
69  } catch (Exception $ex) {
70  return false;
71  }
72  }
73  }
74 
75  return true;
76  }
77 }
getCoordinate()
Get cell coordinate.
Definition: Cell.php:140
$result
Validate a cell value according to its validation rules.
getValue()
Get cell value.
Definition: Cell.php:150
hasDataValidation()
Does this cell contain Data validation rules?
Definition: Cell.php:365
isValid(Cell $cell)
Does this cell contain valid value?
static getInstance(?Spreadsheet $spreadsheet=null)
Get an instance of this class.
isValueInList(Cell $cell)
Does this cell contain valid value, based on list?
getDataValidation()
Get Data validation rules.
Definition: Cell.php:379
getWorksheet()
Get parent worksheet.
Definition: Cell.php:479