123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
-
- namespace Luticate\Utils\Business;
-
- use Luticate\Utils\DataAccess\LuDataAccess;
- use Luticate\Utils\Dbo\LuDbo;
-
- abstract class LuBusiness {
- protected static $_resourceName = null;
- protected static $_resourceNameUnPluralize = true;
-
- /**
- * @param string $reason
- * @throws LuBusinessException
- */
- public static function badInput($reason = 'Invalid user input')
- {
- throw new LuBusinessException($reason, 400);
- }
-
- /**
- * @param string $reason
- * @throws LuBusinessException
- */
- public static function unauthorized($reason = 'Unauthorized')
- {
- throw new LuBusinessException($reason, 401);
- }
-
- /**
- * @param string $reason
- * @throws LuBusinessException
- */
- public static function forbidden($reason = 'Forbidden')
- {
- throw new LuBusinessException($reason, 403);
- }
-
- /**
- * @param string $reason
- * @throws LuBusinessException
- */
- public static function notFound($reason = null)
- {
- if (is_null($reason)) {
- $reason = static::getResourceName() . " not found";
- }
- throw new LuBusinessException($reason, 404);
- }
-
- /**
- * @param \Closure $function
- * @return mixed
- */
- public static function transact(\Closure $function)
- {
- return static::getDataAccess()->transact($function);
- }
-
- /**
- * @param $id int
- */
- public static function checkIdExists($id)
- {
- static::getSingleById($id);
- }
-
- /**
- * @param $id int
- * @return LuDbo
- */
- public static function getSingleById($id)
- {
- $data = static::getDataAccess()->getSingleById($id);
- if (is_null($data)) {
- self::notFound();
- }
- return $data;
- }
-
- /**
- * @param $id int
- * @return LuDbo
- */
- public static function deleteSingleById($id)
- {
- $dbo = static::getSingleById($id);
- static::getDataAccess()->deleteSingleById($id);
- return $dbo;
- }
-
- /**
- * @param $dbo LuDbo
- * @return LuDbo
- */
- public static function addSingleId($dbo)
- {
- $id = static::getDataAccess()->addSingleId($dbo);
- return static::getSingleById($id);
- }
-
- /**
- * @param $id int
- * @param $dbo LuDbo
- * @return LuDbo
- */
- public static function editSingleById($id, $dbo)
- {
- static::checkIdExists($id);
- static::getDataAccess()->editSingleById($dbo, $id);
- return static::getSingleById($id);
- }
-
- /**
- * @return LuDataAccess
- */
- protected static function getDataAccess()
- {
- return null;
- }
-
- public static function getResourceName()
- {
- $res = static::$_resourceName;
- if ($res == null) {
- $match = [];
- $className = get_called_class();
- preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
- if (count($match) < 2) {
- $res = $className;
- }
- else {
- $res = $match[1];
- }
- if (static::$_resourceNameUnPluralize) {
- if (LuStringUtils::endsWith($res, "ies")) {
- $res = substr($res, 0, strlen($res) - 3) . "y";
- }
- else if (LuStringUtils::endsWith($res, "s")) {
- $res = substr($res, 0, strlen($res) - 1);
- }
- }
- }
- return $res;
- }
- }
|