|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Laravelplus\RepositoryPattern; |
| 6 | + |
| 7 | +use BadMethodCallException; |
| 8 | +use Illuminate\Database\Eloquent\Collection; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | +use Illuminate\Pagination\LengthAwarePaginator; |
| 11 | +use Illuminate\Support\Facades\DB; |
| 12 | +use Laravelplus\RepositoryPattern\Contracts\RepositoryInterface; |
| 13 | + |
| 14 | +abstract class BaseRepository implements RepositoryInterface |
| 15 | +{ |
| 16 | + /** |
| 17 | + * The model instance. |
| 18 | + * |
| 19 | + * @var Model |
| 20 | + */ |
| 21 | + protected $model; |
| 22 | + |
| 23 | + // Static properties for configuration |
| 24 | + protected static string $modelClass = ''; |
| 25 | + |
| 26 | + protected static string $table = ''; |
| 27 | + |
| 28 | + protected static string $connection = 'mysql'; |
| 29 | + |
| 30 | + protected static string $primaryKey = 'id'; |
| 31 | + |
| 32 | + protected static array $relations = []; |
| 33 | + |
| 34 | + protected static array $casts = []; |
| 35 | + |
| 36 | + protected static array $hidden = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * BaseRepository constructor. |
| 40 | + */ |
| 41 | + public function __construct(?Model $model = null) |
| 42 | + { |
| 43 | + $modelClass = static::$modelClass ?: Model::class; |
| 44 | + $this->model = $model ?? new $modelClass(); |
| 45 | + $this->table = static::$table ?: $this->model->getTable(); |
| 46 | + $defaultConnection = config('database.default'); |
| 47 | + $conn = static::$connection ?: $this->model->getConnectionName(); |
| 48 | + $this->connection = ($conn && $conn !== $defaultConnection) ? $conn : null; |
| 49 | + } |
| 50 | + |
| 51 | + protected function getQuery() |
| 52 | + { |
| 53 | + if ($this->connection) { |
| 54 | + return DB::connection($this->connection)->table($this->table); |
| 55 | + } |
| 56 | + |
| 57 | + return DB::table($this->table); |
| 58 | + } |
| 59 | + |
| 60 | + public function __get($name) |
| 61 | + { |
| 62 | + if ($name === 'table') { |
| 63 | + return $this->getQuery(); |
| 64 | + } |
| 65 | + if ($name === 'model') { |
| 66 | + return $this->model; |
| 67 | + } |
| 68 | + if ($name === 'relations') { |
| 69 | + return static::$relations; |
| 70 | + } |
| 71 | + if ($name === 'casts') { |
| 72 | + return static::$casts; |
| 73 | + } |
| 74 | + |
| 75 | + return $this->$name; |
| 76 | + } |
| 77 | + |
| 78 | + public function __call($method, $arguments) |
| 79 | + { |
| 80 | + if (isset(static::$relations[$method])) { |
| 81 | + $relation = static::$relations[$method]; |
| 82 | + $defaultConnection = config('database.default'); |
| 83 | + if (is_string($relation) && class_exists($relation)) { |
| 84 | + return new $relation(); |
| 85 | + } |
| 86 | + if (is_string($relation)) { |
| 87 | + $connection = static::$connection; |
| 88 | + if (!$connection || $connection === $defaultConnection) { |
| 89 | + return DB::table($relation); |
| 90 | + } |
| 91 | + |
| 92 | + return DB::connection($connection)->table($relation); |
| 93 | + } |
| 94 | + if (is_array($relation) && isset($relation['table'])) { |
| 95 | + $connection = $relation['connection'] ?? static::$connection; |
| 96 | + $primaryKey = $relation['primaryKey'] ?? 'id'; |
| 97 | + $foreignKey = $relation['foreignKey'] ?? null; |
| 98 | + $query = (!$connection || $connection === $defaultConnection) |
| 99 | + ? DB::table($relation['table']) |
| 100 | + : DB::connection($connection)->table($relation['table']); |
| 101 | + if ($foreignKey && isset($arguments[0])) { |
| 102 | + $query->where($foreignKey, $arguments[0]); |
| 103 | + } |
| 104 | + |
| 105 | + return $query; |
| 106 | + } |
| 107 | + } |
| 108 | + throw new BadMethodCallException("Relation or method '{$method}' not defined in " . static::class); |
| 109 | + } |
| 110 | + |
| 111 | + protected function hideFields($result) |
| 112 | + { |
| 113 | + $hidden = static::$hidden; |
| 114 | + if (empty($hidden)) { |
| 115 | + return $result; |
| 116 | + } |
| 117 | + if ($result instanceof \Illuminate\Support\Collection || is_array($result)) { |
| 118 | + return collect($result)->map(function ($item) use ($hidden) { |
| 119 | + foreach ($hidden as $field) { |
| 120 | + if (is_array($item) && array_key_exists($field, $item)) { |
| 121 | + unset($item[$field]); |
| 122 | + } elseif (is_object($item) && property_exists($item, $field)) { |
| 123 | + unset($item->$field); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return $item; |
| 128 | + }); |
| 129 | + } |
| 130 | + if (is_object($result) || is_array($result)) { |
| 131 | + foreach ($hidden as $field) { |
| 132 | + if (is_array($result) && array_key_exists($field, $result)) { |
| 133 | + unset($result[$field]); |
| 134 | + } elseif (is_object($result) && property_exists($result, $field)) { |
| 135 | + unset($result->$field); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return $result; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Get all records. |
| 145 | + */ |
| 146 | + public function all(): Collection |
| 147 | + { |
| 148 | + $results = $this->model->all(); |
| 149 | + |
| 150 | + return $this->hideFields($results); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Find a record by ID. |
| 155 | + */ |
| 156 | + public function find(int|string $id): ?Model |
| 157 | + { |
| 158 | + $result = $this->model->where(static::$primaryKey, $id)->first(); |
| 159 | + |
| 160 | + return $this->hideFields($result); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Create a new record. |
| 165 | + */ |
| 166 | + public function create(array $data): Model |
| 167 | + { |
| 168 | + return $this->model->create($data); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Update a record by ID. |
| 173 | + */ |
| 174 | + public function update(int|string $id, array $data): ?Model |
| 175 | + { |
| 176 | + $model = $this->find($id); |
| 177 | + if ($model) { |
| 178 | + $model->update($data); |
| 179 | + |
| 180 | + return $model; |
| 181 | + } |
| 182 | + |
| 183 | + return null; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Delete a record by ID. |
| 188 | + */ |
| 189 | + public function delete(int|string $id): bool |
| 190 | + { |
| 191 | + return (bool) $this->model->where(static::$primaryKey, $id)->delete(); |
| 192 | + } |
| 193 | + |
| 194 | + public function paginate(int $perPage = 15): LengthAwarePaginator |
| 195 | + { |
| 196 | + return $this->model->paginate($perPage); |
| 197 | + } |
| 198 | + |
| 199 | + public function findBy(string $field, mixed $value): ?Model |
| 200 | + { |
| 201 | + return $this->model->where($field, $value)->first(); |
| 202 | + } |
| 203 | + |
| 204 | + // Utility methods are now available via RepositoryService: |
| 205 | + // - RepositoryService::map($results, fn($item) => ...) |
| 206 | + // - RepositoryService::mapWithKeys($results, fn($item) => ...) |
| 207 | + // - RepositoryService::modifyFields($results, ['field' => fn($v, $item) => ...]) |
| 208 | + // - RepositoryService::runOnConnection('mysql2', fn($db) => ...) |
| 209 | + // - RepositoryService::crossConnectionQuery(...) |
| 210 | +} |
0 commit comments