FieldValueConverter::convertValue()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
rs 9.6111
c 2
b 0
f 0
cc 5
nc 5
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