read TEI and call reference
[auerswald-callnotifier.git] / src / callnotifier / EDSS1 / Parser.php
1 <?php
2 namespace callnotifier;
3
4 class EDSS1_Parser
5 {
6     const PARAM = 0;
7     const PARAMLENGTH = 1;
8     const PARAMVAL = 2;
9
10     public function parse($bytes)
11     {
12         $m = new EDSS1_Message();
13         $m->tei = ord($bytes{1}) >> 1;//1st bit is always 1 and needs to be removed
14
15         $curpos = 4;
16         list($curpos, $m->callRef) = $this->readLengthDataInt($bytes, ++$curpos);
17         //var_dump($curpos, dechex($m->callRef));
18         $m->type = ord($bytes{++$curpos});
19
20         $complete = false;
21         do {
22             //parameter type
23             $curbit = $bytes{++$curpos};
24             if ($curbit == "\xFF" && $bytes{$curpos + 1} == "\n") {
25                 $complete = true;
26                 break;
27             }
28             $param = new EDSS1_Parameter();
29             $m->parameters[] = $param;
30             $param->type     = ord($curbit);
31
32             //parameter length
33             $curbit = $bytes{++$curpos};
34             $param->length = ord($curbit);
35
36             //parameter data
37             $param->data = substr($bytes, $curpos + 1, $param->length);
38             $curpos += $param->length;
39         } while ($curpos < strlen($bytes) - 1);
40
41         return $m;
42     }
43
44     /**
45      * Read a datablock preceded with a length byte.
46      *
47      * @return array Array with new cursor position, data and data length
48      */
49     public function readLengthData($bytes, $curpos)
50     {
51         //var_dump('old' . $curpos);
52         $length = ord($bytes{$curpos});
53         $data = substr($bytes, $curpos + 1, $length);
54         return array($curpos + $length, $data, $length);
55     }
56
57     /**
58      * Read a datablock preceded with a length byte, return integer data.
59      *
60      * @return array Array with new cursor position, integer data and data length
61      */
62     public function readLengthDataInt($bytes, $curpos)
63     {
64         $ld = $this->readLengthData($bytes, $curpos);
65         $ld[1] = ord($ld[1]);
66         return $ld;
67     }
68 }
69
70 ?>