Skip to content

Commit d96f36c

Browse files
authored
Merge pull request #2 from ingenerator/0.1/validate-number
Add ValidNumber with ::minimum() validator
2 parents 403e083 + db99b86 commit d96f36c

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### Unreleased
22

3+
### v0.1.3 (2018-02-22)
4+
5+
* Extract the basic ValidNumber class for validating minimum
6+
37
### v0.1.2 (2018-02-13)
48

59
* Fix signature of InvalidUserDateTime::createFromMutable for PHP >= 5.6 compatibility

src/Validation/ValidNumber.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* @author Andrew Coulton <andrew@ingenerator.com>
4+
* @licence proprietary
5+
*/
6+
7+
namespace Ingenerator\PHPUtils\Validation;
8+
9+
10+
class ValidNumber
11+
{
12+
protected function __construct() { }
13+
14+
/**
15+
* @param string $number
16+
* @param int|float $min
17+
*
18+
* @return bool
19+
*/
20+
public static function minimum($number, $min)
21+
{
22+
return ($number >= $min);
23+
}
24+
25+
/**
26+
* Shorthand way to specify the fully qualified validator callback
27+
*
28+
* @param string $rulename
29+
*
30+
* @return string
31+
*/
32+
public static function rule($rulename)
33+
{
34+
switch ($rulename) {
35+
case 'minimum':
36+
return static::class.'::'.$rulename;
37+
default:
38+
throw new \InvalidArgumentException('Unknown rule '.__CLASS__.'::'.$rulename);
39+
}
40+
}
41+
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @author Andrew Coulton <andrew@ingenerator.com>
4+
* @licence proprietary
5+
*/
6+
7+
namespace test\unit\Ingenerator\PHPUtils\Validation;
8+
9+
10+
use Ingenerator\PHPUtils\Validation\ValidNumber;
11+
12+
class ValidNumberTest extends \PHPUnit_Framework_TestCase
13+
{
14+
15+
/**
16+
* @testWith ["", false]
17+
* ["junk", false]
18+
* ["0", false]
19+
* ["12", false]
20+
* ["13", true]
21+
* [18, true]
22+
*/
23+
public function test_it_validates_minimum_as_greater_or_equal($value, $expect)
24+
{
25+
$this->assertSame($expect, ValidNumber::minimum($value, 13));
26+
}
27+
28+
/**
29+
* @expectedException \InvalidArgumentException
30+
*/
31+
public function test_it_throws_for_unknown_rule_name()
32+
{
33+
ValidNumber::rule('random nonsense');
34+
}
35+
36+
public function test_it_returns_validator_name()
37+
{
38+
$this->assertSame(
39+
'Ingenerator\PHPUtils\Validation\ValidNumber::minimum',
40+
ValidNumber::rule('minimum')
41+
);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)