Skip to content

Commit f1081cf

Browse files
vjiksamdark
andauthored
Add NumericHelper::isInteger()
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
1 parent e0f4484 commit f1081cf

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
## 1.1.1 under development
55

66
- Enh #62: Add method `StringHelper::split` that split a string to array with non-empty lines (vjik)
7+
- Enh #63: Add method `NumericHelper::isInteger` that checks whether the given string is an integer number (vjik)
78

89
## 1.1.0 November 13, 2020
910

@@ -21,4 +22,3 @@
2122
- Initial release.
2223

2324

24-

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ The following methods are available:
9797

9898
- toOrdinal
9999
- normalize
100+
- isInteger
100101

101102
## Inflector usage
102103

src/NumericHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ public static function normalize($value): string
6161
$value = str_replace([' ', ','], ['', '.'], (string)$value);
6262
return preg_replace('/\.(?=.*\.)/', '', $value);
6363
}
64+
65+
/**
66+
* Checks whether the given string is an integer number.
67+
* @param mixed $value
68+
* @return bool
69+
*/
70+
public static function isInteger($value): bool
71+
{
72+
return filter_var($value, FILTER_VALIDATE_INT) !== false;
73+
}
6474
}

tests/NumericHelperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,30 @@ public function testNormalizeNumberWithIncorrectType(): void
6666
$this->expectException(\InvalidArgumentException::class);
6767
NumericHelper::normalize([]);
6868
}
69+
70+
public function dataIsInteger(): array
71+
{
72+
return [
73+
[new \stdClass(), false],
74+
[[], false],
75+
['42', true],
76+
['-42', true],
77+
['0', true],
78+
[' 7', true],
79+
['-', false],
80+
['hello', false],
81+
['', false],
82+
];
83+
}
84+
85+
/**
86+
* @dataProvider dataIsInteger
87+
*
88+
* @param mixed $value
89+
* @param bool $expected
90+
*/
91+
public function testIsInteger($value, bool $expected): void
92+
{
93+
$this->assertSame($expected, NumericHelper::isInteger($value));
94+
}
6995
}

0 commit comments

Comments
 (0)