atrumursus / values-adapter
php values adapter
v1.0.0
2024-12-13 17:03 UTC
Requires
- php: ^8.2
- giggsey/libphonenumber-for-php: ^8.13
Requires (Dev)
- phpunit/phpunit: ^11.4
README
The library allows you to verify and convert variables of various types. It can be useful for working with data in REST requests, or when analyzing data coming from outside.
Install
composer require atrumursus/values-adapter
Sample 1 :
(new VInt())->min(1)->max(11)->convert('10')
equal 10
(new VBool())->convert('on')
equal true
(new VDateTime())->outFormat(\DateTime::ATOM)->inFormat(\DateTime::RFC850)->convert("Sunday, 17-Nov-24 18:56:48 UTC")
equal "2024-11-17T18:56:48+00:00"
Sample 2 :
Input Data:
[
'employee' => [
'name' => ' JAMES',
'lastname' => ' BOnd ',
'additional' => [
'age' => '27',
'gender' => 'man'
]
]
];
Prepared adapter:
$adapter = (new VDict())
->src('employee')
->map([
'NAME' => [
'src' => 'name',
'adapter' => (new VString())->case('title')->trim(true)
],
'LAST_NAME' => [
'src' => 'lastname',
'adapter' => (new VString())->case('title')->trim(true)
],
'FULL_NAME' => [
'extractor' => function ($data, $options) {
$adapter = (new VExistString)->default('')->trim(true);
return $adapter->convert($data['name']) . ' ' . $adapter->default('Anonymous')->convert($data['lastname']);
},
'adapter' => (new VString())->case('title')->trim(true)
],
'AGE' => [
'src' => ['additional', 'age'],
'adapter' => (new VInt())->min(16)
]
]);
Convert:
$result = $adapter->convert($input);
Result:
[
'NAME' => 'James',
'LAST_NAME' => 'Bond',
'FULL_NAME' => 'James Bond',
'AGE' => 27
];