Completed
Push — master ( 4d5f61...930dd2 )
by Michal
03:15
created

Util::swap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
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
    private static $shape_names = array(
28
        0 => 'Null Shape',
29
        1 => 'Point',
30
        3 => 'PolyLine',
31
        5 => 'Polygon',
32
        8 => 'MultiPoint',
33
        11 => 'PointZ',
34
        13 => 'PolyLineZ',
35
        15 => 'PolygonZ',
36
        18 => 'MultiPointZ',
37
        21 => 'PointM',
38
        23 => 'PolyLineM',
39
        25 => 'PolygonM',
40
        28 => 'MultiPointM',
41
        31 => 'MultiPatch',
42
    );
43
44
    /**
45
     * Reads data
46
     *
47
     * @param string $type type for unpack()
48
     * @param string $data Data to process
49
     *
50
     * @return mixed
51
     */
52 23
    public static function loadData($type, $data) {
53 23
        if ($data === false || strlen($data) == 0) {
54 21
            return false;
55
        }
56 21
        $tmp = unpack($type, $data);
57 21
        return current($tmp);
58
    }
59
60
    /**
61
     * Changes endianity
62
     *
63
     * @param string $binValue Binary value
64
     *
65
     * @return string
66
     */
67 1
    public static function swap($binValue) {
68 1
        $result = $binValue{strlen($binValue) - 1};
69 1
        for ($i = strlen($binValue) - 2; $i >= 0; $i--) {
70 1
            $result .= $binValue{$i};
71 1
        }
72
73 1
        return $result;
74
    }
75
76
    /**
77
     * Encodes double value to correct endianity
78
     *
79
     * @param double $value Value to pack
80
     *
81
     * @return string
82
     */
83 13
    public static function packDouble($value) {
84 13
        $bin = pack("d", (double) $value);
85
86 13
        if (is_null(self::$little_endian)) {
87 1
            self::$little_endian = (pack('L', 1) == pack('V', 1));
88 1
        }
89
90 13
        if (self::$little_endian) {
91 13
            return $bin;
92
        } else {
93
            return self::swap($bin);
94
        }
95
    }
96
97
    /**
98
     * Returns shape name
99
     *
100
     * @param integer $type
101
     *
102
     * @return string
103
     */
104 1
    public static function nameShape($type)
105
    {
106 1
        if (isset(self::$shape_names[$type])) {
107 1
            return self::$shape_names[$type];
108
        }
109 1
        return sprintf('Shape %d', $type);
110
    }
111
}
112