PHPackages                             beeblebrox3/caster - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. beeblebrox3/caster

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

beeblebrox3/caster
==================

PHP Library to cast arrays

0.1.0(8y ago)1726MITPHPPHP &gt;= 7.0

Since Nov 19Pushed 2y ago2 watchersCompare

[ Source](https://github.com/beeblebrox3/caster)[ Packagist](https://packagist.org/packages/beeblebrox3/caster)[ RSS](/packages/beeblebrox3-caster/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (6)Used By (0)

[![Maintainability Rating](https://camo.githubusercontent.com/efdde686715cd0edc5ccdd7693fa01a6edcc595f073a46202bd62e786dcb6df3/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d626565626c6562726f78335f636173746572266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)[![Security Rating](https://camo.githubusercontent.com/25db8e6c06b89a341a2e028a7ed5a0cf9999c35afcbbd46f4ee51de724577e0f/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d626565626c6562726f78335f636173746572266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)[![Vulnerabilities](https://camo.githubusercontent.com/8d4b967e96c806e9786860e9fd900cfd8794d6dc409ad80a0525903d9a3b4f41/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d626565626c6562726f78335f636173746572266d65747269633d76756c6e65726162696c6974696573)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)[![Coverage](https://camo.githubusercontent.com/2a7dc64c5d2a39ab93ab3a59386b9e83ad3eee7b662703788f02c873d2872292/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d626565626c6562726f78335f636173746572266d65747269633d636f766572616765)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)

Caster
======

[](#caster)

PHP Library to cast arrays of values

Requirements
------------

[](#requirements)

- PHP 7

Usage
-----

[](#usage)

Basic example:

```
$types = [
    'must_be_integer' => 'integer',
];
$input = [
    'must_be_integer' => '0.9',
];

$caster = new Beeblebrox3\Caster\Caster();
$res = $caster->cast($types, $input);

// $res will be ['must_be_integer' => 0]
```

The `$types` parameter specifies how the `$input` values should be transformed. You do this specifying an array of rules to be applyied. A rule is identified by a string.

```
$types = [
    'a' => 'integer',
    'b' => 'string',
    'c' => 'bool',
]
```

You can also apply multiple rules to the same value:

```
$types = [
    'a' => 'integer',
    'b' => 'string|lpad:10,0', // will cast to string and after apply a left string pad
]
```

Rules can have arguments:

```
$types = [
    // string up to 60 characters
    'a' => 'string:60',

    // will cast to float and then round with precision of two specifying the mode in which rounding occurs
    // (see https://php.net/round for details)
    'b' => 'float:2,' . PHP_ROUND_HALF_DOWN,
];
```

You can use nested arrays too:

```
$res = $caster->cast(
    ['level1.level2.level3.key' => 'integer'],
    ['level1' => ['level2' => ['level3' => ['key' => '999']]]]
);

// $res wil be ['level1' => ['level2' => ['level3' => ['key' => 999]]]]
```

### Available rules

[](#available-rules)

> to pass options you don't use their names, but pass the values in the displayed order. Example: ` 'a' => 'bool|1'`. arguments with `*` are mandatory

RuleArgumentsDetailsbool`nullIfEmpty`\*cast to boolean. If `nullIfEmpty` is `1`, cast empty strings and null to null, else cast to `false`date\_format`output format`\*
 `input format`format the given date to an specific format. You can also specify the format of the input datefloat`precision`
 `round mode`cast to float and optionally round the value using `precision` and `round mode` (using the `round` function)integercast to integerlpad`length` \*
 `str` \*pad the string on the left site to `length` length using `str` to fillrpad`length` \*
 `str` \*pad the string on the right site to `length` length using `str` to fillstring`max length`casto so string, optionally limiting the string size to `max length` (using `substr`)### Custom Rules

[](#custom-rules)

You can create your own rules with a class implementing the `Beeblebrox3\Caster\Rules\IRule` interface:

```
