Skip to content

Commit a2fb2de

Browse files
author
Fredrick Peter
committed
Method changed from filterError to filter()
1 parent bfc708f commit a2fb2de

File tree

8 files changed

+279
-211
lines changed

8 files changed

+279
-211
lines changed

README.md

Lines changed: 149 additions & 95 deletions
Large diffs are not rendered by default.

src/File.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,15 @@ public function hasError()
339339
/**
340340
* Get First Element of Uploads
341341
*
342-
* @return array|null
342+
* @param string|null $mode
343+
*
344+
* @return array|string|null
343345
*/
344-
public function first()
346+
public function first($mode = null)
345347
{
346-
return self::getUploads($this->uploads, true);
348+
$data = self::getUploads($this->uploads, true);
349+
350+
return $data[$mode] ?? $data;
347351
}
348352

349353
/**

src/Methods/FileMethod.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@
1010

1111
abstract class FileMethod{
1212

13+
/**
14+
* Check if File Input is set
15+
*
16+
* @param string $name
17+
* @return bool
18+
*/
19+
static public function fileIsset($name)
20+
{
21+
if(isset($_FILES[$name])){
22+
return true;
23+
}
24+
25+
return false;
26+
}
27+
1328
/**
1429
* Check if File Input is not empty
1530
*

src/Traits/FileTrait.php

Lines changed: 75 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tamedevelopers\File\Traits;
66

7+
use Tamedevelopers\Support\Capsule\Forge;
78
use Tamedevelopers\Support\Tame;
89

910
/**
@@ -42,107 +43,101 @@ private function setDirectory($path = null)
4243
*
4344
* @param array $message
4445
* @param array $config
45-
* @param array $filterError
4646
* @return void
4747
*/
48-
public function globalConfig($message = [], $config = [], $filterError = [])
48+
public function globalConfig($message = [], $config = [])
4949
{
50-
// create message
51-
$message = $message + [
52-
'401' => 'Select file to upload',
53-
'402' => 'File upload is greater than allowed size of:',
54-
'403' => 'Maximum file upload exceeded. Limit is:',
55-
'404' => 'Uploaded file format not allowed. Allowed formats:',
56-
'405' => 'Image dimension allowed is:',
57-
'405x' => 'Image dimension should be greater than or equal to:',
58-
'200' => 'File uploaded successfully:',
59-
'kb' => 'kb',
60-
'mb' => 'mb',
61-
'gb' => 'gb',
62-
'and' => 'and',
63-
'width' => 'width',
64-
'height'=> 'height',
65-
'files' => 'files',
66-
'file' => 'file',
67-
];
68-
69-
// create config
70-
$config = array_merge([
71-
'limit' => 1,
72-
'size' => 2097152, // 2mb
73-
'mime' => 'images', // video|audio|files|images|general_image|general_media|general_file
74-
'baseUrl' => domain(),
75-
'baseDir' => base_path(),
76-
'driver' => 'local', // local|s3
77-
'structure' => 'default', // default|year|month|day
78-
'generate' => true, // will always generate a unique() name for each uploaded file
79-
], $config);
80-
81-
82-
// Convert size to Bytes
83-
$config['size'] = Tame::sizeToBytes(
84-
!empty($config['size']) && (int) $config['size'] >= 1024
85-
? Tame::byteToUnit($config['size'])
86-
: $config['size'] ?? '2mb'
87-
);
88-
89-
// create filterError
90-
$filterError = array_merge([], $filterError);
91-
92-
// trim any leading '\/' and manually add by ourselves
93-
// this enable to make sure, paths are with a leading '/'
94-
$config['baseDir'] = trim((string) $config['baseDir'], '\/') . '/';
95-
$config['baseUrl'] = trim((string) $config['baseUrl'], '\/') . '/';
96-
97-
// check for valid driver type
98-
// only change the default driver if found
99-
if(in_array($config['driver'], array_keys($this->driverTypes))){
100-
$config['driver'] = $config['driver'];
101-
}
102-
103-
// create default data
104-
$default = [
105-
'message' => $message,
106-
'config' => $config,
107-
'filterError' => $filterError,
108-
];
50+
// define constant to hold global error handler
51+
if(!defined('TAME_FILE_ERROR')){
10952

110-
// if filter error is defined
111-
if(!empty($default['filterError'])){
53+
// create message
54+
$message = $message + [
55+
'401' => 'Select file to upload',
56+
'402' => 'File upload is greater than allowed size of:',
57+
'403' => 'Maximum file upload exceeded. Limit is:',
58+
'404' => 'Uploaded file format not allowed. Allowed formats:',
59+
'405' => 'Image dimension allowed is:',
60+
'405x' => 'Image dimension should be greater than or equal to:',
61+
'200' => 'File uploaded successfully:',
62+
'kb' => 'kb',
63+
'mb' => 'mb',
64+
'gb' => 'gb',
65+
'and' => 'and',
66+
'width' => 'width',
67+
'height'=> 'height',
68+
'files' => 'files',
69+
'file' => 'file',
70+
];
71+
72+
// create config
73+
$config = array_merge([
74+
'limit' => 1,
75+
'size' => 2097152, // 2mb
76+
'mime' => 'images', // video|audio|files|images|general_image|general_media|general_file
77+
'baseUrl' => domain(),
78+
'baseDir' => base_path(),
79+
'driver' => 'local', // local|s3
80+
'structure' => 'default', // default|year|month|day
81+
'generate' => true, // will always generate a unique() name for each uploaded file
82+
], $config);
83+
84+
85+
// Convert size to Bytes
86+
$config['size'] = Tame::sizeToBytes(
87+
!empty($config['size']) && (int) $config['size'] >= 1024
88+
? Tame::byteToUnit($config['size'])
89+
: $config['size'] ?? '2mb'
90+
);
91+
92+
// trim any leading '\/' and manually add by ourselves
93+
// this enable to make sure, paths are with a leading '/'
94+
$config['baseDir'] = trim((string) $config['baseDir'], '\/') . '/';
95+
$config['baseUrl'] = trim((string) $config['baseUrl'], '\/') . '/';
96+
97+
// check for valid driver type
98+
// only change the default driver if found
99+
if(in_array($config['driver'], array_keys($this->driverTypes))){
100+
$config['driver'] = $config['driver'];
101+
}
112102

113-
// filter error to be remove from parent $this->error
114-
$this->filterError($default['filterError']);
115-
}
103+
// create default data
104+
$default = [
105+
'message' => $message,
106+
'config' => $config,
107+
];
116108

117-
// define constant to hold global error handler
118-
if(!defined('TAME_FILE_ERROR')){
119109
define('TAME_FILE_ERROR', $default);
120110
}
121111
}
122112

123113
/**
124-
* Filter error to remove unwanted error response
114+
* Remove unwanted Filter response status code
125115
*
126-
* @param array $errorDisallowed
127-
* 401 => ERROR_401 - Select file to upload",
128-
* 402 => ERROR_402 - File upload is greater than allowed size of",
129-
* 403 => ERROR_403 - Maximum file upload exceeded. Limit is:",
130-
* 404 => ERROR_404 - Uploaded file format not allowed. Allowed format is:",
131-
* 405 => ERROR_405 - Image size allowed error"
132-
* 200 => ERROR_200 - File uploaded successfully"
116+
* @param array $filter
117+
* Possible status code: [401, 402, 403, 404, 405, 200]
118+
*
119+
* 401 => Select file to upload",
120+
* 402 => File upload is greater than allowed size of",
121+
* 403 => Maximum file upload exceeded. Limit is:",
122+
* 404 => Uploaded file format not allowed. Allowed format is:",
123+
* 405 => Image size allowed error"
124+
* 200 => File uploaded successfully"
133125
*
134126
* @return $this
135127
*/
136-
public function filterError($errorDisallowed = [])
128+
public function filter(...$filter)
137129
{
138-
if(is_array($errorDisallowed) && count($errorDisallowed) > 0){
130+
// flattern all into one array element
131+
$filter = Forge::flattenValue($filter);
132+
133+
if(is_array($filter) && count($filter) > 0){
139134

140-
foreach($errorDisallowed as $value){
135+
foreach($filter as $value){
141136
// convert to int values
142137
$value = (int) $value;
143138

144139
// if in error array keys
145-
if(in_array($value, array_keys($this->error) )){
140+
if( in_array($value, array_keys($this->error)) && $value !== 200 ){
146141
unset($this->error[$value]);
147142
}
148143
}

src/Traits/FileValidatorTrait.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ private function proceedToValidate()
7474
$this->callConfigIfNotCalled();
7575

7676
/**
77-
* Select file to upload. - error 401
77+
* When form has not been submitted, request will be empty
7878
*/
7979
if(self::isEmpty($this->name))
8080
{
81-
if(isset($this->error['401'])){
81+
/**
82+
* Select file to upload. - error 401
83+
*/
84+
if(self::fileIsset($this->name) && isset($this->error['401'])){
8285
$this->data = [
8386
'message' => $this->translation(401),
8487
'status' => 401,
@@ -88,7 +91,7 @@ private function proceedToValidate()
8891
}
8992
}
9093

91-
// begin multple validation
94+
// begin file validation
9295
else{
9396
$validationAttempt = true;
9497
foreach($this->fileItemsData() as $key => $file){
@@ -234,13 +237,6 @@ private function callConfigIfNotCalled()
234237
} else{
235238
// merge with default data
236239
$this->config = $this->dataMerge();
237-
238-
// if filter error is defined
239-
if(!empty(TAME_FILE_ERROR['filterError'])){
240-
241-
// filter error to be remove from parent $this->error
242-
$this->filterError(TAME_FILE_ERROR['filterError']);
243-
}
244240
}
245241
}
246242

src/helpers.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ function TameFile()
2323
*
2424
* @param array $message
2525
* @param array $config
26-
* @param array $filterError
2726
*
2827
* @return void
2928
*/
30-
function FileConfig($message = [], $config = [], $filterError = [])
29+
function FileConfig($message = [], $config = [])
3130
{
32-
(new File)->globalConfig($message, $config, $filterError);
31+
(new File)->globalConfig($message, $config);
3332
}
3433
}

test/config.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
'driver' => 'local',
3232
'structure' => 'default', // default|year|month|day
3333
'generate' => true, // will always generate a unique() name for each uploaded file
34-
],
35-
filterError: []
34+
]
3635
);
3736

test/index1.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,30 @@
1414
FileConfig(
1515
config: [
1616
'size' => '3mb'
17-
],
17+
]
1818
);
1919

2020
// easy as this
21-
File::name('avatar')
22-
->validate()
23-
->save();
21+
// File::name('avatar')
22+
// ->filter([401, 403, 500])
23+
// ->validate()
24+
// ->save();
2425

2526

26-
// or more methods chaining
27-
File::name('avatar2')
28-
->limit(3)
29-
->mime('files')
30-
// ->driver('s3')
31-
->folder('public/images')
32-
->validate()
33-
->save();
27+
// // or more methods chaining
28+
// File::name('avatar2')
29+
// ->limit(3)
30+
// ->mime('files')
31+
// // ->driver('s3')
32+
// ->folder('public/images')
33+
// ->validate()
34+
// ->save();
3435

3536

3637
// closure/callable on save() and validate()
3738
$upload = File::name('banners')
38-
->size('50kb')
39-
->filterError([401])
39+
->size('500kb')
40+
// ->filter(401)
4041
->validate()
4142
->save(function($response){
4243

@@ -61,7 +62,7 @@
6162
// ->driver('s3')
6263
->save();
6364

64-
dump(
65+
dd(
6566
$file->getMessage(),
6667
$file->getStatus(),
6768
$file->first(),
@@ -89,6 +90,11 @@
8990
<?php if($upload->hasError()) {?>
9091
<div style="background: #f7b9b9; margin: 0 auto 50px; width: 100%; max-width: 600px; padding: 20px; font-size: 18px">
9192
<?= $upload->getMessage(); ?>
93+
<br>
94+
95+
<a href="<?= $upload->first('url'); ?>" target="_blank">
96+
Preview Data
97+
</a>
9298
</div>
9399
<?php } ?>
94100

@@ -98,15 +104,15 @@
98104
<div class="form-group">
99105
<label for="upload">Image Avatar</label>
100106
<input type="file" class="form-control-file" id="upload"
101-
name="avatar" multiple>
107+
name="avatar">
102108
</div>
103109
</div>
104110

105111
<!--file upload-->
106112
<div class="col-sm-12 mt-3">
107113
<div class="form-group">
108-
<label for="upload">Banners</label>
109-
<input type="file" class="form-control-file" id="upload"
114+
<label for="upload2">Banners</label>
115+
<input type="file" class="form-control-file" id="upload2"
110116
name="banners[]" multiple>
111117
</div>
112118
</div>

0 commit comments

Comments
 (0)