Completed
Push — master ( efa9b4...b05f68 )
by wen
05:55
created

File::mutateValueAsPath()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 0
cts 19
cp 0
rs 8.5906
cc 5
eloc 12
nc 1
nop 0
crap 30
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
/**
11
 * Class File
12
 *
13
 * @package Sco\Admin\Form\Elements
14
 */
15
class File extends BaseFile
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $type = 'file';
21
22
    protected $cast = 'json';
23
24
    /**
25
     * @var bool
26
     */
27
    protected $multiSelect = false;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $showFileList = true;
33
34
    /**
35
     * @var int
36
     */
37
    protected $fileUploadsLimit = 0;
38
39
    /**
40
     * @return bool
41
     */
42
    public function isMultiSelect()
43
    {
44
        return $this->multiSelect;
45
    }
46
47
    /**
48
     * @return $this
49
     */
50
    public function enableMultiSelect()
51
    {
52
        $this->multiSelect = true;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function isShowFileList()
61
    {
62
        return $this->showFileList;
63
    }
64
65
    /**
66
     * Disable file list
67
     *
68
     * @return $this
69
     */
70
    public function disableFileList()
71
    {
72
        $this->showFileList = false;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return \Illuminate\Config\Repository|mixed
79
     */
80
    protected function getDefaultExtensions()
81
    {
82
        return config('admin.upload.extensions.file');
83
    }
84
85
    /**
86
     * @return int
87
     */
88
    public function getFileUploadsLimit()
89
    {
90
        return $this->fileUploadsLimit;
91
    }
92
93
    /**
94
     * The maximum number of files that can be uploaded.
95
     *
96
     * @param int $value
97
     *
98
     * @return $this
99
     */
100
    public function setFileUploadsLimit(int $value)
101
    {
102
        $this->fileUploadsLimit = $value;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return array|mixed|static
109
     */
110
    public function getValue()
111
    {
112
        $value = parent::getValue();
113
        if (empty($value)) {
114
            return [];
115
        }
116
117
        return collect($value)->filter(function ($item) {
118
            return $this->existsFile($item);
119
        })->map(function ($item) {
120
            return $this->getFileInfo($item);
121
        });
122
    }
123
124
    protected function mutateValueAsPath()
125
    {
126
        $this->setMutator(function ($value) {
127
            if (empty($value) || ! is_array($value)) {
128
                $value = [];
129
            }
130
131
            $paths = collect($value)->pluck('path');
132
133
            if ($this->isJsonCastable()) {
134
                return $this->castValueAsJson($paths);
135
            }
136
137
            if ($this->isCommaCastable()) {
138
                return $this->castValueAsCommaSeparated($paths);
139
            }
140
141
            throw new \InvalidArgumentException(sprintf(
142
                '%s value is invalid',
143
                get_class($this)
144
            ));
145
        });
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function toArray()
152
    {
153
        return parent::toArray() + [
154
                'showFileList'     => $this->isShowFileList(),
155
                'multiSelect'      => $this->isMultiSelect(),
156
                'fileUploadsLimit' => $this->getFileUploadsLimit(),
157
            ];
158
    }
159
}
160