Passed
Push — master ( 8ff2ce...afa880 )
by smiley
01:20
created

float()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 9.2222
c 0
b 0
f 0
cc 6
nc 24
nop 2
1
<?php
2
/**
3
 * @filesource   functions.php
4
 * @created      06.01.2019
5
 * @author       smiley <[email protected]>
6
 * @copyright    2019 smiley
7
 * @license      MIT
8
 */
9
10
namespace codemasher\WildstarDB;
11
12
const WSDB_FUNCTIONS = true;
13
14
// http://php.net/manual/en/function.pack.php#119402
15
16
function uint32($i, bool $endianness = null){
17
18
	if($endianness === true){ // big-endian
19
		$f = 'N';
20
	}
21
	elseif($endianness === false){ // little-endian
22
		$f = 'V';
23
	}
24
	else{ // machine byte order
25
		$f = 'L';
26
	}
27
28
	$i = is_int($i) ? pack($f, $i) : unpack($f, $i);
29
30
	return is_array($i) ? $i[1] : $i;
31
}
32
33
function uint64($i, bool $endianness = null){
34
35
	if($endianness === true){ // big-endian
36
		$f = 'J';
37
	}
38
	elseif($endianness === false){ // little-endian
39
		$f = 'P';
40
	}
41
	else{ // machine byte order
42
		$f = 'Q';
43
	}
44
45
	$i = is_int($i) ? pack($f, $i) : unpack($f, $i);
46
47
	return is_array($i) ? $i[1] : $i;
48
}
49
50
function float($i, bool $endianness = null){
51
52
	if($endianness === true){ // big-endian
53
		$f = 'G';
54
	}
55
	elseif($endianness === false){ // little-endian
56
		$f = 'g';
57
	}
58
	else{ // machine byte order
59
		$f = 'f';
60
	}
61
62
	$i = is_float($i) || is_int($i) ? pack($f, $i) : unpack($f, $i);
63
64
	return is_array($i) ? $i[1] : $i;
65
}
66