uint64()   A
last analyzed

Complexity

Conditions 5
Paths 12

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.6111
c 0
b 0
f 0
cc 5
nc 12
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
use function is_array, is_float, is_int, pack, unpack;
13
14
const WSDB_FUNCTIONS = true;
15
16
// http://php.net/manual/en/function.pack.php#119402
17
18
/**
19
 * @param int|string $i
20
 * @param bool|null  $endianness
21
 *
22
 * @return array|string|false
23
 */
24
function uint32($i, bool $endianness = null){
25
26
	if($endianness === true){ // big-endian
27
		$f = 'N';
28
	}
29
	elseif($endianness === false){ // little-endian
30
		$f = 'V';
31
	}
32
	else{ // machine byte order
33
		$f = 'L';
34
	}
35
36
	$i = is_int($i) ? pack($f, $i) : unpack($f, $i);
37
38
	return is_array($i) ? $i[1] : $i;
39
}
40
41
/**
42
 * @param int|string $i
43
 * @param bool|null  $endianness
44
 *
45
 * @return array|string|false
46
 */
47
function uint64($i, bool $endianness = null){
48
49
	if($endianness === true){ // big-endian
50
		$f = 'J';
51
	}
52
	elseif($endianness === false){ // little-endian
53
		$f = 'P';
54
	}
55
	else{ // machine byte order
56
		$f = 'Q';
57
	}
58
59
	$i = is_int($i) ? pack($f, $i) : unpack($f, $i);
60
61
	return is_array($i) ? $i[1] : $i;
62
}
63
64
/**
65
 * @param float|int|string $i
66
 * @param bool|null        $endianness
67
 *
68
 * @return array|string|false
69
 */
70
function float($i, bool $endianness = null){
71
72
	if($endianness === true){ // big-endian
73
		$f = 'G';
74
	}
75
	elseif($endianness === false){ // little-endian
76
		$f = 'g';
77
	}
78
	else{ // machine byte order
79
		$f = 'f';
80
	}
81
82
	$i = (is_float($i) || is_int($i)) ? pack($f, $i) : unpack($f, $i);
83
84
	return is_array($i) ? $i[1] : $i;
85
}
86