LengthAnnotation.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * This file is part of the php-annotation framework.
  4. *
  5. * (c) Rasmus Schultz <rasmus@mindplay.dk>
  6. *
  7. * This software is licensed under the GNU LGPL license
  8. * for more information, please see:
  9. *
  10. * <https://github.com/mindplay-dk/php-annotations>
  11. */
  12. namespace mindplay\demo\annotations;
  13. use mindplay\annotations\AnnotationException;
  14. /**
  15. * Specifies validation of a string, requiring a minimum and/or maximum length.
  16. *
  17. * @usage('property'=>true, 'inherited'=>true)
  18. */
  19. class LengthAnnotation extends ValidationAnnotationBase
  20. {
  21. /**
  22. * @var int|null Minimum string length (or null, if no minimum)
  23. */
  24. public $min = null;
  25. /**
  26. * @var int|null Maximum string length (or null, if no maximum)
  27. */
  28. public $max = null;
  29. /**
  30. * Initialize the annotation.
  31. */
  32. public function initAnnotation(array $properties)
  33. {
  34. if (isset($properties[0])) {
  35. if (isset($properties[1])) {
  36. $this->min = $properties[0];
  37. $this->max = $properties[1];
  38. unset($properties[1]);
  39. } else {
  40. $this->max = $properties[0];
  41. }
  42. unset($properties[0]);
  43. }
  44. parent::initAnnotation($properties);
  45. if ($this->min !== null && !is_int($this->min)) {
  46. throw new AnnotationException('LengthAnnotation requires an (integer) min property');
  47. }
  48. if ($this->max !== null && !is_int($this->max)) {
  49. throw new AnnotationException('LengthAnnotation requires an (integer) max property');
  50. }
  51. if ($this->min === null && $this->max === null) {
  52. throw new AnnotationException('LengthAnnotation requires a min and/or max property');
  53. }
  54. }
  55. }