Binary::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ButterAMQP;
4
5 1
Binary::init();
6
7
/**
8
 * Simple set of static methods to polyfill some PHP functions.
9
 */
10
class Binary
11
{
12
    /**
13
     * Check if machine endian format is big endian format.
14
     *
15
     * @var bool
16
     */
17
    private static $isBigEndian = false;
18
19
    /**
20
     * Initialize flags.
21
     */
22 1
    public static function init()
23
    {
24 1
        self::$isBigEndian = unpack('S', "\x00\x01")[1] == 1;
25 1
    }
26
27
    /**
28
     * Pack a value and enforce big endian format.
29
     *
30
     * @param string $format
31
     * @param string $value
32
     *
33
     * @return string
34
     */
35 39
    public static function packbe($format, $value)
36
    {
37 39
        return self::$isBigEndian ? pack($format, $value) : strrev(pack($format, $value));
38
    }
39
40
    /**
41
     * Unpack a value.
42
     *
43
     * @param string $format
44
     * @param string $data
45
     *
46
     * @return string
47
     */
48
    public static function unpack($format, $data)
49
    {
50
        return unpack($format, $data)[1];
51
    }
52
53
    /**
54
     * Unpack a value and enforce big endian format.
55
     *
56
     * @param string $format
57
     * @param string $data
58
     *
59
     * @return string
60
     */
61 39
    public static function unpackbe($format, $data)
62
    {
63 39
        return self::$isBigEndian ? unpack($format, $data)[1] : unpack($format, strrev($data))[1];
64
    }
65
}
66