您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuStringDbo.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. class LuStringDbo extends LuDbo
  10. {
  11. /**
  12. * @var $_value string
  13. */
  14. private $_value;
  15. /**
  16. * @return string
  17. */
  18. public function getString()
  19. {
  20. return $this->_value;
  21. }
  22. /**
  23. * @param string $value
  24. */
  25. public function setString($value)
  26. {
  27. $this->_value = $value;
  28. }
  29. function jsonSerialize()
  30. {
  31. return $this->_value;
  32. }
  33. public static function jsonDeserialize($json)
  34. {
  35. if (is_object($json) || is_array($json) || is_bool($json) || is_null($json)) {
  36. throw new LuDboDeserializeException("Invalid string value");
  37. }
  38. $val = new self();
  39. $val->setString($json . "");
  40. return $val;
  41. }
  42. public static function generateSample()
  43. {
  44. return "sample string";
  45. }
  46. protected function getLength($trim)
  47. {
  48. return strlen($trim ? trim($this->_value) : $this->_value);
  49. }
  50. public function between($min, $max, $trim = true)
  51. {
  52. $len = self::getLength($trim);
  53. if ($len < $min || $len > $max) {
  54. throw new LuDboConstraintException("String length must be between ${min} and ${max} inclusive");
  55. }
  56. }
  57. public function notEmpty($trim = true)
  58. {
  59. static::min(1, $trim);
  60. }
  61. public function min($min, $trim = true)
  62. {
  63. if (self::getLength($trim) < $min) {
  64. throw new LuDboConstraintException("String length must be greater or equal to ${min}");
  65. }
  66. }
  67. public function max($max, $trim = true)
  68. {
  69. if (self::getLength($trim) > $max) {
  70. throw new LuDboConstraintException("String length must be smaller or equal to ${max}");
  71. }
  72. }
  73. public function trim()
  74. {
  75. $this->_value = trim($this->_value);
  76. }
  77. }