Source of file ValidationLogicCriterion.php
Size: 3,002 Bytes - Last Modified: 2021-12-23T10:20:27+00:00
/var/www/docs.ssmods.com/process/src/code/ValidationLogicCriterion.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | <?php use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injectable; /** * @package ZenValidator * @license BSD License http://www.silverstripe.org/bsd-license * @author <shea@silverstripe.com.au> * * Credit to Uncle Cheese for the recipe */ class ValidationLogicCriterion { use Injectable; /** * The name of the form field that is controlling the Validation * * @var string */ protected $master = null; /** * The comparison function to use, e.g. "EqualTo" * @var string */ protected $operator = null; /** * The value to compare to * * @var mixed */ protected $value = null; /** * The parent {@link ValidationLogicCriteria} * @var ValidationLogicCriteria */ protected $set = null; /** * Constructor * * @param string $master The name of the master field * @param string $operator The name of the comparison function * @param string $value The value to compare to * @param ValidationLogicCriteria $set The parent criteria set */ public function __construct($master, $operator, $value, ValidationLogicCriteria $set) { $this->master = $master; $this->operator = $operator; $this->value = $value; $this->set = $set; } /** * Accessor for the master field * * @return string */ public function getMaster() { return $this->master; } public function phpOperator() { $operators = Config::inst()->get('ValidationLogicCriteria', 'comparisons'); if (isset($operators[$this->operator])) { return $operators[$this->operator]; } else { return $operators['is' . $this->operator]; } } /** * Returns a string of php code to be evaluated * * @return string **/ public function toPHP() { $value1 = '$fields->dataFieldByName(\'' . $this->master . '\')->dataValue()'; $value2 = $this->value; if ($operator = $this->phpOperator()) { return $value1 . " {$operator} \"$value2\""; } switch ($this->operator) { case 'contains': return 'strpos(' . $value1 . '' . ", \"$value2\") !== false"; case 'Checked': return "$value1 == \"1\""; case 'Empty': return $value1 . '==""'; case 'hasCheckedOption': return 'strpos(' . $value1 . '' . ", \"$value2\") !== false"; case 'hasCheckedAtLeast': return 'substr_count(' . $value1 . ',",") >= ' . $value2; case 'hasCheckedLessThan': return 'substr_count(' . $value1 . ',",") <= ' . $value2; default: throw new Exception("ValidationLogicCriteria: php operator \"$this->operator\" not configured."); } } } |