PHP Classes

Typer PHP Type Casting Class: Assure that array entry values have a given types

Recommend this page to a friend!
  Info   View files Documentation   View files View files (19)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-03-23 (6 days ago) RSS 2.0 feedNot enough user ratingsTotal: 22 This week: 22All time: 11,153 This week: 2Up
Version License PHP version Categories
typer 1.0MIT/X Consortium ...7.1Data types, PHP 7
Description 

Author

This package can ensure that array entry values have a given type.

It provides a class with functions that can take values of different types and convert them to specific types.

The functions can also take default values if the parameter values are null.

It can cast integer, string, float, bool, and array types.

Picture of Andrey Iatsenko
  Performance   Level  
Innovation award
Innovation award
Nominee: 5x

 

Documentation

Typer

Packagist Version GitHub Workflow Status (with branch) Coverage Packagist Downloads Packagist Downloads

Mutation testing badge type-coverage psalm-level

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);

Installation

The package can be installed via composer:

composer require yzen.dev/typer

  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocs (1 file)
Files folder imagesrc (1 file)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file infection.json.dist Data Auxiliary data
Accessible without login Plain text file phpbench.json Data Auxiliary data
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:22
This week:22
All time:11,153
This week:2Up