Skip to content

Commit

Permalink
feat(isImageUrl): 增加 isImageUrl 校验器,用于检查字符串(如用户上传的文件)是否为图片地址
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Sep 30, 2022
1 parent a05d019 commit 9082fe7
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/auto-completion-static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,21 @@ public static function check($input, string $name = '%name%'): Ret
}
}

class IsImageUrl
{
/**
* Check the input value, return a Ret object
*
* @param mixed $input
* @param string $name
* @return Ret
* @see BaseValidator::check
*/
public static function check($input, string $name = '%name%'): Ret
{
}
}

class IsIn
{
/**
Expand Down Expand Up @@ -5708,6 +5723,22 @@ public static function notImage($input, $options = [])
{
}

/**
* @return $this
* @see \Wei\IsImageUrl::__invoke
*/
public static function imageUrl($input, int $maxLength = null)
{
}

/**
* @return $this
* @see \Wei\IsImageUrl::__invoke
*/
public static function notImageUrl($input, int $maxLength = null)
{
}

/**
* @return $this
* @see \Wei\IsIn::__invoke
Expand Down Expand Up @@ -8392,6 +8423,21 @@ public function check($input, string $name = '%name%'): Ret
}
}

class IsImageUrl
{
/**
* Check the input value, return a Ret object
*
* @param mixed $input
* @param string $name
* @return Ret
* @see BaseValidator::check
*/
public function check($input, string $name = '%name%'): Ret
{
}
}

class IsIn
{
/**
Expand Down Expand Up @@ -12348,6 +12394,22 @@ public function notImage($key = null, string $label = null, $options = [])
{
}

/**
* @return $this
* @see \Wei\IsImageUrl::__invoke
*/
public function imageUrl($key = null, string $label = null, int $maxLength = null)
{
}

/**
* @return $this
* @see \Wei\IsImageUrl::__invoke
*/
public function notImageUrl($key = null, string $label = null, int $maxLength = null)
{
}

/**
* @return $this
* @see \Wei\IsIn::__invoke
Expand Down
9 changes: 9 additions & 0 deletions docs/auto-completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,14 @@ class IsImageMixin
{
}

/**
* @property Wei\IsImageUrl $isImageUrl Check if the input is valid image URL address
* @method mixed isImageUrl($input, $maxLength = null)
*/
class IsImageUrlMixin
{
}

/**
* @property Wei\IsIn $isIn Check if the input is in specified array
* @method mixed isIn($input, $array = [], $strict = null)
Expand Down Expand Up @@ -1377,6 +1385,7 @@ class WeiMixin
* @mixin IsIdCardTwMixin
* @mixin IsIdenticalToMixin
* @mixin IsImageMixin
* @mixin IsImageUrlMixin
* @mixin IsInMixin
* @mixin IsInConstMixin
* @mixin IsIntMixin
Expand Down
98 changes: 98 additions & 0 deletions lib/IsImageUrl.php
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;
}
}
61 changes: 61 additions & 0 deletions tests/unit/IsImageUrlTest.php
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'],
];
}
}

0 comments on commit 9082fe7

Please sign in to comment.