| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 | <?php
namespace Luticate\Utils;
use DB;
use Luticate\Utils\Dbo\LuDbo;
use Luticate\Utils\Dbo\LuMultipleDbo;
abstract class LuDataAccess {
    public static function transact(\Closure $function)
    {
        DB::transaction($function);
    }
    /**
     * @param $data LuModel[]
     * @return LuDbo[]
     */
    protected static function arrayToDbo($data)
    {
        $tab = [];
        foreach ($data as $q) {
            if (!is_null($q)) {
                $tab[] = $q->toDbo();
            }
        }
        return $tab;
    }
    /**
     * @param $id int
     * @return LuModel
     */
    protected static function _getModelById($id)
    {
        return static::getModel()->where('id', '=', $id)->first();
    }
    /**
     * @param $id int
     * @return LuDbo|null
     */
    public static function getById($id)
    {
        $data = self::_getModelById($id);
        if (is_null($data))
            return null;
        return $data->toDbo();
    }
    /**
     * @param $id int
     * @return bool
     */
    public static function deleteById($id)
    {
        $data = self::_getModelById($id);
        if (is_null($data))
            return false;
        $data->delete();
        return true;
    }
    /**
     * @param $data LuDbo
     * @return int
     */
    public static function addId($data)
    {
        $data = static::getModel()->fromDbo($data);
        unset($data->id);
        $data->save();
        return $data->id;
    }
    /**
     * @param $data LuDbo
     */
    public static function add($data)
    {
        $data = static::getModel()->fromDbo($data);
        $data->save();
    }
    /**
     * @param $id int
     * @param $data LuDbo
     * @return LuDbo|null
     */
    public static function editById($id, $data)
    {
        return static::getModel()->fromDbo($data, self::_getModelById($id))->save();
    }
    /**
     * @param $predicates array
     * @param $orders array
     * @param int $page
     * @param int $perPage
     * @return LuMultipleDbo
     */
    public static function getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
    {
        /**
         * @var $model LuModel
         */
        $model = static::getModel();
        foreach($predicates as $predicate) {
            $column = $predicate[0];
            $operator = $predicate[1];
            $value = $predicate[2];
            $boolean = isset($predicate[3]) ? $predicate[3] : null;
            $model = $model->where($column, $operator, $value, $boolean);
        }
        $count = $model->count();
        foreach($orders as $order) {
            $model = $model->orderBy($order[0], $order[1]);
        }
        $data = $model->take($perPage)->offset($page * $perPage)->get();
        $dbo = self::arrayToDbo($data);
        return new LuMultipleDbo($count, $dbo);
    }
    /**
     * @return LuModel
     */
    protected static function getModel()
    {
        return null;
    }
    /**
     * @return array
     */
    protected static function getOrderBy()
    {
        return array();
    }
    /**
     * @param $query
     * @return array
     */
    protected static function getQueryPredicate($query)
    {
        return array();
    }
    public static function getAll($page = 0, $perPage = PHP_INT_MAX, $query = "")
    {
        $predicates = (is_null($query) || $query == "") ? array() : static::getQueryPredicate($query);
        return self::getMultiple($predicates, static::getOrderBy(), $page, $perPage);
    }
}
 |