You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuStringDbo.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 5/29/16
  6. * Time: 2:41 PM
  7. */
  8. namespace Luticate\Utils\Dbo;
  9. use Luticate\Utils\LuBusiness;
  10. use Luticate\Utils\LuDbo;
  11. class LuStringDbo extends LuDbo
  12. {
  13. /**
  14. * @var $_value string
  15. */
  16. private $_value;
  17. /**
  18. * @return string
  19. */
  20. public function getString()
  21. {
  22. return $this->_value;
  23. }
  24. /**
  25. * @param string $value
  26. */
  27. public function setString($value)
  28. {
  29. $this->_value = $value;
  30. }
  31. function jsonSerialize()
  32. {
  33. return $this->_value;
  34. }
  35. public static function jsonDeserialize($json)
  36. {
  37. if (is_object($json) || is_array($json) || is_bool($json) || is_null($json)) {
  38. throw new LuDboDeserializeException("Invalid string value");
  39. }
  40. $val = new self();
  41. $val->setString($json . "");
  42. return $val;
  43. }
  44. public static function generateSample()
  45. {
  46. return "sample string";
  47. }
  48. protected function getLength($trim)
  49. {
  50. return strlen($trim ? trim($this->_value) : $this->_value);
  51. }
  52. public function between($min, $max, $trim = true)
  53. {
  54. $len = self::getLength($trim);
  55. if ($len < $min || $len > $max) {
  56. throw new LuDboConstraintException("String length must be between ${min} and ${max} inclusive");
  57. }
  58. }
  59. public function min($min, $trim = true)
  60. {
  61. if (self::getLength($trim) < $min) {
  62. throw new LuDboConstraintException("String length must be greater or equal to ${min}");
  63. }
  64. }
  65. public function max($max, $trim = true)
  66. {
  67. if (self::getLength($trim) > $max) {
  68. throw new LuDboConstraintException("String length must be smaller or equal to ${max}");
  69. }
  70. }
  71. }