validator.class.inc.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // Copyright (C) 2010-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. namespace Combodo\iTop\Form\Validator;
  19. /**
  20. * Description of Validator
  21. *
  22. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  23. */
  24. class Validator
  25. {
  26. const VALIDATOR_NAME = 'expression';
  27. const DEFAULT_REGEXP = '';
  28. const DEFAULT_ERROR_MESSAGE = 'Core:Validator:Default';
  29. protected $sRegExp;
  30. protected $sErrorMessage;
  31. public static function GetName()
  32. {
  33. return static::VALIDATOR_NAME;
  34. }
  35. /**
  36. *
  37. * @param Closure $callback (Used in the $oForm->AddField($sId, ..., function() use ($oManager, $oForm, '...') { ... } ); )
  38. */
  39. public function __construct($sRegExp = null, $sErrorMessage = null)
  40. {
  41. $this->sRegExp = ($sRegExp === null) ? static::DEFAULT_REGEXP : $sRegExp;
  42. $this->sErrorMessage = ($sErrorMessage === null) ? static::DEFAULT_ERROR_MESSAGE : $sErrorMessage;
  43. $this->ComputeConstraints();
  44. }
  45. /**
  46. * Returns the regular expression of the validator.
  47. *
  48. * @param boolean $bWithSlashes If true, surrounds $sRegExp with '/'. Used with preg_match & co
  49. * @return string
  50. */
  51. public function GetRegExp($bWithSlashes = false)
  52. {
  53. if ($bWithSlashes)
  54. {
  55. $this->sRegExp = str_replace('/', '\\/', $this->sRegExp);
  56. }
  57. return ($bWithSlashes) ? '/' . $this->sRegExp . '/' : $this->sRegExp;
  58. }
  59. public function GetErrorMessage()
  60. {
  61. return $this->sErrorMessage;
  62. }
  63. public function SetRegExp($sRegExp)
  64. {
  65. $this->sRegExp = $sRegExp;
  66. $this->ComputeConstraints();
  67. return $this;
  68. }
  69. public function SetErrorMessage($sErrorMessage)
  70. {
  71. $this->sErrorMessage = $sErrorMessage;
  72. $this->ComputeConstraints();
  73. return $this;
  74. }
  75. /**
  76. * Computes the regular expression and error message when changing constraints on the validator.
  77. * Should be called in the validator's setters.
  78. */
  79. public function ComputeConstraints()
  80. {
  81. $this->ComputeRegularExpression();
  82. $this->ComputeErrorMessage();
  83. }
  84. /**
  85. * Computes the regular expression when changing constraints on the validator.
  86. */
  87. public function ComputeRegularExpression()
  88. {
  89. // Overload when necessary
  90. }
  91. /**
  92. * Computes the error message when changing constraints on the validator.
  93. */
  94. public function ComputeErrorMessage()
  95. {
  96. // Overload when necessary
  97. }
  98. }