add insert value to google_signin_email column in users table (#7472)
[openemr.git] / library / classes / class.Parser_HL7v2.php
blobcff3b61866e0bd34e53aff588c840726eb98c8bd
1 <?php
3 /**
4 * Parser_HL7v2 Class.
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Stephen Waite <stephen.waite@cmsvt.com>
9 * @copyright Copyright (c) 2021 Stephen Waite <stephen.waite@cmsvt.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 use Aranyasen\HL7\Message;
14 use Aranyasen\HL7\Segment;
16 class Parser_HL7v2
18 /**
19 * @var Message
21 protected $message;
23 public function __construct(string $message)
25 $this->message = new Message($message);
28 public function parse(): array
30 $segments = $this->message->getSegments();
32 // Fail if there are no or one segments
33 if (count($segments) <= 1) {
34 return [];
37 // create return array
38 $cmp = array();
40 // Loop through messages
41 $segmentMethods = get_class_methods(new Segment('XYZ'));
42 foreach ($segments as $key => $segment) {
43 $type = $segment->getName();
45 $classMethods = get_class_methods($segment);
47 foreach ($classMethods as $index => $method) {
48 if (
49 substr($method, 0, 3) == "get" &&
50 !in_array($method, $segmentMethods) &&
51 $segment->$method()
52 ) {
53 $data = $segment->$method();
54 if (is_array($data)) {
55 $data = $this->implode_recursive(', ', $segment->$method());
57 $cmp[$type][] = substr($method, 3) . " " . $data;
61 return $cmp;
65 /**
66 * Recursively implode arrays in the hl7 fields.
68 * https://gist.github.com/jimmygle/2564610#gistcomment-3634215
70 private function implode_recursive(string $separator, array $array): string
72 $string = '';
73 foreach ($array as $i => $a) {
74 if (is_array($a)) {
75 $string .= $this->implode_recursive($separator, $a);
76 } else {
77 $string .= $a;
78 if ($i < count($array) - 1) {
79 $string .= $separator;
84 return $string;