parse number types and numbering plans
[auerswald-callnotifier.git] / src / callnotifier / Logger / Echo.php
1 <?php
2 namespace callnotifier;
3
4 class Logger_Echo implements Logger
5 {
6     public function __construct()
7     {
8         $cc = new \Console_Color2();
9         $this->begin = $cc->convert('%y');
10         $this->end = $cc->convert('%n');
11         $this->blue = $cc->convert('%b');
12         $this->red = $cc->convert('%r');
13         $this->white = $cc->convert('%w');
14         $this->purple = $cc->convert('%p');
15     }
16
17     public function log($type, $arData)
18     {
19         if ($type == 'msgData') {
20             echo $this->begin . $arData['type'] . $this->end
21                 . ': ' . $arData['details'] . "\n";
22             if (preg_match('#^[A-Z][0-9]{2}: (.+)$#', $arData['details'], $matches)) {
23                 $bytestring = $matches[1];
24                 $line = '';
25                 foreach (explode(' ', $bytestring) as $strbyte) {
26                     $line .= chr(hexdec($strbyte));
27                 }
28                 $line = preg_replace(
29                     '/[^[:print:]]/',
30                     $this->white . '?' . $this->end,
31                     $line
32                 );
33                 echo $this->red . '     bytes' . $this->end . ': ' . $line . "\n";
34             }
35         } else if ($type == 'edss1msg') {
36             $msg = $arData['msg'];
37             echo sprintf(
38                 $this->purple . 'EDSS1_Message' . $this->end
39                 . ' type %02X '
40                 . $this->purple . '%s' . $this->end
41                 . ' SAPI %d, CR %d, TEI %d, call %d'
42                 . ', %d parameters',
43                 $msg->type,
44                 $msg->getTypeName(),
45                 $msg->sapi,
46                 $msg->callResponse,
47                 $msg->tei,
48                 $msg->callRef,
49                 count($msg->parameters)
50             ) . "\n";
51             foreach ($msg->parameters as $param) {
52                 echo sprintf(
53                     " Parameter type %02X%s, %d bytes: %s\n",
54                     $param->type,
55                     $param->title
56                     ? ' ' . $this->purple . $param->title . $this->end
57                     : '',
58                     $param->length,
59                     preg_replace('/[^[:print:]]/', '?', $param->data)
60                     . (isset($param->number)
61                        ? ' ' . $this->red . $param->number . $this->end
62                        : ''
63                     )
64                 );
65                 if ($param instanceof EDSS1_Parameter_INumber) {
66                     echo sprintf(
67                         "    Number type: %s, plan: %s\n",
68                         EDSS1_Parameter_Names::$numberTypes[$param->numberType],
69                         EDSS1_Parameter_Names::$numberingPlans[$param->numberingPlan]
70                     );
71                 }
72             }
73         } else {
74             echo $this->blue . $type . $this->end . ': '
75                 . var_export($arData, true) . "\n";
76         }
77     }
78
79 }
80
81 ?>