1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
-
- namespace Luticate\Utils\Business;
-
- use Luticate\Utils\DataAccess\LuDataAccess;
- use Luticate\Utils\Dbo\LuDbo;
-
- abstract class LuBusiness {
-
- /**
- * @param string $reason
- * @throws LuBusinessException
- */
- public static function unauthorized($reason = 'Unauthorized')
- {
- throw new LuBusinessException($reason, 401);
- }
-
- /**
- * @param string $reason
- * @throws LuBusinessException
- */
- public static function notFound($reason = 'Resource not found')
- {
- throw new LuBusinessException($reason, 404);
- }
-
- /**
- * @param string $reason
- * @throws LuBusinessException
- */
- public static function badInput($reason = 'Invalid user input')
- {
- throw new LuBusinessException($reason, 400);
- }
-
- /**
- * @param $id int
- * @return LuDbo|null
- */
- public static function getById($id)
- {
- $data = static::getDataAccess()->getSingleById($id);
- if (is_null($data)) {
- self::notFound(static::getResourceName() . " not found");
- }
- return $data;
- }
-
- /**
- * @param $id int
- * @return true
- */
- public static function deleteById($id)
- {
- $res = static::getDataAccess()->deleteSingleById($id);
- if (!$res) {
- self::notFound(static::getResourceName() . " not found");
- }
- return true;
- }
-
- /**
- * @return LuDataAccess
- */
- protected static function getDataAccess()
- {
- return null;
- }
-
- public static function getResourceName()
- {
- $match = [];
- preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
- return $match[1];
- }
- }
|