Passed
Pull Request — master (#28)
by Maurício
12:24
created

Util::swap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * phpMyAdmin ShapeFile library
7
 * <https://github.com/phpmyadmin/shapefile/>.
8
 *
9
 * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
10
 * Copyright 2016 - 2017 Michal Čihař <[email protected]>
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, you can download one from
23
 * https://www.gnu.org/copyleft/gpl.html.
24
 */
25
26
namespace PhpMyAdmin\ShapeFile;
27
28
use function current;
29
use function pack;
30
use function sprintf;
31
use function strrev;
32
use function unpack;
33
34
class Util
35
{
36
    private static bool|null $littleEndian = null;
37
38
    private const SHAPE_NAMES = [
39
        0 => 'Null Shape',
40
        1 => 'Point',
41
        3 => 'PolyLine',
42
        5 => 'Polygon',
43
        8 => 'MultiPoint',
44
        11 => 'PointZ',
45
        13 => 'PolyLineZ',
46
        15 => 'PolygonZ',
47
        18 => 'MultiPointZ',
48
        21 => 'PointM',
49
        23 => 'PolyLineM',
50
        25 => 'PolygonM',
51
        28 => 'MultiPointM',
52
        31 => 'MultiPatch',
53
    ];
54
55
    /**
56
     * Reads data.
57
     *
58
     * @param string       $type type for unpack()
59
     * @param string|false $data Data to process
60
     */
61 97
    public static function loadData(string $type, string|false $data): mixed
62
    {
63 97
        if ($data === false || $data === '') {
64 90
            return false;
65
        }
66
67 86
        $tmp = unpack($type, $data);
68
69 86
        return $tmp === false ? false : current($tmp);
70
    }
71
72
    /**
73
     * Encodes double value to correct endianity.
74
     */
75 55
    public static function packDouble(float $value): string
76
    {
77 55
        $bin = pack('d', $value);
78
79 55
        if (self::$littleEndian === null) {
80 4
            self::$littleEndian = (pack('L', 1) === pack('V', 1));
81
        }
82
83 55
        if (self::$littleEndian) {
84 55
            return $bin;
85
        }
86
87
        return strrev($bin);
88
    }
89
90
    /**
91
     * Returns shape name.
92
     */
93 4
    public static function nameShape(int $type): string
94
    {
95 4
        return self::SHAPE_NAMES[$type] ?? sprintf('Shape %d', $type);
96
    }
97
}
98