diff --git a/README.md b/README.md
index a93b59c..8ce0be3 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,10 @@
-
+
-
-
-
-
+
+
+
+
@@ -12,7 +12,7 @@
> This library is a **work in progress**. Please, do not use it in production.
-Type Guard module is part of the [PHP's Standard Library](https://github.com/std-library), and allows you to **narrow down the type** of a variable to a more specific type. Using the `type` function, you can perform specific checks to determine the type of an object and then use that object in a way that is **type-safe** according to the [PHPStan](https://phpstan.org/) and [Psalm](https://psalm.dev/) static analyzers.
+Type Guard module is part of the [Another Library](https://github.com/another-library), and allows you to **narrow down the type** of a variable to a more specific type. Using the `type` function, you can perform specific checks to determine the type of an object and then use that object in a way that is **type-safe** according to the [PHPStan](https://phpstan.org/) and [Psalm](https://psalm.dev/) static analyzers.
Here is an example, where we use the `type` function to narrow down the type of a variable that previously had a `mixed` type:
@@ -36,7 +36,7 @@ $users = getUsers();
$users = type($users)->not()->null();
```
-And one more example, where we narrow down the type of a variable to a Collection without loosing the type information:
+And one more example, where we narrow down the type of a variable to a Collection without losing the type information:
```php
/** @var Collection|null $users */
@@ -53,7 +53,7 @@ $users = type($users)->as(Collection::class);
You may use [Composer](https://getcomposer.org) to install Type Guard into your PHP project:
```bash
-composer require std-library/type-guard
+composer require another-library/type-guard
```
## Usage
@@ -68,6 +68,7 @@ composer require std-library/type-guard
- [`not()->null()`](#notnull)
- [`asArray()`](#asarray)
- [`asResource()`](#asresource)
+- [`asIterable()`](#asiterable)
### `as`
@@ -140,6 +141,14 @@ Asserts and narrows down the type of the given variable to an array.
```php
$variable = type($variable)->asArray();
```
+### `asIterable()`
+
+Asserts and narrows down the type of the given variable to an iterable.
+
+```php
+$variable = type($variable)->asIterable();
+```
+
### `asResource()`
@@ -151,4 +160,4 @@ $variable = type($variable)->asResource();
------
-**Type Guard** is part of the [PHP's Standard Library](https://github.com/std-library) project. It was created by **[Nuno Maduro](https://twitter.com/enunomaduro)** and open-sourced under the **[MIT license](https://opensource.org/licenses/MIT)**.
+**Type Guard** is part of the [Another Library](https://github.com/another-library) project. It was created by **[Nuno Maduro](https://twitter.com/enunomaduro)** and open-sourced under the **[MIT license](https://opensource.org/licenses/MIT)**.
diff --git a/composer.json b/composer.json
index 88bbb3a..d706112 100644
--- a/composer.json
+++ b/composer.json
@@ -1,7 +1,7 @@
{
- "name": "std-library/type-guard",
- "description": "Type Guard module is part of the PHP's Standard Library, and allows you to narrow down the type of an variable to a more specific type.",
- "keywords": ["std-library", "type-guard", "assert", "narrow", "type", "php"],
+ "name": "another-library/type-guard",
+ "description": "Type Guard module is part of the Another Library, and allows you to narrow down the type of an variable to a more specific type.",
+ "keywords": ["another-library", "type-guard", "assert", "narrow", "type", "php"],
"license": "MIT",
"authors": [
{
@@ -22,17 +22,17 @@
},
"autoload": {
"psr-4": {
- "StdLibrary\\TypeGuard\\": "src/"
- }
- },
- "autoload-dev": {
- "psr-4": {
- "Tests\\": "tests/"
+ "AnotherLibrary\\TypeGuard\\": "src/"
},
"files": [
"src/Functions.php"
]
},
+ "autoload-dev": {
+ "psr-4": {
+ "Tests\\": "tests/"
+ }
+ },
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
diff --git a/docs/example.jpg b/docs/example.jpg
index 8c77c3e..2032a11 100644
Binary files a/docs/example.jpg and b/docs/example.jpg differ
diff --git a/src/Functions.php b/src/Functions.php
index cf7b423..8ba8954 100644
--- a/src/Functions.php
+++ b/src/Functions.php
@@ -2,7 +2,7 @@
declare(strict_types=1);
-use StdLibrary\TypeGuard\Type;
+use AnotherLibrary\TypeGuard\Type;
if (! function_exists('type')) {
/**
diff --git a/src/Not.php b/src/Not.php
index d0793cb..05e9aed 100644
--- a/src/Not.php
+++ b/src/Not.php
@@ -2,7 +2,7 @@
declare(strict_types=1);
-namespace StdLibrary\TypeGuard;
+namespace AnotherLibrary\TypeGuard;
use TypeError;
diff --git a/src/Type.php b/src/Type.php
index dc8ef68..b976298 100644
--- a/src/Type.php
+++ b/src/Type.php
@@ -2,7 +2,7 @@
declare(strict_types=1);
-namespace StdLibrary\TypeGuard;
+namespace AnotherLibrary\TypeGuard;
use TypeError;
@@ -161,6 +161,22 @@ public function asCallable(): callable
return $this->variable;
}
+ /**
+ * Asserts and narrow down the type to an iterable.
+ *
+ * @phpstan-assert-if-true iterable $this->variable
+ *
+ * @return (TVariable is iterable ? TVariable : never)
+ */
+ public function asIterable(): iterable
+ {
+ if (! is_iterable($this->variable)) {
+ throw new TypeError('Variable is not a [iterable].');
+ }
+
+ return $this->variable;
+ }
+
/**
* Creates a not type instance.
*
diff --git a/tests/AsIterableTest.php b/tests/AsIterableTest.php
new file mode 100644
index 0000000..25907b4
--- /dev/null
+++ b/tests/AsIterableTest.php
@@ -0,0 +1,17 @@
+asIterable();
+
+ expect($value)->toBeIterable();
+});
+
+test('not iterable type', function (): void {
+ $variable = 1;
+
+ type($variable)->asIterable();
+})->throws(TypeError::class, 'Variable is not a [iterable].');
diff --git a/types/AsIterableTest.php b/types/AsIterableTest.php
new file mode 100644
index 0000000..e4bd517
--- /dev/null
+++ b/types/AsIterableTest.php
@@ -0,0 +1,12 @@
+|string $variable */
+$variable = random_int(0, 1) !== 0 ? 'string' : [1, 2];
+assertType('array', type($variable)->asIterable());
+
+$variable = random_int(0, 1) !== 0 ? 'string' : [];
+assertType('array{}', type($variable)->asIterable());