Dimensions.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Illuminate\Validation\Rules;
  3. use Illuminate\Support\Traits\Conditionable;
  4. use Stringable;
  5. class Dimensions implements Stringable
  6. {
  7. use Conditionable;
  8. /**
  9. * The constraints for the dimensions rule.
  10. *
  11. * @var array
  12. */
  13. protected $constraints = [];
  14. /**
  15. * Create a new dimensions rule instance.
  16. *
  17. * @param array $constraints
  18. */
  19. public function __construct(array $constraints = [])
  20. {
  21. $this->constraints = $constraints;
  22. }
  23. /**
  24. * Set the "width" constraint.
  25. *
  26. * @param int $value
  27. * @return $this
  28. */
  29. public function width($value)
  30. {
  31. $this->constraints['width'] = $value;
  32. return $this;
  33. }
  34. /**
  35. * Set the "height" constraint.
  36. *
  37. * @param int $value
  38. * @return $this
  39. */
  40. public function height($value)
  41. {
  42. $this->constraints['height'] = $value;
  43. return $this;
  44. }
  45. /**
  46. * Set the "min width" constraint.
  47. *
  48. * @param int $value
  49. * @return $this
  50. */
  51. public function minWidth($value)
  52. {
  53. $this->constraints['min_width'] = $value;
  54. return $this;
  55. }
  56. /**
  57. * Set the "min height" constraint.
  58. *
  59. * @param int $value
  60. * @return $this
  61. */
  62. public function minHeight($value)
  63. {
  64. $this->constraints['min_height'] = $value;
  65. return $this;
  66. }
  67. /**
  68. * Set the "max width" constraint.
  69. *
  70. * @param int $value
  71. * @return $this
  72. */
  73. public function maxWidth($value)
  74. {
  75. $this->constraints['max_width'] = $value;
  76. return $this;
  77. }
  78. /**
  79. * Set the "max height" constraint.
  80. *
  81. * @param int $value
  82. * @return $this
  83. */
  84. public function maxHeight($value)
  85. {
  86. $this->constraints['max_height'] = $value;
  87. return $this;
  88. }
  89. /**
  90. * Set the "ratio" constraint.
  91. *
  92. * @param float $value
  93. * @return $this
  94. */
  95. public function ratio($value)
  96. {
  97. $this->constraints['ratio'] = $value;
  98. return $this;
  99. }
  100. /**
  101. * Set the minimum aspect ratio.
  102. *
  103. * @param float $value
  104. * @return $this
  105. */
  106. public function minRatio($value)
  107. {
  108. $this->constraints['min_ratio'] = $value;
  109. return $this;
  110. }
  111. /**
  112. * Set the maximum aspect ratio.
  113. *
  114. * @param float $value
  115. * @return $this
  116. */
  117. public function maxRatio($value)
  118. {
  119. $this->constraints['max_ratio'] = $value;
  120. return $this;
  121. }
  122. /**
  123. * Set the aspect ratio range.
  124. *
  125. * @param float $min
  126. * @param float $max
  127. * @return $this
  128. */
  129. public function ratioBetween($min, $max)
  130. {
  131. $this->constraints['min_ratio'] = $min;
  132. $this->constraints['max_ratio'] = $max;
  133. return $this;
  134. }
  135. /**
  136. * Convert the rule to a validation string.
  137. *
  138. * @return string
  139. */
  140. public function __toString()
  141. {
  142. $result = '';
  143. foreach ($this->constraints as $key => $value) {
  144. $result .= "$key=$value,";
  145. }
  146. return 'dimensions:'.substr($result, 0, -1);
  147. }
  148. }