Highway to PSR2
[openemr.git] / library / classes / class.Parser_HL7v2.php
blob10842c537b1e9d8faced9ccb3cb77b6670255715
1 <?php
2 // $Id$
3 // $Author$
4 // HL7 Parser
6 class Parser_HL7v2
9 var $field_separator;
10 var $map;
11 var $message;
12 var $message_type;
14 var $MSH;
15 var $EVN;
17 function __construct($message, $_options = null)
19 // Assume separator is a pipe
20 $this->message = $message;
21 $this->field_separator = '|';
22 if (is_array($_options)) {
23 $this->options = $_options;
26 function parse()
28 $message = $this->message;
29 // Split HL7v2 message into lines
30 $segments = explode("\n", $message);
31 // Fail if there are no or one segments
32 if (count($segments) <= 1) {
33 return false;
36 // Loop through messages
37 $count = 0;
38 foreach ($segments as $__garbage => $segment) {
39 $count++;
41 // Determine segment ID
42 $type = substr($segment, 0, 3);
43 switch ($type) {
44 case 'MSH':
45 case 'EVN':
46 $this->message_type = trim($type);
47 call_user_func_array(
48 array(&$this, '_'.$type),
49 array(
50 // All but type
51 substr(
52 $segment,
53 -(strlen($segment)-3)
57 $this->map[$count]['type'] = $type;
58 $this->map[$count]['position'] = 0;
59 break;
61 default:
62 $this->message_type = trim($type);
63 $this->__default_segment_parser($segment);
64 $this->map[$count]['type'] = $type;
65 $this->map[$count]['position'] = count($this->message[$type]);
66 break;
67 } // end switch type
70 // Depending on message type, handle differently
71 switch ($this->message_type) {
72 default:
73 return ('Message type '.$this->message_type.' is '.
74 'currently unhandled'."<br/>\n");
75 break;
76 } // end switch
77 } // end constructor Parser_HL7v2
79 function Handle()
81 // Set to handle current method
82 $type = str_replace('^', '_', $this->MSH['message_type']);
84 // Check for an appropriate handler
85 $handler = CreateObject('_FreeMED.Handler_HL7v2_'.$type, $this);
87 // Error out if the handler doesn't exist
88 if (!is_object($handler)) {
89 if ($this->options['debug']) {
90 print "<b>Could not load class ".
91 "_FreeMED.Handler_HL7v2_".$type.
92 "</b><br/>\n";
95 return false;
98 // Run appropriate handler
99 return $handler->Handle();
100 } // end method Handle
102 //----- All handlers go below here
104 function _EVN($segment)
106 $composites = $this->__parse_segment($segment);
107 if ($this->options['debug']) {
108 print "<b>EVN segment</b><br/>\n";
109 foreach ($composites as $k => $v) {
110 print "composite[$k] = ".prepare($v)."<br/>\n";
114 list (
115 $this->EVN['event_type_code'],
116 $this->EVN['event_datetime'],
117 $this->EVN['event_planned'],
118 $this->EVN['event_reason'],
119 $this->EVN['operator_id']
120 ) = $composites;
121 } // end method _EVN
123 function _MSH($segment)
125 // Get separator
126 $this->field_separator = substr($segment, 0, 1);
127 $composites = $this->__parse_segment($segment);
128 if ($this->options['debug']) {
129 print "<b>MSH segment</b><br/>\n";
130 foreach ($composites as $k => $v) {
131 print "composite[$k] = ".prepare($v)."<br/>\n";
135 // Assign values
136 list (
137 $__garbage, // Skip index [0], it's the separator
138 $this->MSH['encoding_characters'],
139 $this->MSH['sending_application'],
140 $this->MSH['sending_facility'] ,
141 $this->MSH['receiving_application'],
142 $this->MSH['receiving_facility'],
143 $this->MSH['message_datetime'],
144 $this->MSH['security'],
145 $this->MSH['message_type'],
146 $this->MSH['message_control_id'],
147 $this->MSH['processing_id'],
148 $this->MSH['version_id'],
149 $this->MSH['sequence_number'],
150 $this->MSH['confirmation_pointer'],
151 $this->MSH['accept_ack_type'],
152 $this->MSH['application_ack_type'],
153 $this->MSH['country_code']
154 ) = $composites;
156 // TODO: Extract $this->MSH['encoding_characters'] and use
157 // it instead of assuming the defaults.
158 } // end method _MSH
160 //----- Truly internal functions
162 function __default_segment_parser($segment)
164 $composites = $this->__parse_segment($segment);
166 // The first composite is always the message type
167 $type = $composites[0];
169 // Debug
170 if ($this->options['debug']) {
171 print "<b>".$type." segment</b><br/>\n";
172 foreach ($composites as $k => $v) {
173 print "composite[$k] = ".prepare($v)."<br/>\n";
177 // Try to parse composites
178 foreach ($composites as $key => $composite) {
179 // If it is a composite ...
180 if (!(strpos($composite, '^') === false)) {
181 $composites[$key] = $this->__parse_composite($composite);
185 $pos = 0;
187 // Find out where we are
188 if (is_array($this->message[$type])) {
189 $pos = count($this->message[$type]);
192 //Ramesh Nagul - EnSoftek commented line out as it is throwing an error in parsing.
193 //$this->message[$type][$pos] = $composites;
194 // Add parsed segment to message
195 //Fix is below
196 $this->map[$pos][$type] = $composites;
197 } // end method __default_segment_parser
199 function __parse_composite($composite)
201 return explode('^', $composite);
202 } // end method __parse_composite
204 function __parse_segment($segment)
206 return explode($this->field_separator, $segment);
207 } // end method __parse_segment
209 function composite_array()
211 $cmp = array();
212 $cmp["MSH"] = $this->MSH;
213 $cmp["EVN"] = $this->EVN;
214 return $cmp;
216 } // end class Parser_HL7v2