16 function Parser_HL7v2 ( $message, $_options = NULL ) {
17 // Assume separator is a pipe
18 $this->message
= $message;
19 $this->field_separator
= '|';
20 if (is_array($_options)) {
21 $this->options
= $_options;
25 $message = $this->message
;
26 // Split HL7v2 message into lines
27 $segments = explode("\n", $message);
28 // Fail if there are no or one segments
29 if (count($segments) <= 1) {
33 // Loop through messages
35 foreach ($segments AS $__garbage => $segment) {
38 // Determine segment ID
39 $type = substr($segment, 0, 3);
43 $this->message_type
= trim($type);
45 array(&$this, '_'.$type),
54 $this->map
[$count]['type'] = $type;
55 $this->map
[$count]['position'] = 0;
59 $this->message_type
= trim($type);
60 $this->__default_segment_parser($segment);
61 $this->map
[$count]['type'] = $type;
62 $this->map
[$count]['position'] = count($this->message
[$type]);
67 // Depending on message type, handle differently
68 switch ($this->message_type
) {
70 return ('Message type '.$this->message_type
.' is '.
71 'currently unhandled'."<br/>\n");
74 } // end constructor Parser_HL7v2
77 // Set to handle current method
78 $type = str_replace('^', '_', $this->MSH
['message_type']);
80 // Check for an appropriate handler
81 $handler = CreateObject('_FreeMED.Handler_HL7v2_'.$type, $this);
83 // Error out if the handler doesn't exist
84 if (!is_object($handler)) {
85 if ($this->options
['debug']) {
86 print "<b>Could not load class ".
87 "_FreeMED.Handler_HL7v2_".$type.
93 // Run appropriate handler
94 return $handler->Handle();
95 } // end method Handle
97 //----- All handlers go below here
99 function _EVN ($segment) {
100 $composites = $this->__parse_segment ($segment);
101 if ($this->options
['debug']) {
102 print "<b>EVN segment</b><br/>\n";
103 foreach ($composites as $k => $v) {
104 print "composite[$k] = ".prepare($v)."<br/>\n";
109 $this->EVN
['event_type_code'],
110 $this->EVN
['event_datetime'],
111 $this->EVN
['event_planned'],
112 $this->EVN
['event_reason'],
113 $this->EVN
['operator_id']
117 function _MSH ($segment) {
119 $this->field_separator
= substr($segment, 0, 1);
120 $composites = $this->__parse_segment ($segment);
121 if ($this->options
['debug']) {
122 print "<b>MSH segment</b><br/>\n";
123 foreach ($composites as $k => $v) {
124 print "composite[$k] = ".prepare($v)."<br/>\n";
130 $__garbage, // Skip index [0], it's the separator
131 $this->MSH
['encoding_characters'],
132 $this->MSH
['sending_application'],
133 $this->MSH
['sending_facility'] ,
134 $this->MSH
['receiving_application'],
135 $this->MSH
['receiving_facility'],
136 $this->MSH
['message_datetime'],
137 $this->MSH
['security'],
138 $this->MSH
['message_type'],
139 $this->MSH
['message_control_id'],
140 $this->MSH
['processing_id'],
141 $this->MSH
['version_id'],
142 $this->MSH
['sequence_number'],
143 $this->MSH
['confirmation_pointer'],
144 $this->MSH
['accept_ack_type'],
145 $this->MSH
['application_ack_type'],
146 $this->MSH
['country_code']
149 // TODO: Extract $this->MSH['encoding_characters'] and use
150 // it instead of assuming the defaults.
153 //----- Truly internal functions
155 function __default_segment_parser ($segment) {
156 $composites = $this->__parse_segment($segment);
158 // The first composite is always the message type
159 $type = $composites[0];
162 if ($this->options
['debug']) {
163 print "<b>".$type." segment</b><br/>\n";
164 foreach ($composites as $k => $v) {
165 print "composite[$k] = ".prepare($v)."<br/>\n";
169 // Try to parse composites
170 foreach ($composites as $key => $composite) {
171 // If it is a composite ...
172 if (!(strpos($composite, '^') === false)) {
173 $composites[$key] = $this->__parse_composite($composite);
177 // Find out where we are
178 if (is_array($this->message
[$type])) {
179 $pos = count($this->message
[$type]);
184 // Add parsed segment to message
185 $this->message
[$type][$pos] = $composites;
186 } // end method __default_segment_parser
188 function __parse_composite ($composite) {
189 return explode('^', $composite);
190 } // end method __parse_composite
192 function __parse_segment ($segment) {
193 return explode($this->field_separator
, $segment);
194 } // end method __parse_segment
196 function composite_array() {
198 $cmp["MSH"] = $this->MSH
;
199 $cmp["EVN"] = $this->EVN
;
202 } // end class Parser_HL7v2