You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuBusiness.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Luticate\Utils;
  3. abstract class LuBusiness {
  4. public static $parameters = array();
  5. /**
  6. * @param string $name
  7. * @param mixed $default
  8. * @return mixed
  9. */
  10. public static function getParam($name, $default = null)
  11. {
  12. return array_key_exists($name, self::$parameters) ? self::$parameters[$name] : $default;
  13. }
  14. /**
  15. * @param string[] $params
  16. * @return bool
  17. */
  18. public static function hasParam(array $params)
  19. {
  20. foreach ($params as $p) {
  21. if (!array_key_exists($p, self::$parameters)) {
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
  27. /**
  28. * @param string $param
  29. * @param mixed $validate
  30. * @return string
  31. */
  32. public static function checkParam($param, $validate = null)
  33. {
  34. if (!self::hasParam(array($param)))
  35. abort(400, 'Missing parameter: ' . $param);
  36. $value = self::getParam($param);
  37. if ($validate != null) {
  38. if (is_callable($validate)) {
  39. $validate = $validate($value);
  40. }
  41. else {
  42. $validate = preg_match($validate, $value);
  43. }
  44. if (!$validate)
  45. abort(400, 'Bad parameter value: ' . $param);
  46. }
  47. return $value;
  48. }
  49. /**
  50. * @param string
  51. */
  52. public static function unauthorized($reason = 'Unauthorized')
  53. {
  54. abort(401, $reason);
  55. }
  56. /**
  57. * @param string
  58. */
  59. public static function notFound($reason = 'Resource not found')
  60. {
  61. abort(404, $reason);
  62. }
  63. /**
  64. * @param string
  65. */
  66. public static function badInput($reason = 'Invalid user input')
  67. {
  68. abort(400, $reason);
  69. }
  70. /**
  71. * @param $param string
  72. * @param $values string[]
  73. * @return string
  74. */
  75. public static function getParamInArray($param, $values)
  76. {
  77. $value = self::getParam($param);
  78. if (is_null($value) || !in_array($value, $values))
  79. $value = $values[0];
  80. return $value;
  81. }
  82. /**
  83. * @param $param string
  84. * @param $default int
  85. * @return int
  86. */
  87. public static function getParamInt($param, $default = 0)
  88. {
  89. $value = self::getParam($param);
  90. if (!is_null($value) && is_numeric($value))
  91. return (int)$value;
  92. return $default;
  93. }
  94. /**
  95. * @param $id int
  96. * @return LuDbo|null
  97. */
  98. public static function getById($id)
  99. {
  100. $data = static::getDataAccess()->getById($id);
  101. if (is_null($data))
  102. self::notFound(static::getResourceName() . " not found");
  103. return $data;
  104. }
  105. /**
  106. * @param $id int
  107. * @return true
  108. */
  109. public static function deleteById($id)
  110. {
  111. $res = static::getDataAccess()->deleteById($id);
  112. if (!$res)
  113. self::notFound(static::getResourceName() . " not found");
  114. return true;
  115. }
  116. /**
  117. * @return LuDataAccess
  118. */
  119. protected static function getDataAccess()
  120. {
  121. return null;
  122. }
  123. public static function getAll($page = 0, $perPage = PHP_INT_MAX, $query = "")
  124. {
  125. return static::getDataAccess()->getAll($page, $perPage, $query);
  126. }
  127. public static function getResourceName()
  128. {
  129. $match = [];
  130. preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
  131. return $match[1];
  132. }
  133. }