FieldValueConverter::convertValues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Kir\MySQL\Builder\Helpers;
4
5
class FieldValueConverter {
6
	/**
7
	 * @param array<string, mixed> $row
8
	 * @param array<string, string> $columnDefinitions
9
	 * @return array<string, mixed>
10
	 */
11
	public static function convertValues(array $row, array $columnDefinitions): array {
12
		$result = [];
13
		foreach($row as $key => $value) {
14
			if($value !== null) {
15
				$result[$key] = self::convertValue($value, $columnDefinitions[$key]);
16
			} else {
17
				$result[$key] = $value;
18
			}
19
		}
20
21
		return $result;
22
	}
23
24
	/**
25
	 * @param mixed $value
26
	 * @param string $type
27
	 * @return mixed
28
	 */
29
	private static function convertValue($value, string $type) {
30
		return match ($type) {
31
			'i' => $value !== null ? (int) $value : null,
32
			'f' => $value !== null ? (float) $value : null,
33
			default => $value,
34
		};
35
	}
36
}
37