Completed
Push — master ( 67a9e5...5289c4 )
by Michal
03:56
created

Util   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadData() 0 9 3
A swap() 0 9 2
A packDouble() 0 14 3
A nameShape() 0 8 2
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
23
namespace PhpMyAdmin\ShapeFile;
24
25
class Util
26
{
27
    private static $little_endian = null;
28
29
    private static $shape_names = array(
30
        0 => 'Null Shape',
31
        1 => 'Point',
32
        3 => 'PolyLine',
33
        5 => 'Polygon',
34
        8 => 'MultiPoint',
35
        11 => 'PointZ',
36
        13 => 'PolyLineZ',
37
        15 => 'PolygonZ',
38
        18 => 'MultiPointZ',
39
        21 => 'PointM',
40
        23 => 'PolyLineM',
41
        25 => 'PolygonM',
42
        28 => 'MultiPointM',
43
        31 => 'MultiPatch',
44
    );
45
46
    /**
47
     * Reads data.
48
     *
49
     * @param string $type type for unpack()
50
     * @param string $data Data to process
51
     *
52
     * @return mixed
53
     */
54 23
    public static function loadData($type, $data)
55
    {
56 23
        if ($data === false || strlen($data) == 0) {
57 21
            return false;
58
        }
59 21
        $tmp = unpack($type, $data);
60
61 21
        return current($tmp);
62
    }
63
64
    /**
65
     * Changes endianity.
66
     *
67
     * @param string $binValue Binary value
68
     *
69
     * @return string
70
     */
71 1
    public static function swap($binValue)
72
    {
73 1
        $result = $binValue[strlen($binValue) - 1];
74 1
        for ($i = strlen($binValue) - 2; $i >= 0; --$i) {
75 1
            $result .= $binValue[$i];
76 1
        }
77
78 1
        return $result;
79
    }
80
81
    /**
82
     * Encodes double value to correct endianity.
83
     *
84
     * @param float $value Value to pack
85
     *
86
     * @return string
87
     */
88 13
    public static function packDouble($value)
89
    {
90 13
        $bin = pack('d', (float) $value);
91
92 13
        if (is_null(self::$little_endian)) {
93 1
            self::$little_endian = (pack('L', 1) == pack('V', 1));
94 1
        }
95
96 13
        if (self::$little_endian) {
97 13
            return $bin;
98
        } else {
99
            return self::swap($bin);
100
        }
101
    }
102
103
    /**
104
     * Returns shape name.
105
     *
106
     * @param int $type
107
     *
108
     * @return string
109
     */
110 1
    public static function nameShape($type)
111
    {
112 1
        if (isset(self::$shape_names[$type])) {
113 1
            return self::$shape_names[$type];
114
        }
115
116 1
        return sprintf('Shape %d', $type);
117
    }
118
}
119