1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Sco\Admin\Display\Concerns\WithPagination; |
8
|
|
|
use Sco\Admin\Traits\UploadStorageTrait; |
9
|
|
|
|
10
|
|
|
class Image extends Display |
11
|
|
|
{ |
12
|
|
|
use UploadStorageTrait, WithPagination; |
13
|
|
|
|
14
|
|
|
protected $type = 'image'; |
15
|
|
|
|
16
|
|
|
protected $imagePathAttribute; |
17
|
|
|
|
18
|
|
|
public function getImagePathAttribute() |
19
|
|
|
{ |
20
|
|
|
return $this->imagePathAttribute; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setImagePathAttribute($value) |
24
|
|
|
{ |
25
|
|
|
$this->imagePathAttribute = $value; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function get() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
if ($this->isPagination()) { |
33
|
|
|
$data = $this->paginate(); |
34
|
|
|
|
35
|
|
|
return $data->setCollection($this->parseRows($data->getCollection())); |
36
|
|
|
} |
37
|
|
|
return $this->parseRows($this->getQuery()->get()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function parseRows(Collection $rows) |
41
|
|
|
{ |
42
|
|
|
if (is_null($pathKey = $this->getImagePathAttribute())) { |
43
|
|
|
throw new \InvalidArgumentException('Must set image attribute'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $rows->map(function (Model $row) use ($pathKey) { |
47
|
|
|
if (! isset($row->$pathKey)) { |
48
|
|
|
throw new \InvalidArgumentException("Not Found '{$pathKey}' attribute"); |
49
|
|
|
} |
50
|
|
|
$row->setAttribute('_primary', $row->getKey()); |
51
|
|
|
$row->setAttribute('_url', $this->getImageUrl($row->$pathKey)); |
52
|
|
|
|
53
|
|
|
return $row; |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getImageUrl($value) |
58
|
|
|
{ |
59
|
|
View Code Duplication |
if (! empty($value) && (strpos($value, '://') === false)) { |
|
|
|
|
60
|
|
|
$value = $this->getFileUrl($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $value; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.