Skip to content

Commit

Permalink
feat(IsText, IsMediumText): 增加 IsTextIsMediumText 校验规则
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Nov 24, 2020
1 parent 962ace0 commit 6c6e1da
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/IsMediumText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Wei Framework
*
* @copyright Copyright (c) 2008-2020 Twin Huang
* @license http://opensource.org/licenses/mit-license.php MIT License
*/

namespace Wei;

/**
* Check if the input is a string of 16777215(16Mb-1) bytes or less
*
* @author Twin Huang <twinhuang@qq.com>
*/
class IsMediumText extends IsString
{
/**
* @var int
*/
protected $maxLength = 16777215;
}
25 changes: 25 additions & 0 deletions lib/IsText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Wei Framework
*
* @copyright Copyright (c) 2008-2020 Twin Huang
* @license http://opensource.org/licenses/mit-license.php MIT License
*/

namespace Wei;

use Wei\IsChar;
use Wei\IsString;

/**
* Check if the input is a string of 65535(64Kb-1) bytes or less
*
* @author Twin Huang <twinhuang@qq.com>
*/
class IsText extends IsString
{
/**
* @var int
*/
protected $maxLength = 65535;
}
47 changes: 47 additions & 0 deletions tests/unit/IsMediumTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace WeiTest;

/**
* @internal
*/
final class IsMediumTextTest extends BaseValidatorTestCase
{
/**
* @dataProvider providerForMediumTextVal
* @param mixed $input
*/
public function testStringVal($input)
{
$this->assertTrue($this->isMediumText($input));
}

/**
* @dataProvider providerForNotMediumTextVal
* @param mixed $input
*/
public function testNotStringVal($input)
{
$this->assertFalse($this->isMediumText($input));
}

public function providerForMediumTextVal()
{
return [
[''],
['123'],
[str_repeat('1', 16777215)],
[str_repeat('', 16777215 / 3)],
[str_repeat('🙂', 16777215 / 4)],
];
}

public function providerForNotMediumTextVal()
{
return [
[str_repeat('1', 16777215 + 1)],
[str_repeat('', 16777215 / 3) . '1'],
[str_repeat('🙂', 16777215 / 4) . '1234'],
];
}
}
47 changes: 47 additions & 0 deletions tests/unit/IsTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace WeiTest;

/**
* @internal
*/
final class IsTextTest extends BaseValidatorTestCase
{
/**
* @dataProvider providerForTextVal
* @param mixed $input
*/
public function testStringVal($input)
{
$this->assertTrue($this->isText($input));
}

/**
* @dataProvider providerForNotTextVal
* @param mixed $input
*/
public function testNotStringVal($input)
{
$this->assertFalse($this->isText($input));
}

public function providerForTextVal()
{
return [
[''],
['123'],
[str_repeat('1', 65535)],
[str_repeat('', 65535 / 3)],
[str_repeat('🙂', 65535 / 4)],
];
}

public function providerForNotTextVal()
{
return [
[str_repeat('1', 65535) . '1'],
[str_repeat('', 65535 / 3) . '1'],
[str_repeat('🙂', 65535 / 4) . '1234'],
];
}
}

0 comments on commit 6c6e1da

Please sign in to comment.