-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(file-storage): 储存增加上传后的 node 验证规则
- Loading branch information
Showing
5 changed files
with
118 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zhiyi\Plus\FileStorage\Validators\Rulers; | ||
|
||
use Zhiyi\Plus\FileStorage\StorageInterface; | ||
|
||
class FileStorageRuler implements RulersInterface | ||
{ | ||
/** | ||
* The storage. | ||
* @var \Zhiyi\Plus\FileStorage\StorageInterface | ||
*/ | ||
protected $storage; | ||
|
||
/** | ||
* Create the ruler instalce. | ||
* @param Zhiyi\Plus\FileStorage\StorageInterface $storage | ||
*/ | ||
public function __construct(StorageInterface $storage) | ||
{ | ||
$this->storage; | ||
} | ||
|
||
/** | ||
* Rule handler. | ||
* @param array $params | ||
* @return bool | ||
*/ | ||
public function handle(array $params): bool | ||
{ | ||
try { | ||
return (bool) $this->storage | ||
->meta(new Resource($params[1])) | ||
->getSize(); | ||
} finally { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zhiyi\Plus\FileStorage\Validators\Rulers; | ||
|
||
interface RulerInterface | ||
{ | ||
/** | ||
* Rule handler. | ||
* @param array $params | ||
* @return bool | ||
*/ | ||
public function handle(array $params): bool; | ||
} |
56 changes: 56 additions & 0 deletions
56
app/FileStorage/Validators/Rulers/ValidatorRulesRegister.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zhiyi\Plus\FileStorage\Validators\Rulers; | ||
|
||
use Zhiyi\Plus\AppInterface; | ||
use Illuminate\Contracts\Validation\Factory as ValidationFactoryContract; | ||
|
||
class ValidatorRegister | ||
{ | ||
/** | ||
* The app. | ||
* @var \Zhiyi\Plus\AppInterface | ||
*/ | ||
protected $app; | ||
|
||
/** | ||
* The app validator. | ||
* @var \Illuminate\Contracts\Validation\Factory | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* The rulers. | ||
* @var array | ||
*/ | ||
protected $rules = [ | ||
'file_storage' | ||
]; | ||
|
||
/** | ||
* Create the validator rules register instance. | ||
* @param \Zhiyi\Plus\AppInterface $app | ||
* @param \Illuminate\Contracts\Validation\Factory $validator | ||
*/ | ||
public function __construct(AppInterface $app, ValidationFactoryContract $validator) | ||
{ | ||
$this->app = $app; | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* The reguster. | ||
* @return void | ||
*/ | ||
public function register(): void | ||
{ | ||
$app = $this->app; | ||
foreach($this->rules as $ruleName => $rulerClassname) { | ||
$this->validator->extend($ruleName, function (...$params) use ($app, $rulerClassname): bool { | ||
return $app->make($rulerClassname)->handle($params); | ||
}); | ||
} | ||
} | ||
} |