Completed
Push — master ( 001830...de2f31 )
by Michal
03:36
created

Util   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadData() 0 5 2
A swap() 0 8 2
B packDouble() 0 18 5
1
<?php
2
/**
3
 * BytesFall ShapeFiles library
4
 *
5
 * The library implements the 2D variants of the ShapeFile format as defined in
6
 * http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf.
7
 * The library currently supports reading and editing of ShapeFiles and the
8
 * Associated information (DBF file).
9
 *
10
 * @package bfShapeFiles
11
 * @version 0.0.2
12
 * @link http://bfshapefiles.sourceforge.net/
13
 * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2-or-later
14
 *
15
 * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, you can download one from
28
 * http://www.gnu.org/copyleft/gpl.html.
29
 *
30
 */
31
namespace ShapeFile;
32
33
class Util {
34
  public static function loadData($type, $data) {
35
    if (!$data) return $data;
36
    $tmp = unpack($type, $data);
37
    return current($tmp);
38
  }
39
40
  public static function swap($binValue) {
41
    $result = $binValue{strlen($binValue) - 1};
42
    for($i = strlen($binValue) - 2; $i >= 0 ; $i--) {
43
      $result .= $binValue{$i};
44
    }
45
46
    return $result;
47
  }
48
49
  public static function packDouble($value, $mode = 'LE') {
50
    $value = (double)$value;
51
    $bin = pack("d", $value);
52
53
    //We test if the conversion of an integer (1) is done as LE or BE by default
54
    switch (pack ('L', 1)) {
55
      case pack ('V', 1): //Little Endian
56
        $result = ($mode == 'LE') ? $bin : self::swap($bin);
57
      break;
58
      case pack ('N', 1): //Big Endian
59
        $result = ($mode == 'BE') ? $bin : self::swap($bin);
60
      break;
61
      default: //Some other thing, we just return false
62
        $result = FALSE;
63
    }
64
65
    return $result;
66
  }
67
}
68