123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
-
- namespace Luticate\Utils;
-
- abstract class LuBusiness {
- public static $parameters = array();
-
- /**
- * @param string $name
- * @param mixed $default
- * @return mixed
- */
- public static function getParam($name, $default = null)
- {
- return array_key_exists($name, self::$parameters) ? self::$parameters[$name] : $default;
- }
-
- /**
- * @param string[] $params
- * @return bool
- */
- public static function hasParam(array $params)
- {
- foreach ($params as $p) {
- if (!array_key_exists($p, self::$parameters)) {
- return false;
- }
- }
- return true;
- }
-
- /**
- * @param string $param
- * @param mixed $validate
- * @return string
- */
- public static function checkParam($param, $validate = null)
- {
- if (!self::hasParam(array($param)))
- abort(400, 'Missing parameter: ' . $param);
- $value = self::getParam($param);
- if ($validate != null) {
- if (is_callable($validate)) {
- $validate = $validate($value);
- }
- else {
- $validate = preg_match($validate, $value);
- }
- if (!$validate)
- abort(400, 'Bad parameter value: ' . $param);
- }
- return $value;
- }
-
- /**
- * @param string
- */
- public static function unauthorized($reason = 'Unauthorized')
- {
- abort(401, $reason);
- }
-
- /**
- * @param string
- */
- public static function notFound($reason = 'Resource not found')
- {
- abort(404, $reason);
- }
-
- /**
- * @param string
- */
- public static function badInput($reason = 'Invalid user input')
- {
- abort(400, $reason);
- }
-
- /**
- * @param $param string
- * @param $values string[]
- * @return string
- */
- public static function getParamInArray($param, $values)
- {
- $value = self::getParam($param);
- if (is_null($value) || !in_array($value, $values))
- $value = $values[0];
- return $value;
- }
-
- /**
- * @param $param string
- * @param $default int
- * @return int
- */
- public static function getParamInt($param, $default = 0)
- {
- $value = self::getParam($param);
- if (!is_null($value) && is_numeric($value))
- return (int)$value;
- return $default;
- }
-
- /**
- * @param $id int
- * @return LuDbo|null
- */
- public static function getById($id)
- {
- $data = static::getDataAccess()->getById($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()->deleteById($id);
- if (!$res)
- self::notFound(static::getResourceName() . " not found");
- return true;
- }
-
- /**
- * @return LuDataAccess
- */
- protected static function getDataAccess()
- {
- return null;
- }
-
- public static function getAll($page = 0, $perPage = PHP_INT_MAX)
- {
- return static::getDataAccess()->getAll($page, $perPage);
- }
-
- public static function getResourceName()
- {
- $match = [];
- preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
- return $match[1];
- }
- }
|