validator.class.inc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $sRet = '/' . str_replace('/', '\\/', $this->sRegExp) . '/';
  56. }
  57. else
  58. {
  59. $sRet = $this->sRegExp;
  60. }
  61. return $sRet;
  62. }
  63. public function GetErrorMessage()
  64. {
  65. return $this->sErrorMessage;
  66. }
  67. public function SetRegExp($sRegExp)
  68. {
  69. $this->sRegExp = $sRegExp;
  70. $this->ComputeConstraints();
  71. return $this;
  72. }
  73. public function SetErrorMessage($sErrorMessage)
  74. {
  75. $this->sErrorMessage = $sErrorMessage;
  76. $this->ComputeConstraints();
  77. return $this;
  78. }
  79. /**
  80. * Computes the regular expression and error message when changing constraints on the validator.
  81. * Should be called in the validator's setters.
  82. */
  83. public function ComputeConstraints()
  84. {
  85. $this->ComputeRegularExpression();
  86. $this->ComputeErrorMessage();
  87. }
  88. /**
  89. * Computes the regular expression when changing constraints on the validator.
  90. */
  91. public function ComputeRegularExpression()
  92. {
  93. // Overload when necessary
  94. }
  95. /**
  96. * Computes the error message when changing constraints on the validator.
  97. */
  98. public function ComputeErrorMessage()
  99. {
  100. // Overload when necessary
  101. }
  102. }