Skip to content

Commit 18527a3

Browse files
committed
Add mixed type
1 parent 1968514 commit 18527a3

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@ TypeGuard can validate:
1212
- Scalar types: `string`, `integer`, etc.:
1313

1414
```php
15-
(new \TypeGuard\Type('string'))->match('foo'); // => true
15+
(new \TypeGuard\Guard('string'))->match('foo'); // => true
1616
```
1717

1818
- Object types: `ArrayAccess`, `stdClass`, etc.:
1919

2020
```php
21-
(new \TypeGuard\Type('stdClass'))->match(new stdClass()); // => true
21+
(new \TypeGuard\Guard('stdClass'))->match(new stdClass()); // => true
2222
```
2323

2424
- Union types: `string|integer`:
2525

2626
```php
27-
$guard = new \TypeGuard\Type('string|integer');
27+
$guard = new \TypeGuard\Guard('string|integer');
2828
$guard->match('foo'); // => true
2929
$guard->match(1); // => true
3030
```
3131

3232
- Intersection types: `ArrayAccess&Countable`:
3333

3434
```php
35-
(new \TypeGuard\Type('ArrayAccess&Countable'))->match(new ArrayIterator()); // => true
35+
(new \TypeGuard\Guard('ArrayAccess&Countable'))->match(new ArrayIterator()); // => true
3636
```
3737

3838
- Optional types: `?string`:
3939

4040
```php
41-
(new \TypeGuard\Type('?string'))->match(null); // => true
41+
(new \TypeGuard\Guard('?string'))->match(null); // => true
4242
```
4343

src/Type.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function __construct(string $typeName)
1818
*/
1919
public function match($parameter): bool
2020
{
21+
if ('mixed' === $this->typeName) {
22+
return true;
23+
}
24+
2125
$type = \gettype($parameter);
2226

2327
return $type === 'object'

test/TypeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function matchDataProvider(): array
2525
['NULL', null, true],
2626
['ArrayAccess', new \ArrayIterator, true],
2727
['Countable', new \ArrayIterator, true],
28+
['mixed', 'foo', true],
29+
['mixed', 1, true],
30+
['mixed', null, true],
31+
['mixed', new \ArrayIterator, true],
2832
];
2933
}
3034
}

0 commit comments

Comments
 (0)