Skip to content

Commit

Permalink
blank and filled now support stringable (#617)
Browse files Browse the repository at this point in the history
* blank and filled now support stringable

* chore: Add blank and filled functions for stringable support

* feat: Add tests for blank and filled functions with stringable support

* chore: Update Functions.php to support Stringable interface

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed May 8, 2024
1 parent efc00e2 commit 853ceec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/helpers/src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ function blank($value): bool
return count($value) === 0;
}

if ($value instanceof Stringable) {
return trim((string) $value) === '';
}

return empty($value);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Helpers/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
* @contact [email protected]
*/
use Hyperf\Contract\ApplicationInterface;
use Hyperf\Stringable\Stringable;

use function FriendsOfHyperf\Helpers\blank;
use function FriendsOfHyperf\Helpers\class_namespace;
use function FriendsOfHyperf\Helpers\Command\call;
use function FriendsOfHyperf\Helpers\filled;
use function FriendsOfHyperf\Helpers\object_get;
use function FriendsOfHyperf\Helpers\preg_replace_array;

Expand Down Expand Up @@ -61,3 +64,35 @@

expect(call('foo:bar', ['argument' => 'value']))->toBe(0);
});

test('test filled', function ($expect, $value) {
expect(filled($value))->toBe($expect);
})->with([
[false, null],
[false, ''],
[false, ' '],
[false, new Stringable('')],
[false, new Stringable(' ')],
[true, 10],
[true, true],
[true, false],
[true, 0],
[true, 0.0],
[true, new Stringable(' FooBar ')],
]);

test('test blank', function ($expect, $value) {
expect(blank($value))->toBe($expect);
})->with([
[true, null],
[true, ''],
[true, ' '],
[true, new Stringable('')],
[true, new Stringable(' ')],
[false, 10],
[false, true],
[false, false],
[false, 0],
[false, 0.0],
[false, new Stringable(' FooBar ')],
]);

0 comments on commit 853ceec

Please sign in to comment.