Completed
Push — master ( d9e017...cb8e5c )
by wen
04:31
created

File::getFileExts()   A

Complexity

Conditions 2
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 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
class File extends Element
6
{
7
    protected $type = 'file';
8
9
    protected $actionUrl;
10
11
    protected $multiple = false;
12
13
    protected $showFileList = true;
14
15
    protected $withCredentials = false;
16
17
    protected $fileSizeLimit = 0;
18
19
    protected $fileUploadsLimit = 0;
20
21
    protected $fileExts;
22
23
    public function getValue()
24
    {
25
        $value = parent::getValue();
26
        if (empty($value)) {
27
            return [];
28
        }
29
30
        return [
31
            [
32
                'name' => '',
33
                'url'  => asset('vendor/admin/images/1200.jpg'),
34
            ],
35
        ];
36
    }
37
38
    public function getActionUrl()
39
    {
40
        if ($this->actionUrl) {
41
            return $this->actionUrl;
42
        }
43
        $model = app('admin.components')->get(get_class($this->getModel()));
44
45
        $params       = [
46
            'model' => $model->getName(),
47
            'field' => $this->getName(),
48
        ];
49
        $params['id'] = null;
50
        if ($this->getModel()->exists) {
51
            $params['id'] = $this->getModel()->getKey();
52
        }
53
        $params['_token'] = csrf_token();
54
55
        return route('admin.model.upload.file', $params);
56
    }
57
58
    public function setActionUrl($value)
59
    {
60
        $this->actionUrl = $value;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Allow multiple selection files
67
     *
68
     * @return $this
69
     */
70
    public function isMultiple()
71
    {
72
        $this->multiple = true;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Show file list
79
     *
80
     * @return $this
81
     */
82
    public function disableFileList()
83
    {
84
        $this->showFileList = false;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Indicates whether or not cross-site Access-Control requests
91
     * should be made using credentials
92
     *
93
     * @return $this
94
     */
95
    public function withCredentials()
96
    {
97
        $this->withCredentials = true;
98
99
        return $this;
100
    }
101
102
    /**
103
     * The maximum size allowed for a file upload. (KB)
104
     *
105
     * @param int $value
106
     *
107
     * @return $this
108
     */
109
    public function setFileSizeLimit($value)
110
    {
111
        $this->fileSizeLimit = intval($value);
112
        return $this;
113
    }
114
115
    public function getFileExts()
116
    {
117
        if ($this->fileExts) {
118
            return $this->fileExts;
119
        }
120
121
        return config('admin.defaultFileExts');
122
    }
123
124
    /**
125
     * A list of allowable extensions that can be uploaded.
126
     *
127
     * @param array|string $value
128
     *
129
     * @return $this
130
     */
131
    public function setFileExts($value)
132
    {
133
        $this->fileExts = is_array($value) ? $value : explode(',', $value);
134
135
        return $this;
136
    }
137
138
    /**
139
     * The maximum number of files that can be uploaded.
140
     *
141
     * @param int $value
142
     *
143
     * @return $this
144
     */
145
    public function setFileUploadsLimit($value)
146
    {
147
        $this->fileUploadsLimit = intval($value);
148
149
        return $this;
150
    }
151
152
    public function toArray()
153
    {
154
        return parent::toArray() + [
155
                'action'           => $this->getActionUrl(),
156
                'showFileList'     => $this->showFileList,
157
                'multiple'         => $this->multiple,
158
                'fileSizeLimit'    => $this->fileSizeLimit,
159
                'fileUploadsLimit' => $this->fileUploadsLimit,
160
                'fileExts'         => $this->getFileExts(),
161
            ];
162
    }
163
}
164