FieldValueConverter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

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

2 Methods

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