farzai/php-types-guesser

1.0.0 2021-07-12 17:42 UTC

This package is auto-updated.

Last update: 2024-10-13 00:41:23 UTC


README

This package will helpful cast your value or guess your value type

Build Status

Required

{
  "php": "^7.3||^8.0"
}

Installation

composer require farzai/php-types-guesser

Example

String type
use Farzai\Guesser\TypeGuesser;

$guesser = TypeGuesser::of("This is message");

// Print "This is message"
echo $guesser;

$guesser->isString() === true;
Array type
use Farzai\Guesser\TypeGuesser;

$guesser = TypeGuesser::of([1, 2]);

 // Print "[1, 2]"
echo $guesser;

$guesser->isArray() === true;
$guesser->isJson() === false;
JSON Type
use Farzai\Guesser\TypeGuesser;

$guesser = TypeGuesser::of("[1, 2]");

echo $guesser; // Print "[1, 2]"

$guesser->isArray() === true;
$guesser->isJson() === true;
$guesser->isString() === true;
Boolean Type
use Farzai\Guesser\TypeGuesser;

// Try to enter string value
$guesser = TypeGuesser::of("true");
$guesser->isBoolean() === true;
$guesser->getValue() === true;

// Boolean type
$guesser = TypeGuesser::of(true);
$guesser->isBoolean() === true;
$guesser->getValue() === true;
Numeric Type
use Farzai\Guesser\TypeGuesser;

$guesser = TypeGuesser::of("1");
$guesser->isNumeric() === true;
$guesser->isInteger() === true;
$guesser->isFloat() === false;
$guesser->getValue() === 1;


$guesser = TypeGuesser::of(1.2);
$guesser->isNumeric() === true;
$guesser->isInteger() === false;
$guesser->isFloat() === true;
$guesser->getValue() === 1.2;


$guesser = TypeGuesser::of("2.1");
$guesser->isNumeric() === true;
$guesser->isInteger() === false;
$guesser->isFloat() === true;
$guesser->getValue() === 2.1;