Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/internations/type-jail
Enforce type constraints through proxies
https://github.com/internations/type-jail
Last synced: 9 days ago
JSON representation
Enforce type constraints through proxies
- Host: GitHub
- URL: https://github.com/internations/type-jail
- Owner: InterNations
- Created: 2015-01-11T09:36:57.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-05-10T12:05:17.000Z (over 2 years ago)
- Last Synced: 2024-10-08T09:37:45.156Z (about 1 month ago)
- Language: PHP
- Homepage:
- Size: 289 KB
- Stars: 22
- Watchers: 37
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Type Jail [![Test](https://github.com/InterNations/type-jail/actions/workflows/tests.yaml/badge.svg)](https://github.com/InterNations/type-jail/actions/workflows/tests.yaml)
Enforce super type contract of an object## Usage
```php
use InterNations\Component\TypeJail\Factory\SuperTypeFactory;
use InterNations\Component\TypeJail\Factory\JailFactory;
use InterNations\Component\TypeJail\Factory\SuperTypeJailFactory;$file = new SplFileObject(__FILE__);
$factory = new JailFactory();
$file = $factory->createInstanceJail($file, 'SplFileInfo');// Will return true
var_dump($file instanceof SplFileInfo);// Will return the file path because that method is declared in SplFileInfo
$file->getFilePath();// Will throw an exception indicating a type violation because that method
// is declared in SplFileObject
$file->flock();$factory = new SuperTypeJailFactory();
$file = $factory->createInstanceJail($file, 'SplFileInfo');// Will return false
var_dump($file instanceof SplFileInfo);// Will return the file path because that method is declared in SplFileInfo
$file->getFilePath();// Will throw an exception indicating a type violation because that method
// is declared in SplFileObject
$file->flock();$factory = new SuperTypeFactory();
$file = $factory->createInstanceJail($file, 'SplFileInfo');// Will return false
var_dump($file instanceof SplFileInfo);// Will return the file path because that method is declared in SplFileInfo
$file->getFilePath();// Fatal error: method not found
$file->flock();
```## Acknowledgement
Standing on the shoulders of [ocramius/proxy-manager](https://github.com/Ocramius/ProxyManager/) by Marco Pivetta that makes it super-duper easy to work with proxies.