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 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Luticate\Utils\Business;
  3. use Luticate\Utils\DataAccess\LuDataAccess;
  4. use Luticate\Utils\Dbo\LuDbo;
  5. abstract class LuBusiness {
  6. /**
  7. * @param string $reason
  8. * @throws LuBusinessException
  9. */
  10. public static function unauthorized($reason = 'Unauthorized')
  11. {
  12. throw new LuBusinessException($reason, 401);
  13. }
  14. /**
  15. * @param string $reason
  16. * @throws LuBusinessException
  17. */
  18. public static function notFound($reason = 'Resource not found')
  19. {
  20. throw new LuBusinessException($reason, 404);
  21. }
  22. /**
  23. * @param string $reason
  24. * @throws LuBusinessException
  25. */
  26. public static function badInput($reason = 'Invalid user input')
  27. {
  28. throw new LuBusinessException($reason, 400);
  29. }
  30. /**
  31. * @param $id int
  32. * @return LuDbo|null
  33. */
  34. public static function getById($id)
  35. {
  36. $data = static::getDataAccess()->getSingleById($id);
  37. if (is_null($data)) {
  38. self::notFound(static::getResourceName() . " not found");
  39. }
  40. return $data;
  41. }
  42. /**
  43. * @param $id int
  44. * @return true
  45. */
  46. public static function deleteById($id)
  47. {
  48. $res = static::getDataAccess()->deleteSingleById($id);
  49. if (!$res) {
  50. self::notFound(static::getResourceName() . " not found");
  51. }
  52. return true;
  53. }
  54. /**
  55. * @return LuDataAccess
  56. */
  57. protected static function getDataAccess()
  58. {
  59. return null;
  60. }
  61. public static function getResourceName()
  62. {
  63. $match = [];
  64. preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
  65. return $match[1];
  66. }
  67. }