https://github.com/softboxlab/php-array-utils
https://github.com/softboxlab/php-array-utils
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/softboxlab/php-array-utils
- Owner: SoftboxLab
- License: mit
- Created: 2017-06-15T23:45:41.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-19T21:08:24.000Z (almost 9 years ago)
- Last Synced: 2025-06-02T02:11:26.272Z (10 months ago)
- Language: PHP
- Size: 59.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP-ARRAY-UTILS
[](https://travis-ci.org/tarcisiojr/php-cast)
[](https://codecov.io/gh/tarcisiojr/php-cast)
[](https://packagist.org/packages/tarcisiojr/php-cast)
[](https://packagist.org/packages/tarcisiojr/php-cast)
[](https://packagist.org/packages/tarcisiojr/php-cast)
[](https://packagist.org/packages/tarcisiojr/php-cast)
[](https://insight.sensiolabs.com/projects/f4c39a14-b982-42d4-bd35-90bf660dc49a)
Helps to cast values of array of array. Util when it is necessary garantee the type of output value, por exemple,
encoding of arrays in JSON. Beyond cast values, it is possible format too.
## Installation
```
composer require tarcisiojr/php-cast
```
## Usage
Example:
```php
class Example {
public function test() {
$value = [
"a" => [
"b" => [
"c1" => "a",
"c2" => "2",
"c3" => "3",
"c4" => ["1", "2", "3"],
"c5" => [
["d1" => "9"],
["d1" => "8"],
["d1" => "7"],
]
]
]
];
// 1. Create a instance of CastHelper
// 2. Configure the rules to cast values
// 3. Execute method 'cast' to cast array values
$ret = (new CastHelper())
->addRule("a.b.c1", "string")
->addRule("a.b.c2", "int")
->addRule("a.b.c3", "float")
->addRule("a.b.c4.*", "int")
->addRule("a.b.c5.*.d1", "string|lpad:2,0")
->cast($value);
echo "\n\n" . json_encode($ret, JSON_PRETTY_PRINT);
}
}
```
Output:
```
{
"a": {
"b": {
"c1": "a",
"c2": 2,
"c3": 3,
"c4": [
1,
2,
3
],
"c5": [
{
"d1": "09"
},
{
"d1": "08"
},
{
"d1": "07"
}
]
}
}
}
```
### Path Expression
* a.b selects "value":
```php
[
"a" => [
"b" => "value"
]
];
```
* a.2.b selects "two":
```php
[
"a" => [
[ "b" => "zero" ],
[ "b" => "one" ],
[ "b" => "two" ],
[ "b" => "three" ]
]
];
```
* a.*.b selects all values "zero, "one", "two" and "three":
```php
[
"a" => [
[ "b" => "zero" ],
[ "b" => "one" ],
[ "b" => "two" ],
[ "b" => "three" ]
]
];
```
### Rule Expression
Expression: cast_type|option_1:param_1,param_2,...param_n;option_2:param_1...;option_n...
Where cast_type is the identifier of cast rule, options are aditional configurations do be executed against the value, for example, trim, pad, etc.
Cast Types:
* int: casts to a int value.
* float: casts to a float point value.
* bool: casts to a boolean value.
* string: casts to a string value.
- max_length:size truncate the value at max length.
- rpad:size,char pad at right position with supplied char.
- lpad:size,char pad at left position with supplied char.
## Extending
It is possible to add new cast rules, for this it will be necessary create a class the extends ```CastRule``` interface
and register it with ```PHP\Cast\CastRule\CastRuleFactory::registerCastRule``` method. You can use the
```PHP\Cast\CastRule\CastRuleBase``` to shorten the path. See example above:
```php
class BooleanCastRule extends PHP\Cast\CastRule\CastRuleBase {
public function getIdentifier() {
return "bool";
}
public function cast($value) {
return (boolean) $value;
}
}
...
PHP\Cast\CastRule\CastRuleFactory::registerCastRule(new BooleanCastRule());
...
```