SettingNumeric.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace dokuwiki\plugin\config\core\Setting;
  3. /**
  4. * Class setting_numeric
  5. */
  6. class SettingNumeric extends SettingString {
  7. // This allows for many PHP syntax errors...
  8. // var $_pattern = '/^[-+\/*0-9 ]*$/';
  9. // much more restrictive, but should eliminate syntax errors.
  10. protected $pattern = '/^[-+]? *[0-9]+ *(?:[-+*] *[0-9]+ *)*$/';
  11. protected $min = null;
  12. protected $max = null;
  13. /** @inheritdoc */
  14. public function update($input) {
  15. $local = $this->local;
  16. $valid = parent::update($input);
  17. if($valid && !(is_null($this->min) && is_null($this->max))) {
  18. $numeric_local = (int) eval('return ' . $this->local . ';');
  19. if((!is_null($this->min) && $numeric_local < $this->min) ||
  20. (!is_null($this->max) && $numeric_local > $this->max)) {
  21. $this->error = true;
  22. $this->input = $input;
  23. $this->local = $local;
  24. $valid = false;
  25. }
  26. }
  27. return $valid;
  28. }
  29. /** @inheritdoc */
  30. public function out($var, $fmt = 'php') {
  31. if($fmt != 'php') return '';
  32. $local = $this->local === '' ? "''" : $this->local;
  33. $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";
  34. return $out;
  35. }
  36. }