alexpts/php-typecast

Simple type cast request data

1.2.2 2018-03-09 14:32 UTC

This package is auto-updated.

Last update: 2024-12-15 03:34:32 UTC


README

SensioLabsInsight

Build Status Code Coverage Code Climate Scrutinizer Code Quality

Cast types

Install

composer require alexpts/php-typecast

Example

$body = [
    'title' => 'Some',
    'user' => [
        'name' => 'Alex'
        'age' => '29',
        'isAdmin' = 'false',
    ],
    'friendsIds' => ['1', '2', '3', 4],
    'date' => '11-12-2017',
    'date2' => '11-12-2017',
];

$caster = new TypeCast(new DeepArray);

// shot format
$data = $caster->cast($body, [
    'friendsIds' => ['array', ['each' => ['int']]],
    'title' => ['string'],
    'user' => ['array'],
    'user.name' => ['string'],
    'user.age' => ['int'],
    'user.isAdmin' => ['bool'],
    'date' => ['datetime'],
    'date2' => ['datetime', ['datetimeFormat' => ['Y-m-d']]], // full format
]);

/*
$data ==== [
    'title' => 'Some',
    'user' => [
        'name' => 'Alex'
        'age' => 29,
        'isAdmin' = false,
    ],
    'friendsIds' => [1, 2, 3, 4],
    'date' => new \DateTime('11-12-2017'),
    'date2' => '2017-12-11',
];
*/

Types:

string

Set type to string

bool

Set type to boolean

int

Set type to int

array

Set type to array

float

Set type to float

object

Set type to float

null

Set type to null

numbers

Remove all not number symbols from string

each

Convert each array item to describe types (see example above)

datetime

Convert value to \DateTime object with default timezone

datetimeFormat

Convert \DateTime object to string via format method

Custom convert type

You can add custom convert via method on TypeCast service

public function registerType(string $name, callable $handler): self