Skip to content

Commit bda9393

Browse files
author
Fredrick Peter
committed
Validate method now optional and not mandatory
1 parent 3f25535 commit bda9393

File tree

12 files changed

+194
-74
lines changed

12 files changed

+194
-74
lines changed

README.md

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
* [Requirements](#requirements)
1212
* [Installation](#installation)
1313
* [Instantiate](#instantiate)
14-
* [Amazon Aws Dump](#amazon-aws-dump)
14+
* [Amazon Aws S3](#amazon-aws-s3)
1515
* [Global Config](#global-config)
1616
* [Response Data](#response-data)
1717
* [Get Message](#get-message)
1818
* [Get Status](#get-status)
19+
* [Get Class](#get-class)
1920
* [First](#first)
2021
* [Get](#get)
2122
* [Usage](#usage)
@@ -81,7 +82,8 @@ $file = new Tamedevelopers\File\File();
8182
$file = TameFile();
8283
```
8384

84-
## Amazon Aws Dump
85+
## Amazon Aws S3
86+
- To use s3 first require `composer require aws/aws-sdk-php`
8587
- By default Aws load entire SDK we won't be needing
8688
- Copy the below code into your root `composer.json` and then run `composer update` in terminal.
8789

@@ -102,10 +104,11 @@ $file = TameFile();
102104
- Define as `named argument`
103105

104106

105-
| Key | Types | Description |
106-
|---------------|-------------------|---------------------------------------------------|
107-
| message | Assoc `array` | Create all error message in different language |
108-
| config | Assoc `array` | Create all needed config data |
107+
| Key | Types | Description |
108+
|---------------|-------------------|-----------------------------------|
109+
| message | Assoc `array` | Create all error messages |
110+
| config | Assoc `array` | Create all needed config data |
111+
| class | Assoc `array` | Create error and success class |
109112

110113
```config
111114
FileConfig(
@@ -135,6 +138,10 @@ FileConfig(
135138
'driver' => 'local',
136139
'structure' => 'default', // default|year|month|day
137140
'generate' => true, // will always generate a unique() name for each uploaded file
141+
],
142+
class: [
143+
'error' => 'bg-danger',
144+
'success' => 'bg-success',
138145
]
139146
);
140147
```
@@ -144,7 +151,6 @@ FileConfig(
144151

145152
### Get Message
146153
- This will return error message
147-
148154
```
149155
$file = File::name('html_input_name');
150156
@@ -153,13 +159,20 @@ $file->getMessage();
153159

154160
### Get Status
155161
- This will return error status code
156-
157162
```
158163
$file = File::name('html_input_name');
159164
160165
$file->getStatus();
161166
```
162167

168+
### Get Class
169+
- This will only return msg, if there's an error or success
170+
```
171+
$file = File::name('html_input_name');
172+
173+
$file->getClass();
174+
```
175+
163176
### First
164177
- This will get the first uploaded data
165178
- You can pass and [optional] param as string `name` \| `url`
@@ -174,9 +187,7 @@ $file->getStatus();
174187

175188
- `or`
176189
```
177-
$upload = File::name('avatar')
178-
->validate()
179-
->save();
190+
$upload = File::name('avatar')->save();
180191
181192
$upload->first('url);
182193
$upload->first('name);
@@ -196,7 +207,6 @@ $upload->first('name);
196207
- `or`
197208
```
198209
$upload = File::name('avatar')
199-
->validate()
200210
->save();
201211
202212
$upload->first();
@@ -348,7 +358,6 @@ File::name('avatar')
348358
| general_media | `['audio/mpeg','audio/x-wav', 'video/mp4','video/mpeg','video/quicktime','video/x-msvideo','video/x-ms-wmv']` |
349359
| general_file | `['application/msword','application/pdf','text/plain','application/zip', 'application/x-zip-compressed', 'multipart/x-zip','application/x-zip-compressed' 'application/x-rar-compressed', 'application/octet-stream', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']`|
350360

351-
352361
```
353362
File::name('avatar')
354363
->mime('images');
@@ -378,35 +387,28 @@ File::name('avatar')
378387
```
379388

380389
### Validate
381-
- Takes an [optional] param as a `callable\|closure` function.
382-
- The method needs to be called to validate upload errors before saving
390+
- [optional] Method can be use, to return error message.
391+
- Takes an [optional] param as a `callable\|closure` function.
383392

384-
```
385-
File::name('banner')
386-
->folder('upload/banner')
387-
->width(700)
388-
->height(400)
389-
->validate();
390-
```
391393

392-
- `or`
393394
```
394395
File::name('banner')
395396
->folder('upload/banner')
396397
->validate(function($response){
397398
398399
// perform any other needed task in here
400+
echo $response->getMessage();
401+
return;
399402
});
400403
```
401404

402405
### Save
403406
- Takes an [optional] param as a `callable\|closure` function.
404-
- Calling this [method] will automatically save uploaded data on `success`.
407+
- Calling this [method] will automatically save the data.
405408

406409
```
407410
File::name('banner')
408411
->folder('upload/banner')
409-
->validate()
410412
->save(function($response){
411413
412414
// perform any other needed task in here
@@ -417,7 +419,6 @@ File::name('banner')
417419
```
418420
$file = File::name('banner')
419421
->folder('upload/banner')
420-
->validate()
421422
->save();
422423
423424
dd(
@@ -433,7 +434,6 @@ dd(
433434
```
434435
File::name()
435436
->folder('upload/banner')
436-
->validate('avatar')
437437
->save(function($response){
438438
439439
// perform resize
@@ -459,7 +459,6 @@ File::name()
459459
```
460460
File::name('avatar')
461461
->folder('upload/banner')
462-
->validate()
463462
->save(function($response){
464463
465464
// perform watermark
@@ -473,7 +472,6 @@ File::name('avatar')
473472
```
474473
File::name('avatar')
475474
->folder('upload/banner')
476-
->validate()
477475
->save(function($response){
478476
479477
// perform compressor

composer.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
],
1717
"require": {
1818
"php": ">=8.0",
19-
"tamedevelopers/support": "^1.0.2",
20-
"aws/aws-sdk-php": "^3.282.0"
19+
"tamedevelopers/support": "^1.0.2"
2120
},
2221
"suggest": {
2322
"aws/aws-sdk-php": "Required to use Amazon `s3` composer require aws/aws-sdk-php (^3.282.0)."
@@ -30,14 +29,5 @@
3029
"src/helpers.php"
3130
]
3231
},
33-
"scripts": {
34-
"pre-autoload-dump": "Aws\\Script\\Composer\\Composer::removeUnusedServices"
35-
},
36-
"extra": {
37-
"aws/aws-sdk-php": [
38-
"Ec2",
39-
"CloudWatch"
40-
]
41-
},
4232
"minimum-stability": "stable"
4333
}

src/File.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static public function name($name = null)
5858
self::convertToFileHelper()
5959
);
6060
}
61-
61+
6262
/**
6363
* Begin Upload Validation
6464
*
@@ -83,6 +83,8 @@ public function validate(callable $function = null)
8383
*/
8484
public function save(callable $function = null)
8585
{
86+
$this->ignoreIfValidatorHasBeenCalled();
87+
8688
if($this->success){
8789
if(empty($this->folder)){
8890
$this->folder = 'public/images';
@@ -320,6 +322,30 @@ public function getStatus()
320322
return (int) $this->data['status'];
321323
}
322324

325+
/**
326+
* Get Class Error
327+
*
328+
* @return string
329+
*/
330+
public function getClass()
331+
{
332+
if($this->hasError()){
333+
return $this->class['error'];
334+
}
335+
336+
return $this->success ? $this->class['success'] : null;
337+
}
338+
339+
/**
340+
* Get Uploader name
341+
*
342+
* @return string|null
343+
*/
344+
public function getName()
345+
{
346+
return $this->name;
347+
}
348+
323349
/**
324350
* Check if upload has an error
325351
*

src/FileStorage.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Tamedevelopers\File;
44

5+
use Exception;
6+
use Tamedevelopers\Support\Env;
57
use Tamedevelopers\Support\Tame;
68
use Tamedevelopers\File\Methods\FileMethod;
79
use Tamedevelopers\File\Traits\CommonTrait;
10+
use Tamedevelopers\Support\Capsule\Manager;
811
use Tamedevelopers\File\Traits\FileStorageTrait;
12+
use Tamedevelopers\Support\Capsule\CustomException;
913

1014
class FileStorage extends FileMethod{
1115

@@ -83,8 +87,16 @@ public function handle()
8387

8488
// check if driver is local
8589
if(!$this->isLocalDriver()){
90+
// driver
91+
$driver = $this->config['driver'];
92+
93+
// validate if package is installed
94+
$this->isDriverPackageInstalled(
95+
$this->getCloudAssociateClass($driver)
96+
);
97+
8698
// initialize third-party bucket
87-
$bucket = new $this->driverTypes[$this->config['driver']]();
99+
$bucket = new $this->driverTypes[$driver]();
88100

89101
// get filename without extension
90102
$fileName = pathinfo($migrate['name'], PATHINFO_FILENAME);
@@ -133,4 +145,53 @@ private function migrate()
133145
]);
134146
}
135147

148+
149+
/**
150+
* Get Class Associated with drivers, for selected Cloud storage
151+
*
152+
* @param string|null $mode
153+
* @return void
154+
*/
155+
private function getCloudAssociateClass($mode = null)
156+
{
157+
return [
158+
's3' => [
159+
'exists' => class_exists('Aws\S3\S3Client'),
160+
'message' => "Class Aws\S3\S3Client not found: \nRequire the package by running: `composer require aws/aws-sdk-php`\n",
161+
],
162+
][$mode] ?? [
163+
'exists' => false,
164+
'message' => "Unkown Driver Bucket Selected",
165+
];
166+
}
167+
168+
/**
169+
* Check If Driver Package has been installed
170+
*
171+
* @param mixed $class
172+
* @return void
173+
*/
174+
static private function isDriverPackageInstalled($data = '')
175+
{
176+
try {
177+
if (!$data['exists']) {
178+
throw new CustomException(
179+
$data['message'] . (new Exception)->getTraceAsString()
180+
);
181+
}
182+
} catch (CustomException $e) {
183+
// Handle the exception silently (turn off error reporting)
184+
error_reporting(0);
185+
186+
Manager::setHeaders(404, function() use($e){
187+
188+
// create error logger
189+
Env::bootLogger();
190+
191+
// Trigger a custom error
192+
trigger_error($e->getMessage(), E_USER_ERROR);
193+
});
194+
}
195+
}
196+
136197
}

src/ImageWatermark.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private function applyWaterMark($imageSource, $watermarkSource, $position, $sour
118118
}
119119

120120
// Add the watermark to the source image.
121-
if (!imagecopy($imageSource, $watermarkSource, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height)) {
121+
if (!imagecopy($imageSource, $watermarkSource, (int) $watermark_x, (int) $watermark_y, 0, 0, (int) $watermark_width, (int) $watermark_height)) {
122122
return false;
123123
}
124124

src/Traits/CommonTrait.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
namespace Tamedevelopers\File\Traits;
44

5-
/**
6-
* @property mixed $config
7-
*/
5+
86
trait CommonTrait{
97

8+
/**
9+
* Only ignore if validate metho, has been manually called
10+
*
11+
* @return mixed
12+
*/
13+
public function ignoreIfValidatorHasBeenCalled()
14+
{
15+
if(!$this->isValidatedCalled){
16+
$this->validate();
17+
}
18+
}
19+
1020
/**
1121
* Check if is local driver
1222
*

0 commit comments

Comments
 (0)