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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Luticate\Utils\Business;
  3. use Luticate\Utils\DataAccess\LuDataAccess;
  4. use Luticate\Utils\Dbo\LuDbo;
  5. abstract class LuBusiness {
  6. protected static $_resourceName = null;
  7. protected static $_resourceNameUnPluralize = true;
  8. /**
  9. * @param string $reason
  10. * @throws LuBusinessException
  11. */
  12. public static function badInput($reason = 'Invalid user input')
  13. {
  14. throw new LuBusinessException($reason, 400);
  15. }
  16. /**
  17. * @param string $reason
  18. * @throws LuBusinessException
  19. */
  20. public static function unauthorized($reason = 'Unauthorized')
  21. {
  22. throw new LuBusinessException($reason, 401);
  23. }
  24. /**
  25. * @param string $reason
  26. * @throws LuBusinessException
  27. */
  28. public static function forbidden($reason = 'Forbidden')
  29. {
  30. throw new LuBusinessException($reason, 403);
  31. }
  32. /**
  33. * @param string $reason
  34. * @throws LuBusinessException
  35. */
  36. public static function notFound($reason = null)
  37. {
  38. if (is_null($reason)) {
  39. $reason = static::getResourceName() . " not found";
  40. }
  41. throw new LuBusinessException($reason, 404);
  42. }
  43. /**
  44. * @param \Closure $function
  45. * @return mixed
  46. */
  47. public static function transact(\Closure $function)
  48. {
  49. return static::getDataAccess()->transact($function);
  50. }
  51. /**
  52. * @param $id int
  53. */
  54. public static function checkIdExists($id)
  55. {
  56. static::getSingleById($id);
  57. }
  58. /**
  59. * @param $id int
  60. * @return LuDbo
  61. */
  62. public static function getSingleById($id)
  63. {
  64. $data = static::getDataAccess()->getSingleById($id);
  65. if (is_null($data)) {
  66. self::notFound();
  67. }
  68. return $data;
  69. }
  70. /**
  71. * @param $id int
  72. * @return LuDbo
  73. */
  74. public static function deleteSingleById($id)
  75. {
  76. $dbo = static::getSingleById($id);
  77. static::getDataAccess()->deleteSingleById($id);
  78. return $dbo;
  79. }
  80. /**
  81. * @param $dbo LuDbo
  82. * @return LuDbo
  83. */
  84. public static function addSingleId($dbo)
  85. {
  86. $id = static::getDataAccess()->addSingleId($dbo);
  87. return static::getSingleById($id);
  88. }
  89. /**
  90. * @param $id int
  91. * @param $dbo LuDbo
  92. * @return LuDbo
  93. */
  94. public static function editSingleById($id, $dbo)
  95. {
  96. static::checkIdExists($id);
  97. static::getDataAccess()->editSingleById($dbo, $id);
  98. return static::getSingleById($id);
  99. }
  100. /**
  101. * @return LuDataAccess
  102. */
  103. protected static function getDataAccess()
  104. {
  105. return null;
  106. }
  107. public static function getResourceName()
  108. {
  109. $res = static::$_resourceName;
  110. if ($res == null) {
  111. $match = [];
  112. $className = get_called_class();
  113. preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
  114. if (count($match) < 2) {
  115. $res = $className;
  116. }
  117. else {
  118. $res = $match[1];
  119. }
  120. if (static::$_resourceNameUnPluralize) {
  121. if (LuStringUtils::endsWith($res, "ies")) {
  122. $res = substr($res, 0, strlen($res) - 3) . "y";
  123. }
  124. else if (LuStringUtils::endsWith($res, "s")) {
  125. $res = substr($res, 0, strlen($res) - 1);
  126. }
  127. }
  128. }
  129. return $res;
  130. }
  131. }