|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JeroenG\LaravelPhotoGallery\Adapters\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use JeroenG\LaravelPhotoGallery\Models\Photo; |
|
7
|
|
|
use JeroenG\LaravelPhotoGallery\Entities as Entity; |
|
8
|
|
|
use JeroenG\LaravelPhotoGallery\Contracts\PhotoAdapter; |
|
9
|
|
|
|
|
10
|
|
|
class EloquentPhotoAdapter implements PhotoAdapter |
|
11
|
|
|
{ |
|
12
|
|
|
public function all() |
|
13
|
|
|
{ |
|
14
|
|
|
$collection = []; |
|
15
|
|
|
$all = Photo::all(); |
|
16
|
|
|
foreach ($all as $photo) { |
|
17
|
|
|
$collection[$photo->id] = $this->fromEloquent($photo); |
|
18
|
|
|
} |
|
19
|
|
|
return Collection::make($collection); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function find($id) |
|
23
|
|
|
{ |
|
24
|
|
|
$photo = Photo::findOrFail($id); |
|
25
|
|
|
if($photo) return $this->fromEloquent($photo); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function findHidden($id) |
|
29
|
|
|
{ |
|
30
|
|
|
$photo = Photo::onlyTrashed()->where('id', $id)->get(); |
|
31
|
|
|
if($photo) return $this->fromEloquent($photo); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function findByAlbumId($albumId) |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->findByAttribute(['album_id' => $albumId]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function findByAttribute(array $attribute) |
|
40
|
|
|
{ |
|
41
|
|
|
$collection = []; |
|
42
|
|
|
$all = Photo::where(function($query) use ($attribute) { |
|
43
|
|
|
foreach ($attribute as $att => $value) { |
|
44
|
|
|
$query->where($att, $value); |
|
45
|
|
|
} |
|
46
|
|
|
})->get(); |
|
47
|
|
|
foreach ($all as $photo) { |
|
48
|
|
|
$collection[$photo->id] = $this->fromEloquent($photo); |
|
49
|
|
|
} |
|
50
|
|
|
return Collection::make($collection); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function add(Entity\Photo $photo) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->save($photo); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function update(Entity\Photo $photo) |
|
59
|
|
|
{ |
|
60
|
|
|
$data = $photo->toArray(); |
|
61
|
|
|
$photo = Photo::find($data['id']); |
|
62
|
|
|
$photo->name = $data['name']; |
|
63
|
|
|
$photo->description = $data['description']; |
|
64
|
|
|
$photo->order = $data['order']; |
|
65
|
|
|
$photo->album_id = $data['album_id']; |
|
66
|
|
|
return $photo->save(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function save(Entity\Photo $photo) |
|
70
|
|
|
{ |
|
71
|
|
|
$data = $photo->toArray(); |
|
72
|
|
|
if(array_key_exists('id', $data)) { |
|
73
|
|
|
return $this->update($photo); |
|
74
|
|
|
} else { |
|
75
|
|
|
return $this->toEloquent($data)->save(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function toEloquent($data) |
|
80
|
|
|
{ |
|
81
|
|
|
$photo = new Photo; |
|
82
|
|
|
foreach ($data as $key => $value) { |
|
83
|
|
|
$photo->$key = $value; |
|
84
|
|
|
} |
|
85
|
|
|
return $photo; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function fromEloquent(Photo $photo) |
|
89
|
|
|
{ |
|
90
|
|
|
$entity = new Entity\Photo(); |
|
91
|
|
|
$entity->map([ |
|
92
|
|
|
'id' => $photo->id, |
|
93
|
|
|
'name' => $photo->name, |
|
94
|
|
|
'description' => $photo->description, |
|
95
|
|
|
'file' => $photo->file, |
|
96
|
|
|
'size' => $photo->size, |
|
97
|
|
|
'album_id' => $photo->album_id, |
|
98
|
|
|
]); |
|
99
|
|
|
return $entity; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function hide(Entity\Photo $photo) |
|
103
|
|
|
{ |
|
104
|
|
|
return Photo::where('id', $photo->getId())->delete(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function restore(Entity\Photo $photo) |
|
108
|
|
|
{ |
|
109
|
|
|
return Photo::withTrashed()->where('id', $photo->getId())->restore(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function delete(Entity\Photo $photo) |
|
113
|
|
|
{ |
|
114
|
|
|
return Photo::withTrashed()->where('id', $photo->getId())->forceDelete(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |