forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FhirResourcesService.php
166 lines (150 loc) · 5.88 KB
/
FhirResourcesService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* FHIRResources service class
*
* @package OpenEMR
* @link http:https://www.open-emr.org
* @author Jerry Padgett <[email protected]>
* @copyright Copyright (c) 2018 Jerry Padgett <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use HL7\FHIR\STU3\FHIRDomainResource\FHIREncounter;
use HL7\FHIR\STU3\FHIRDomainResource\FHIRPatient;
use HL7\FHIR\STU3\FHIRDomainResource\FHIRPractitioner;
use HL7\FHIR\STU3\FHIRElement\FHIRAddress;
use HL7\FHIR\STU3\FHIRElement\FHIRAdministrativeGender;
use HL7\FHIR\STU3\FHIRElement\FHIRCodeableConcept;
use HL7\FHIR\STU3\FHIRElement\FHIRHumanName;
use HL7\FHIR\STU3\FHIRElement\FHIRId;
use HL7\FHIR\STU3\FHIRElement\FHIRReference;
use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterParticipant;
use HL7\FHIR\STU3\FHIRResource\FHIRBundle;
use HL7\FHIR\STU3\FHIRResource\FHIRBundle\FHIRBundleLink;
use HL7\FHIR\STU3\PHPFHIRResponseParser;
//use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterLocation;
//use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterDiagnosis;
//use HL7\FHIR\STU3\FHIRElement\FHIRPeriod;
//use HL7\FHIR\STU3\FHIRElement\FHIRParticipantRequired;
class FhirResourcesService
{
public function createBundle($resource = '', $resource_array = [], $encode = true)
{
$bundleUrl = \RestConfig::$REST_FULL_URL;
$nowDate = date("Y-m-d\TH:i:s");
$meta = array('lastUpdated' => $nowDate);
$bundleLink = new FHIRBundleLink(array('relation' => 'self', 'url' => $bundleUrl));
// set bundle type default to collection so may include different
// resource types. at least I hope thats how it works....
$bundleInit = array(
'identifier' => $resource . "bundle",
'type' => 'collection',
'total' => count($resource_array),
'meta' => $meta);
$bundle = new FHIRBundle($bundleInit);
$bundle->addLink($bundleLink);
foreach ($resource_array as $addResource) {
$bundle->addEntry($addResource);
}
if ($encode) {
return json_encode($bundle);
}
return $bundle;
}
public function createPatientResource($resourceId = '', $data = '', $encode = true)
{
// @todo add display text after meta
$nowDate = date("Y-m-d\TH:i:s");
$id = new FhirId();
$id->setValue($resourceId);
$name = new FHIRHumanName();
$address = new FHIRAddress();
$gender = new FHIRAdministrativeGender();
$meta = array('versionId' => '1', 'lastUpdated' => $nowDate);
$initResource = array('id' => $id, 'meta' => $meta);
$name->setUse('official');
$name->setFamily($data['lname']);
$name->given = [$data['fname'], $data['mname']];
$address->addLine($data['street']);
$address->setCity($data['city']);
$address->setState($data['state']);
$address->setPostalCode($data['postal_code']);
$gender->setValue(strtolower($data['sex']));
$patientResource = new FHIRPatient($initResource);
//$patientResource->setId($id);
$patientResource->setActive(true);
$patientResource->setGender($gender);
$patientResource->addName($name);
$patientResource->addAddress($address);
if ($encode) {
return json_encode($patientResource);
} else {
return $patientResource;
}
}
public function createPractitionerResource($id = '', $data = '', $encode = true)
{
$resource = new FHIRPractitioner();
$id = new FhirId();
$name = new FHIRHumanName();
$address = new FHIRAddress();
$id->setValue($id);
$name->setUse('official');
$name->setFamily($data['lname']);
$name->given = [$data['fname'], $data['mname']];
$address->addLine($data['street']);
$address->setCity($data['city']);
$address->setState($data['state']);
$address->setPostalCode($data['zip']);
$resource->setId($id);
$resource->setActive(true);
$gender = new FHIRAdministrativeGender();
$gender->setValue('unknown');
$resource->setGender($gender);
$resource->addName($name);
$resource->addAddress($address);
if ($encode) {
return json_encode($resource);
} else {
return $resource;
}
}
public function createEncounterResource($eid = '', $data = '', $encode = true)
{
$pid = 'patient-' . $data['pid'];
$temp = $data['provider_id'];
//$r = $this->createPractitionerResource($data['provider_id'], $temp);
$resource = new FHIREncounter();
$id = new FhirId();
$id->setValue('encounter-' . $eid);
$resource->setId($id);
$participant = new FHIREncounterParticipant();
$prtref = new FHIRReference;
$temp = 'Practitioner/provider-' . $data['provider_id'];
$prtref->setReference($temp);
$participant->setIndividual($prtref);
$date = date('Y-m-d', strtotime($data['date']));
$participant->setPeriod(['start' => $date]);
$resource->addParticipant($participant);
$reason = new FHIRCodeableConcept();
$reason->setText($data['reason']);
$resource->addReason($reason);
$resource->status = 'finished';
$resource->setSubject(['reference' => "Patient/$pid"]);
if ($encode) {
return json_encode($resource);
} else {
return $resource;
}
}
public function parseResource($rjson = '', $scheme = 'json')
{
$parser = new PHPFHIRResponseParser(false);
if ($scheme == 'json') {
$class_object = $parser->parse($rjson);
} else {
// @todo xml- not sure yet.
}
return $class_object; // feed to resource class or use as is object
}
}