Source of file ValidationLogicCriteria.php
Size: 6,934 Bytes - Last Modified: 2021-12-23T10:20:27+00:00
/var/www/docs.ssmods.com/process/src/code/ValidationLogicCriteria.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 | <?php use SilverStripe\Forms\FormField; use SilverStripe\Core\Config\Configurable; 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 ValidationLogicCriteria { use Configurable; use Injectable; /** * The name of the form field that depends on the criteria * * @var string */ protected $master = null; /** * The form field that responds to the state of {@link $master} * * @var FormField */ protected $slave = null; /** * A parent {@link ValidationLogicCriteria}, for grouping * * @var ValidationLogicCriteria */ protected $parent = null; /** * A list of {@link ValidationLogicCriterion} objects * * @var array */ protected $criteria = array(); /** * Either "and" or "or", determines disjunctive or conjunctive logic for the whole criteria set * @var string */ protected $logicalOperator = null; /** * @var array */ private static $comparisons; /** * @param FormField $slave The form field that responds to changes of another form field * @param string $master The name of the form field to respond to * @param ValidationLogicCriteria $parent The parent {@link ValidationLogicCriteria} */ public function __construct(FormField $slave, $master, $parent = null) { $this->slave = $slave; $this->master = $master; $this->parent = $parent; return $this; } /** * Wildcard method for applying all the possible conditions * * @link https://github.com/unclecheese/silverstripe-display-logic/blob/c5f807dc24765438b427814350cc884ca70498b0/src/Criteria.php#L100 * @param string $method The method name * @param array $args The arguments * @return ValidationLogicCriteria */ public function __call($method, $args) { if (in_array($method, array_keys($this->config()->comparisons))) { $val = isset($args[0]) ? $args[0] : null; if (substr($method, 0, 2) == "is") { $operator = substr($method, 2); } else { $operator = $method; // $operator = ucwords($method); } $this->addCriterion(ValidationLogicCriterion::create($this->master, $operator, $val, $this)); return $this; } if (method_exists($this, $method)) { return $this->$method(...$args); } if ($this->parent && method_exists($this->parent, $method)) { return $this->parent->$method(...$args); } } /** * Adds a {@link ValidationLogicCriterion} for a range of values * * @param int $min The minimum value * @param int $max The maxiumum value * @return ValidationLogicCriteria */ public function isBetween($min, $max) { $this->addCriterion(ValidationLogicCriterion::create($this->master, "Between", "{$min}-{$max}", $this)); return $this; } /** * Adds a new criterion, and makes this set use conjuctive logic * * @param string $master The master form field * @return ValidationLogicCriteria */ public function andIf($master = null) { if ($this->logicalOperator == "or") { throw new Exception("ValidationLogicCriteria: Cannot declare a logical operator more than once. (Specified andIf() after calling orIf()). Use a nested ValidationLogicCriteriaSet to combine conjunctive and disjuctive logic."); } if ($master) { $this->master = $master; } $this->logicalOperator = "and"; return $this; } /** * Adds a new criterion, and makes this set use disjunctive logic * * @param string $master The master form field * @return ValidationLogicCriteria */ public function orIf($master = null) { if ($this->logicalOperator == "and") { throw new Exception("ValidationLogicCriteria: Cannot declare a logical operator more than once. (Specified orIf() after calling andIf()). Use a nested ValidationLogicCriteriaSet to combine conjunctive and disjuctive logic."); } if ($master) { $this->master = $master; } $this->logicalOperator = "or"; return $this; } /** * Adds a new criterion * * @param ValidationLogicCriterion $c */ public function addCriterion(ValidationLogicCriterion $c) { $this->criteria[] = $c; } /** * Gets all the criteria * * @return array */ public function getCriteria() { return $this->criteria; } /** * Gets a Javascript symbol for the logical operator * * @return string */ public function getLogicalOperator() { return $this->logicalOperator == "or" ? "||" : "&&"; } /** * Creates a nested {@link ValidationLogicCriteria} * * @return ValidationLogicCriteria */ public function group() { return ValidationLogicCriteria::create($this->slave, $this->master, $this); } /** * Ends the chaining and returns the parent {@link FormField} object * * @return FormField */ public function end() { // TODO: fix this // if ($this->parent) { // $this->parent->addCriterion($this); // } return $this->slave; } /** * @return string|bool */ public function phpEvalString() { $string = "("; $first = true; foreach ($this->getCriteria() as $c) { $string .= $first ? "" : " {$this->getLogicalOperator()} "; $string .= $c->toPHP(); $first = false; } $string .= ");"; return $string; } /** * Creates a JavaScript readable representation of the logic * * @return string */ public function toScript() { $script = "("; $first = true; foreach ($this->getCriteria() as $c) { $script .= $first ? "" : " {$this->getLogicalOperator()} "; $script .= $c->toScript(); $first = false; } $script .= ")"; return $script; } /** * Gets a list of all the master fields in this criteria set * * @return string */ public function getMasterList() { $list = array(); foreach ($this->getCriteria() as $c) { if ($c instanceof ValidationLogicCriteria) { $list += $c->getMasterList(); } else { $list[] = $c->getMaster(); } } return $list; } } |