https://github.com/beeblebrox3/caster
PHP Library to cast arrays
https://github.com/beeblebrox3/caster
library php
Last synced: 9 months ago
JSON representation
PHP Library to cast arrays
- Host: GitHub
- URL: https://github.com/beeblebrox3/caster
- Owner: beeblebrox3
- License: mit
- Created: 2017-11-19T06:35:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-06-08T22:36:44.000Z (about 3 years ago)
- Last Synced: 2025-06-02T09:27:55.326Z (about 1 year ago)
- Topics: library, php
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)
[](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)
[](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)
[](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)
# Caster
PHP Library to cast arrays of values
## Requirements
- PHP 7
## Usage
Basic example:
```php
$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.
```php
$types = [
'a' => 'integer',
'b' => 'string',
'c' => 'bool',
]
```
You can also apply multiple rules to the same value:
```php
$types = [
'a' => 'integer',
'b' => 'string|lpad:10,0', // will cast to string and after apply a left string pad
]
```
Rules can have arguments:
```php
$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:
```php
$res = $caster->cast(
['level1.level2.level3.key' => 'integer'],
['level1' => ['level2' => ['level3' => ['key' => '999']]]]
);
// $res wil be ['level1' => ['level2' => ['level3' => ['key' => 999]]]]
```
### 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
| Rule | Arguments | Details |
|-------------|----------------------------------------|--------------------------------------------------------------------------------------------------------------|
| bool | `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 date |
| float | `precision`
`round mode` | cast to float and optionally round the value using `precision` and `round mode` (using the `round` function) |
| integer | | cast to integer |
| lpad | `length` *
`str` * | pad the string on the left site to `length` length using `str` to fill |
| rpad | `length` *
`str` * | pad the string on the right site to `length` length using `str` to fill |
| string | `max length` | casto so string, optionally limiting the string size to `max length` (using `substr`) |
### Custom Rules
You can create your own rules with a class implementing the `Beeblebrox3\Caster\Rules\IRule` interface:
```php
addCustomRule('pipe', PipeRule::class);
$res = $caster->cast(['a' => 'pipe'], ['a' => '199']);
```
The `PipeRule` will only return the same input.
#### Function as custom rule
> new on version `0.1.0`
```php
$caster = new Caster();
$caster->addCustomrule('pipe', function ($value) { return $value; });
```
Now you created the same pipe rule as before, but without the class.
## Changelog
- `0.1.0` Add support functions as custom rules;
- `0.0.2` Add custom rules support;