-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isImageUrl): 增加
isImageUrl
校验器,用于检查字符串(如用户上传的文件)是否为图片地址
- Loading branch information
Showing
4 changed files
with
230 additions
and
0 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,98 @@ | ||
<?php | ||
|
||
/** | ||
* Wei Framework | ||
* | ||
* @copyright Copyright (c) 2008-2022 Twin Huang | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
|
||
namespace Wei; | ||
|
||
/** | ||
* Check if the input is valid image URL address | ||
* | ||
* @author Twin Huang <twinhuang@qq.com> | ||
*/ | ||
class IsImageUrl extends BaseValidator | ||
{ | ||
public const VALID_TYPE = 'string'; | ||
|
||
protected $invalidMessage = '%name% must be a valid image URL'; | ||
|
||
protected $extsMessage = '%name% extension(%ext%) is not allowed, allowed extension: %exts%'; | ||
|
||
protected $negativeMessage = '%name% must not be URL'; | ||
|
||
/** | ||
* The allowed file extensions | ||
* | ||
* @var array | ||
*/ | ||
protected $exts = [ | ||
'jpg', | ||
'jpeg', | ||
'bmp', | ||
'gif', | ||
'png', | ||
]; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $maxLength = 255; | ||
|
||
/** | ||
* The detected file extension | ||
* | ||
* @var string | ||
* @internal | ||
*/ | ||
protected $ext = ''; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __invoke($input, int $maxLength = null) | ||
{ | ||
null !== $maxLength && $this->storeOption('maxLength', $maxLength); | ||
|
||
return $this->isValid($input); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doValidate($input) | ||
{ | ||
if (!filter_var($input, \FILTER_VALIDATE_URL, \FILTER_FLAG_PATH_REQUIRED)) { | ||
$this->addError('invalid'); | ||
return false; | ||
} | ||
|
||
if (null !== $this->maxLength) { | ||
$result = $this->validateRule($input, 'maxCharLength', $this->maxLength); | ||
if (!$result) { | ||
return $result; | ||
} | ||
} | ||
|
||
$path = parse_url($input, \PHP_URL_PATH); | ||
if (!in_array(strtolower($this->getExt($path)), $this->exts, true)) { | ||
$this->addError('exts'); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function getExt(string $file): string | ||
{ | ||
if (false !== $pos = strrpos($file, '.')) { | ||
$this->ext = strtolower(substr($file, $pos + 1)); | ||
} else { | ||
$this->ext = ''; | ||
} | ||
return $this->ext; | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
namespace WeiTest; | ||
|
||
/** | ||
* @mixin \IsImageUrlMixin | ||
* @internal | ||
*/ | ||
final class IsImageUrlTest extends BaseValidatorTestCase | ||
{ | ||
/** | ||
* @dataProvider providerForUrl | ||
* @param mixed $input | ||
*/ | ||
public function testImageUrl($input) | ||
{ | ||
$this->assertTrue($this->isImageUrl($input)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerForNotUrl | ||
* @param mixed $input | ||
*/ | ||
public function testNotImageUrl($input) | ||
{ | ||
$this->assertFalse($this->isImageUrl($input)); | ||
} | ||
|
||
public function providerForUrl() | ||
{ | ||
return [ | ||
['https://example.com/dir/1.jpg'], | ||
['https://example.com/dir/2.jpeg'], | ||
['https://example.com/dir/3.bmp'], | ||
['https://example.com/dir/4.gif'], | ||
['https://example.com/dir/5.png'], | ||
['https://www.google.com/1.jpg'], | ||
['http://www.google.com/1.jpg'], | ||
['http://www.google.com/1.jpg?a=b'], | ||
// filter_var consider valid | ||
['file:///tmp/test.png'], | ||
['abc://example/1.jpg'], | ||
]; | ||
} | ||
|
||
public function providerForNotUrl() | ||
{ | ||
return [ | ||
['http://exa_mple.com'], | ||
['g.cn'], | ||
['http//www.example'], | ||
['http:/www.example'], | ||
['/tmp/test.c'], | ||
['/'], | ||
["http://\r\n/bar"], | ||
// filter_var consider invalid | ||
['//example/1.jpg'], | ||
['https://example.com/dir/' . str_repeat('1', 255) . '.jpg'], | ||
]; | ||
} | ||
} |