|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace faiverson\gateways\abstracts; |
| 4 | + |
| 5 | +use faiverson\gateways\exceptions\RepositoryException; |
| 6 | +use faiverson\gateways\contracts\RepositoryInterface; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class Repository |
| 11 | + */ |
| 12 | +abstract class Repository implements RepositoryInterface |
| 13 | +{ |
| 14 | + protected $model; |
| 15 | + |
| 16 | + public function __construct() |
| 17 | + { |
| 18 | + $this->app = app(); |
| 19 | + $this->model = $this->app->make($this->model()); |
| 20 | + if (!$this->model instanceof Model) { |
| 21 | + throw new RepositoryException( |
| 22 | + "Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model" |
| 23 | + ); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Specify Model class name |
| 29 | + * |
| 30 | + * @return mixed |
| 31 | + */ |
| 32 | + abstract public function model(); |
| 33 | + |
| 34 | + /** |
| 35 | + * @param array $columns |
| 36 | + * @return mixed |
| 37 | + */ |
| 38 | + public function all( |
| 39 | + $data = null, |
| 40 | + $columns = ['*'], |
| 41 | + $limit = null, |
| 42 | + $offset = null, |
| 43 | + $order_by = null, |
| 44 | + $filters = [], |
| 45 | + $with = [] |
| 46 | + ) { |
| 47 | + $query = $this->model; |
| 48 | + foreach ($with as $join) { |
| 49 | + $query = $query->with($join); |
| 50 | + } |
| 51 | + $query = $this->setFilters($query, $filters); |
| 52 | + if ($limit != null) { |
| 53 | + $query = $query->take($limit); |
| 54 | + } |
| 55 | + |
| 56 | + if ($offset != null) { |
| 57 | + $query = $query->skip($offset); |
| 58 | + } |
| 59 | + |
| 60 | + if ($order_by != null) { |
| 61 | + foreach ($order_by as $column => $dir) { |
| 62 | + $query = $query->orderBy($column, $dir); |
| 63 | + } |
| 64 | + } |
| 65 | + return $query->get($columns); |
| 66 | + } |
| 67 | + |
| 68 | + public function total($filters = array()) |
| 69 | + { |
| 70 | + $query = $this->model; |
| 71 | + $query = $this->setFilters($query, $filters); |
| 72 | + return $query->count(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param array $data |
| 77 | + * @return mixed |
| 78 | + */ |
| 79 | + public function create(array $data) |
| 80 | + { |
| 81 | + return $this->model->create($this->setAttributes($data)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param array $data |
| 86 | + * @return mixed |
| 87 | + */ |
| 88 | + public function firstOrCreate(array $data) |
| 89 | + { |
| 90 | + return $this->model->firstOrCreate($this->setAttributes($data)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param array $data |
| 95 | + * @return mixed |
| 96 | + */ |
| 97 | + public function firstOrNew(array $data) |
| 98 | + { |
| 99 | + return $this->model->firstOrNew($this->setAttributes($data)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param array $data |
| 104 | + * @return mixed |
| 105 | + */ |
| 106 | + public function updateOrCreate(array $data, array $extra) |
| 107 | + { |
| 108 | + return $this->model->updateOrCreate($this->setAttributes($data), $this->setAttributes($extra)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param array $data |
| 113 | + * @param $id |
| 114 | + * @param string $attribute |
| 115 | + * @return mixed |
| 116 | + */ |
| 117 | + public function update(array $data, $id) |
| 118 | + { |
| 119 | + return $this->model->find($id)->update($this->setAttributes($data)); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param $id |
| 124 | + * @return mixed |
| 125 | + */ |
| 126 | + public function destroy($id) |
| 127 | + { |
| 128 | + return $this->model->destroy($id); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param $id |
| 133 | + * @param array $columns |
| 134 | + * @return mixed |
| 135 | + */ |
| 136 | + public function find($id, $columns = ['*']) |
| 137 | + { |
| 138 | + return $this->model->find($id, $columns); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @param $attribute |
| 143 | + * @param $value |
| 144 | + * @param array $columns |
| 145 | + * @return mixed |
| 146 | + */ |
| 147 | + public function findBy($attribute, $value, $columns = ['*'], $limit = null, $offset = null, $order_by = null, $with = []) |
| 148 | + { |
| 149 | + $query = $this->model; |
| 150 | + |
| 151 | + if (!empty($with) && count($with) > 0) { |
| 152 | + foreach ($with as $join) { |
| 153 | + $query = $query->with($join); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if ($limit != null) { |
| 158 | + $query = $query->take($limit); |
| 159 | + } |
| 160 | + |
| 161 | + if ($offset != null) { |
| 162 | + $query = $query->skip($offset); |
| 163 | + } |
| 164 | + |
| 165 | + if ($order_by != null) { |
| 166 | + foreach ($order_by as $column => $dir) { |
| 167 | + $query = $query->orderBy($column, $dir); |
| 168 | + } |
| 169 | + } |
| 170 | + return $query->where($attribute, $value)->get($columns); |
| 171 | + } |
| 172 | + |
| 173 | + public function setAttributes(array $data) |
| 174 | + { |
| 175 | + $data = array_map(function ($value) { |
| 176 | + return is_array($value) || is_object($value) ? $this->setAttributes($value) : trim($value); |
| 177 | + }, $data); |
| 178 | + |
| 179 | + $data = array_filter($data, function ($value) { |
| 180 | + return ($value !== null && $value !== ''); |
| 181 | + }); |
| 182 | + |
| 183 | + return $data; |
| 184 | + } |
| 185 | + |
| 186 | + public function setFilters($query, Array $filters) |
| 187 | + { |
| 188 | + return $query; |
| 189 | + } |
| 190 | +} |
0 commit comments