From 7299b516b2f92f7816f00f73017d03875200e9b3 Mon Sep 17 00:00:00 2001 From: Sergey Romanenko Date: Sun, 8 May 2022 16:22:20 +0300 Subject: [PATCH] Strings 4.3.0 --- CHANGELOG.md | 9 +++++++++ src/Strings.php | 40 ++++++++++++++++++++++++++++++++++++++++ tests/StringsTest.php | 18 ++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b79ccd..ea241c4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ + +# [4.3.0](https://github.com/glowyphp/strings) (2022-05-08) +* Added isNull() method. +* Added isInteger() method. +* Added isFloat() method. +* Added isUuid() method. +* Added toNull() method. +* Method pipe() returns a new instance of the class. + # [4.2.0](https://github.com/glowyphp/strings) (2022-01-01) * Added isHexColor() method. diff --git a/src/Strings.php b/src/Strings.php index 62ed9a8..6abc3d8 100644 --- a/src/Strings.php +++ b/src/Strings.php @@ -2029,6 +2029,36 @@ public function isHTML(): bool return $this->toString() !== strip_tags($this->toString()); } + /** + * Determine whether the string is integer. + * + * @return bool Returns TRUE on success or FALSE otherwise. + */ + public function isInteger(): bool + { + return (bool) filter_var($this->toString(), FILTER_VALIDATE_INT); + } + + /** + * Determine whether the string is float. + * + * @return bool Returns TRUE on success or FALSE otherwise. + */ + public function isFloat(): bool + { + return ((bool) filter_var($this->toString(), FILTER_VALIDATE_FLOAT) !== $this->isInteger()); + } + + /** + * Determine whether the string is null. + * + * @return bool Returns TRUE on success or FALSE otherwise. + */ + public function isNull(): bool + { + return $this->toString() === null || $this->toString() === 'null'; + } + /** * Determine whether the string is Boolean. * @@ -2105,6 +2135,16 @@ public function toFloat(): float return floatval($this->string); } + /** + * Return Strings object as null. + * + * @return float Return Strings object as null. + */ + public function toNull(): float + { + return null; + } + /** * Returns a boolean representation of the given logical string value. * diff --git a/tests/StringsTest.php b/tests/StringsTest.php index 60395f1..486742c 100644 --- a/tests/StringsTest.php +++ b/tests/StringsTest.php @@ -849,6 +849,24 @@ $this->assertFalse(Strings::create('fòôbàřs')->isIP()); }); +test('test isInteger() method', function (): void { + $this->assertTrue(Strings::create('1')->isInteger()); + $this->assertFalse(Strings::create('1.0')->isInteger()); + $this->assertFalse(Strings::create('Foo')->isInteger()); +}); + +test('test isFloat() method', function (): void { + $this->assertFalse(Strings::create('1')->isFloat()); + $this->assertTrue(Strings::create('0.1')->isFloat()); + $this->assertTrue(Strings::create('1.0')->isFloat()); + $this->assertTrue(Strings::create('0.1')->isFloat()); + $this->assertFalse(Strings::create('Foo')->isFloat()); +}); + +test('test isNull() method', function (): void { + $this->assertTrue(Strings::create('null')->isNull()); +}); + test('test isMAC() method', function (): void { $this->assertTrue(Strings::create('00:11:22:33:44:55')->isMAC()); $this->assertFalse(Strings::create('127.0.0.1')->isMAC());