Completed
Push — master ( b9af01...17dfa5 )
by Michal
03:38
created

Util   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 35
rs 10
wmc 7
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadData() 0 7 2
A swap() 0 8 2
A packDouble() 0 14 3
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
 * http://www.gnu.org/copyleft/gpl.html.
21
 */
22
namespace ShapeFile;
23
24
class Util {
25
    private static $little_endian = null;
26
27
    public static function loadData($type, $data) {
28
        if (!$data) {
29
            return $data;
30
        }
31
        $tmp = unpack($type, $data);
32
        return current($tmp);
33
    }
34
35
    public static function swap($binValue) {
36
        $result = $binValue{strlen($binValue) - 1};
37
        for ($i = strlen($binValue) - 2; $i >= 0; $i--) {
38
            $result .= $binValue{$i};
39
        }
40
41
        return $result;
42
    }
43
44
    public static function packDouble($value) {
45
        $value = (double) $value;
46
        $bin = pack("d", $value);
47
48
        if (is_null(self::$little_endian)) {
49
            self::$little_endian = (pack('L', 1) == pack('V', 1));
50
        }
51
52
        if (self::$little_endian) {
53
            return $bin;
54
        } else {
55
            return self::swap($bin);
56
        }
57
    }
58
}
59