|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Cities\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Base\BaseRepository; |
|
6
|
|
|
use App\Shop\Cities\Exceptions\CityNotFoundException; |
|
7
|
|
|
use App\Shop\Cities\Repositories\Interfaces\CityRepositoryInterface; |
|
8
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
9
|
|
|
use App\Shop\Cities\City; |
|
10
|
|
|
use Illuminate\Support\Collection; |
|
11
|
|
|
|
|
12
|
|
|
class CityRepository extends BaseRepository implements CityRepositoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* CityRepository constructor. |
|
16
|
|
|
* |
|
17
|
|
|
* @param City $city |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(City $city) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct($city); |
|
22
|
|
|
$this->model = $city; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param array $columns |
|
27
|
|
|
* @param string $orderBy |
|
28
|
|
|
* @param string $sortBy |
|
29
|
|
|
* |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
public function listCities($columns = ['*'], string $orderBy = 'name', string $sortBy = 'asc') |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->all($columns, $orderBy, $sortBy); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param int $id |
|
39
|
|
|
* @return City |
|
40
|
|
|
* @throws CityNotFoundException |
|
41
|
|
|
* |
|
42
|
|
|
* @deprecated @findCityByName |
|
43
|
|
|
*/ |
|
44
|
|
|
public function findCityById(int $id) : City |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
return $this->findOneOrFail($id); |
|
|
|
|
|
|
48
|
|
|
} catch (ModelNotFoundException $e) { |
|
49
|
|
|
throw new CityNotFoundException('City not found.'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $params |
|
55
|
|
|
* |
|
56
|
|
|
* @return boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
public function updateCity(array $params) : bool |
|
59
|
|
|
{ |
|
60
|
|
|
$this->model->update($params); |
|
61
|
|
|
return $this->model->save(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $state_code |
|
66
|
|
|
* |
|
67
|
|
|
* @return Collection |
|
68
|
|
|
*/ |
|
69
|
|
|
public function listCitiesByStateCode(string $state_code) : Collection |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->model->where(compact('state_code'))->get(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
* @throws CityNotFoundException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function findCityByName(string $name) : City |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
return $this->model->where(compact('name'))->firstOrFail(); |
|
|
|
|
|
|
84
|
|
|
} catch (ModelNotFoundException $e) { |
|
85
|
|
|
throw new CityNotFoundException('City not found.'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|