Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ObjectMapper | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
| mapObject | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Traits; |
| 4 | |
| 5 | trait ObjectMapper |
| 6 | { |
| 7 | public function mapObject($input, array $map): array |
| 8 | { |
| 9 | $output = []; |
| 10 | |
| 11 | $input = is_object($input) ? get_object_vars($input) : $input; |
| 12 | |
| 13 | foreach ($map as $originalKey => $rules) { |
| 14 | if (isset($input[$originalKey])) { |
| 15 | $value = $input[$originalKey] === '' ? null : $input[$originalKey]; |
| 16 | |
| 17 | $newKey = $rules['rename'] ?? $originalKey; |
| 18 | |
| 19 | if (isset($rules['transform']) && is_callable($rules['transform'])) { |
| 20 | $value = $rules['transform']($value); |
| 21 | } |
| 22 | |
| 23 | $output[$newKey] = $value; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return $output; |
| 28 | } |
| 29 | } |