FieldValueConverter::convertValues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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