https://github.com/yzen-dev/typer
This is a simple helper package that will help you avoid using "if" and ternary operators.
https://github.com/yzen-dev/typer
Last synced: 12 days ago
JSON representation
This is a simple helper package that will help you avoid using "if" and ternary operators.
- Host: GitHub
- URL: https://github.com/yzen-dev/typer
- Owner: yzen-dev
- Created: 2024-02-18T21:52:47.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-23T21:49:27.000Z (over 2 years ago)
- Last Synced: 2025-12-14T16:48:40.420Z (6 months ago)
- Language: PHP
- Size: 130 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Typer


[](https://codecov.io/gh/yzen-dev/typer)


[](https://dashboard.stryker-mutator.io/reports/github.com/yzen-dev/typer/master)
[](https://shepherd.dev/github/yzen-dev/typer)
[](https://shepherd.dev/github/yzen-dev/typer)
This is a simple helper package that helps make the code cleaner. Often, when working with data from third-party sources, such as website parsing, you need to write hundreds of lines of code to check for a particular property.
Most likely, you write a lot of if or ternary operators, and your code looks something like this:
```php
$user = new User();
$user->id = isset($dynamicArray['id']) ? (int)$dynamicArray['id'] : null;
$user->email = isset($dynamicArray['email']) ? (string)$dynamicArray['email'] : null;
$user->balance = isset($dynamicArray['balance']) ? (float)$dynamicArray['balance'] : null;
$user->blocked = isset($dynamicArray['blocked']) ? ($dynamicArray['blocked'] === 'true' ? true : false) : null;
```
When using **Typer**, you don't need to worry about a lot of checks and transformations. Simply wrap the code in the `typer` method:
```php
$user = new User();
$user->id = Typer::int($dynamicArray, 'id');
$user->email = Typer::string($dynamicArray, 'email');
$user->balance = Typer::float($dynamicArray, 'balance');
$user->blocked = Typer::bool($dynamicArray, 'blocked');
```
If, in the absence of a parameter, you need to specify a default value other than "null", you can simply pass it as the second argument:
```php
$user->balance = Typer::float($dynamicArray, 'balance', 10.0);
```
## **Installation**
The package can be installed via composer:
```
composer require yzen.dev/typer
```