FieldValueConverter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
dl 0
loc 29
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertValue() 0 5 3
A convertValues() 0 11 3
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