Completed
Push — master ( 85c58c...2c5e88 )
by wen
10:27
created

File::prepareValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Http\UploadedFile;
6
use Sco\Admin\Facades\Admin;
7
use Sco\Admin\Traits\UploadStorageTrait;
8
use Validator;
9
10
class File extends BaseFile
11
{
12
    protected $type = 'file';
13
14
    protected $multiSelect = false;
15
16
    protected $showFileList = true;
17
18
    protected $fileUploadsLimit = 0;
19
20
    public function isMultiSelect()
21
    {
22
        return $this->multiSelect;
23
    }
24
25
    public function enableMultiSelect()
26
    {
27
        $this->multiSelect = true;
28
29
        return $this;
30
    }
31
32
    public function isShowFileList()
33
    {
34
        return $this->showFileList;
35
    }
36
37
    /**
38
     * Disable file list
39
     *
40
     * @return $this
41
     */
42
    public function disableFileList()
43
    {
44
        $this->showFileList = false;
45
46
        return $this;
47
    }
48
49
    protected function getDefaultExtensions()
50
    {
51
        return config('admin.upload.extensions.file');
52
    }
53
54
    public function getFileUploadsLimit()
55
    {
56
        return $this->fileUploadsLimit;
57
    }
58
59
    /**
60
     * The maximum number of files that can be uploaded.
61
     *
62
     * @param int $value
63
     *
64
     * @return $this
65
     */
66
    public function setFileUploadsLimit(int $value)
67
    {
68
        $this->fileUploadsLimit = $value;
69
70
        return $this;
71
    }
72
73
    public function getValue()
74
    {
75
        $value = $this->getValueFromModel();
76
        if (empty($value)) {
77
            return [];
78
        }
79
80
        return collect(explode(',', $value))->filter(function ($item) {
81
            return $this->existsFile($item);
82
        })->map(function ($item) {
83
            return $this->getFileInfo($item);
84
        });
85
    }
86
87
    protected function prepareValue($value)
88
    {
89
        if (empty($value) || ! is_array($value)) {
90
            return '';
91
        }
92
93
        return collect($value)->implode('path', ',');
94
    }
95
96
    public function toArray()
97
    {
98
        return parent::toArray() + [
99
                'showFileList'     => $this->isShowFileList(),
100
                'multiSelect'      => $this->isMultiSelect(),
101
                'fileUploadsLimit' => $this->getFileUploadsLimit(),
102
            ];
103
    }
104
}
105