PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Module/Data/src/Cmf/Data/Validator/StrLen.php

https://github.com/itcreator/custom-cmf
PHP | 57 lines | 25 code | 9 blank | 23 comment | 5 complexity | 84a240da953bb0a6f2ad00ab4a9b49ce MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Custom CMF.
  4. *
  5. * @link https://github.com/itcreator/custom-cmf for the canonical source repository
  6. * @copyright Copyright (c) 2011 Vital Leshchyk <vitalleshchyk@gmail.com>
  7. * @license https://github.com/itcreator/custom-cmf/blob/master/LICENSE
  8. */
  9. namespace Cmf\Data\Validator;
  10. /**
  11. * String length validator
  12. *
  13. * @author Vital Leshchyk <vitalleshchyk@gmail.com>
  14. */
  15. class StrLen extends AbstractValidator
  16. {
  17. const INVALID = 'invalid';
  18. /** @var int */
  19. protected $minLength = 0;
  20. /** @var int|null */
  21. protected $maxLength = 0;
  22. /**
  23. * @throws Exception
  24. * @param int $minLength
  25. * @param int|null $maxLength
  26. */
  27. public function __construct($minLength, $maxLength = null)
  28. {
  29. if ($maxLength < 0 || $minLength < 0 || $maxLength < $minLength) {
  30. throw new Exception('Incorrect min and max parameters');
  31. }
  32. $this->maxLength = $maxLength;
  33. $this->minLength = $minLength;
  34. }
  35. /**
  36. * @param string $value
  37. * @return bool
  38. */
  39. public function isValid($value)
  40. {
  41. $result = (mb_strlen($value) >= $this->minLength && mb_strlen($value) <= $this->maxLength);
  42. if (!($result)) {
  43. $lng = \Cmf\Language\Factory::get($this);
  44. $this->messages[self::INVALID] = sprintf($lng[self::INVALID], $this->minLength, $this->maxLength);
  45. };
  46. return $result;
  47. }
  48. }