Recommend this page to a friend! |
Download .zip |
Info | Documentation | View files (19) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-03-23 (6 days ago) | Not enough user ratings | Total: 22 This week: 22 | All time: 11,153 This week: 2 |
Version | License | PHP version | Categories | |||
typer 1.0 | MIT/X Consortium ... | 7.1 | Data types, PHP 7 |
Description | Author | |
This package can ensure that array entry values have a given type. |
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:
$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:
$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:
$user->balance = Typer::float($dynamicArray, 'balance', 10.0);
The package can be installed via composer:
composer require yzen.dev/typer
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
docs (1 file) | ||||
src (1 file) | ||||
tests (1 file, 2 directories) | ||||
.editorconfig | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
infection.json.dist | Data | Auxiliary data | ||
phpbench.json | Data | Auxiliary data | ||
phpcs.xml | Data | Auxiliary data | ||
phpunit.xml | Data | Auxiliary data | ||
psalm.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | .github | / | workflows |
File | Role | Description |
---|---|---|
cs.yml | Data | Auxiliary data |
mutation.yml | Data | Auxiliary data |
psalm.yml | Data | Auxiliary data |
tests.yml | Data | Auxiliary data |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
Benchmark (1 file) | ||||
Units (1 file, 1 directory) | ||||
ClearCache.php | Class | Class source |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.