Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LuStringDbo.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 min($min, $trim = true)
  58. {
  59. if (self::getLength($trim) < $min) {
  60. throw new LuDboConstraintException("String length must be greater or equal to ${min}");
  61. }
  62. }
  63. public function max($max, $trim = true)
  64. {
  65. if (self::getLength($trim) > $max) {
  66. throw new LuDboConstraintException("String length must be smaller or equal to ${max}");
  67. }
  68. }
  69. }