Checks.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. class Checks
  4. {
  5. public $filters = "filters";
  6. /**
  7. * Throws exception if not integer.
  8. * @param Request|array $all Contains array with query.
  9. * @param string $property Contains the name of property
  10. * @param boolean $required Check if property exists in request
  11. * @param mixed $default Default value.
  12. * @return string Return string or if the property not exists.
  13. * @throws Error Throws errors.
  14. */
  15. function string($all, $property, $required = true, $default = null)
  16. {
  17. if ($all != null) {
  18. if ($all instanceof Request) {
  19. $all = $all->query->all();
  20. }
  21. $key_exists = array_key_exists($property, $all);
  22. if (!$key_exists && $default != null) {
  23. $all[$property] = $default;
  24. $key_exists = true;
  25. }
  26. if (!$key_exists) {
  27. if ($required) {
  28. throw (new Errors())->errorRequired($property, "string");
  29. } else {
  30. // value not exists
  31. return null;
  32. }
  33. } else if (is_string($all[$property])) {
  34. return $all[$property];
  35. } else {
  36. throw (new Errors())->errorString($property);
  37. }
  38. } else {
  39. return null;
  40. }
  41. }
  42. /**
  43. * Throws exception if not integer.
  44. * @param Request|array $all Contains array with query.
  45. * @param string $property Contains the name of property
  46. * @param boolean $required Check if property exists in request
  47. * @param mixed $default Default value.
  48. * @return Integer Return integer or if the property not exists.
  49. * @throws Error Throws errors.
  50. */
  51. function integer($all, $property, $required = true, $default = null)
  52. {
  53. if ($all != null) {
  54. if ($all instanceof Request) {
  55. $all = $all->query->all();
  56. }
  57. $key_exists = array_key_exists($property, $all);
  58. if (!$key_exists && $default != null) {
  59. $all[$property] = $default;
  60. $key_exists = true;
  61. }
  62. if (!$key_exists) {
  63. if ($required) {
  64. throw (new Errors())->errorRequired($property, "integer");
  65. } else {
  66. // value not exists
  67. return null;
  68. }
  69. } else if (is_numeric($all[$property])) {
  70. return $all[$property];
  71. } else {
  72. throw (new Errors())->errorInteger($property);
  73. }
  74. } else {
  75. return null;
  76. }
  77. }
  78. /**
  79. * Throws exception if not integer.
  80. * @param string $property Contains the name of property
  81. * @param string $value Value for array key
  82. * @return string Return strings with format 'filters[$property]=$value or null.
  83. * @throws Error Throws errors.
  84. */
  85. public function filters($property, $value)
  86. {
  87. if ($value != null) {
  88. $value = "filters[$property]=$value";
  89. }
  90. return $value;
  91. }
  92. /**
  93. * Throws exception if not boolean.
  94. * @param Request|array $all Contains array with query.
  95. * @param string $property Contains the name of property
  96. * @param boolean $required Check if property exists in request
  97. * @param mixed $default Default value.
  98. * @return integer Return integer 0, 1 or null.
  99. * @throws Error Throws errors.
  100. */
  101. function boolean($all, $property, $required = true, $default = null)
  102. {
  103. if ($all != null) {
  104. if ($all instanceof Request) {
  105. $all = $all->query->all();
  106. }
  107. $key_exists = array_key_exists($property, $all);
  108. if (!$key_exists && $default != null) {
  109. $all[$property] = $default;
  110. $key_exists = true;
  111. }
  112. if (!$key_exists) {
  113. if ($required) {
  114. throw (new Errors())->errorRequired($property, "boolean");
  115. } else {
  116. // value not exists
  117. return null;
  118. }
  119. } else if (is_bool($all[$property]) || in_array(strtolower($all[$property]),['true','false','0','1'])) {
  120. $bool = filter_var( $all[$property], FILTER_VALIDATE_BOOLEAN );
  121. return (int)$bool;
  122. } else {
  123. throw (new Errors())->errorBoolean($property);
  124. }
  125. } else {
  126. return null;
  127. }
  128. }
  129. }