Skip to content

Commit

Permalink
Add asInsteadOf
Browse files Browse the repository at this point in the history
  • Loading branch information
BadJacky committed Jul 3, 2024
1 parent 803fe97 commit 2c28a56
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 17 deletions.
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
# Very short description of the package

[![Latest Version on Packagist](https://img.shields.io/packagist/v/badjacky/basic-type.svg?style=flat-square)](https://packagist.org/packages/badjacky/basic-type)
[![Total Downloads](https://img.shields.io/packagist/dt/badjacky/basic-type.svg?style=flat-square)](https://packagist.org/packages/badjacky/basic-type)
![GitHub Actions](https://github.com/badjacky/basic-type/actions/workflows/main.yml/badge.svg)
# Basic Type Assert

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
Basic Type Assert is a PHP package designed to simplify and enhance type assertions in your applications.

## Installation

You can install the package via composer:
Install the package via Composer:

```bash
composer require badjacky/basic-type
```

## Usage

Here is a basic usage example:

```php
// Usage description here
use BadJacky\BasicType\Assert;

// Example usage
Assert::string($value);
Assert::integer($value);
```

### Testing
## Testing

To run tests, use the following command:

```bash
composer test
```

### Changelog
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
See [CHANGELOG](CHANGELOG.md) for recent updates.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
See [CONTRIBUTING](CONTRIBUTING.md) for contribution guidelines.

### Security
## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.
If you find any security-related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [BadJacky](https://github.com/badjacky)
- [All Contributors](../../contributors)
- [BadJacky](https://github.com/BadJacky)
- All contributors

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.

## Laravel Package Boilerplate
## Acknowledgments

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).
11 changes: 11 additions & 0 deletions src/BasicType.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,15 @@ public function setValue(mixed $value): self
$this->value = $value;
return $this;
}

public function asInsteadOf(mixed $value): mixed
{
if (is_object($value)) {
$value = get_class($value);
}
if (false === $this->value instanceof $value) {
throw new \TypeError('Value is not an instance of ' . $value);
}
return $this->value;
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Child.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BadJacky\BasicType\Tests\Fixtures;

class Child extends Original
{

}
7 changes: 7 additions & 0 deletions tests/Fixtures/Original.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace BadJacky\BasicType\Tests\Fixtures;

class Original
{

}
8 changes: 8 additions & 0 deletions tests/Fixtures/StandClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BadJacky\BasicType\Tests\Fixtures;

class StandClass
{

}
20 changes: 20 additions & 0 deletions tests/Unit/InsteadOfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use function BadJacky\BasicType\type;

//\BadJacky\BasicType\BasicType::asInsteadOf 测试
it('test insteadof success', function () {
$parent = new \BadJacky\BasicType\Tests\Fixtures\Original();
$child = new \BadJacky\BasicType\Tests\Fixtures\Child();
expect(type($child)->asInsteadOf($parent))
->toBeInstanceOf(\BadJacky\BasicType\Tests\Fixtures\Original::class);
});

it('test insteadof failed', function () {
$stand = new \BadJacky\BasicType\Tests\Fixtures\StandClass();
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('Value is not an instance of BadJacky\BasicType\Tests\Fixtures\Original' );

type($stand)->asInsteadOf(\BadJacky\BasicType\Tests\Fixtures\Original::class);

});

0 comments on commit 2c28a56

Please sign in to comment.