Completed
Push — master ( 1bcc06...3cdf3f )
by Michal
03:18
created

Util::loadData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 3
b 0
f 0
1
<?php
2
/**
3
 * phpMyAdmin ShapeFile library
4
 * <https://github.com/phpmyadmin/shapefile/>
5
 *
6
 * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
7
 * Copyright 2016 Michal Čihař <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, you can download one from
20
 * https://www.gnu.org/copyleft/gpl.html.
21
 */
22
namespace ShapeFile;
23
24
class Util {
25
    private static $little_endian = null;
26
27
    /**
28
     * Reads data
29
     *
30
     * @param string $type type for unpack()
31
     * @param string $data Data to process
32
     *
33
     * @return mixed
34
     */
35 22
    public static function loadData($type, $data) {
36 22
        if ($data === false || strlen($data) == 0) {
37 20
            return false;
38
        }
39 20
        $tmp = unpack($type, $data);
40 20
        return current($tmp);
41
    }
42
43
    /**
44
     * Changes endianity
45
     *
46
     * @param string $binValue Binary value
47
     *
48
     * @return string
49
     */
50 1
    public static function swap($binValue) {
51 1
        $result = $binValue{strlen($binValue) - 1};
52 1
        for ($i = strlen($binValue) - 2; $i >= 0; $i--) {
53 1
            $result .= $binValue{$i};
54 1
        }
55
56 1
        return $result;
57
    }
58
59
    /**
60
     * Encodes double value to correct endianity
61
     *
62
     * @param double $value Value to pack
63
     *
64
     * @return string
65
     */
66 13
    public static function packDouble($value) {
67 13
        $bin = pack("d", (double) $value);
68
69 13
        if (is_null(self::$little_endian)) {
70 1
            self::$little_endian = (pack('L', 1) == pack('V', 1));
71 1
        }
72
73 13
        if (self::$little_endian) {
74 13
            return $bin;
75
        } else {
76
            return self::swap($bin);
77
        }
78
    }
79
}
80