psr12 fixes for new PHP_CodeSniffer (#4795)
[openemr.git] / src / Services / FHIR / FhirValidationService.php
blobfb5560efeb68b218d12a986a08f124ad4e21cfbb
1 <?php
3 namespace OpenEMR\Services\FHIR;
5 use OpenEMR\Services\FhirOperationOutcomeResourceService;
6 use OpenEMR\FHIR\R4\FHIRDomainResource\FHIROperationOutcome;
7 use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRPatient;
8 use OpenEMR\FHIR\R4\FHIRResource\FHIROperationOutcome\FHIROperationOutcomeIssue;
9 use OpenEMR\FHIR\R4\FHIRElement\FHIRIssueSeverity;
10 use OpenEMR\FHIR\R4\FHIRElement\FHIRIssueType;
11 use OpenEMR\FHIR\R4\FHIRElement\FHIRCodeableConcept;
13 class FhirValidationService
15 public function validate($data)
17 if (!array_key_exists('resourceType', $data)) {
18 return $this->operationOutcomeResourceService('error', 'invalid', false, 'resourceType Not Found');
20 if ($data['resourceType']) {
21 $class = 'OpenEMR\FHIR\R4\FHIRDomainResource\FHIR' . $data['resourceType'];
22 unset($data['resourceType']);
23 try {
24 $patientResource = new $class($data);
25 } catch (\InvalidArgumentException $e) {
26 return $this->
27 operationOutcomeResourceService('fatal', 'invalid', false, $e->getMessage());
28 } catch (\Error $e) {
29 return $this->
30 operationOutcomeResourceService('fatal', 'invalid', false, 'resourceType Not Found');
32 $diff = array_diff_key($data, (array) $patientResource);
33 if ($diff) {
34 return $this->operationOutcomeResourceService(
35 'error',
36 'invalid',
37 "Invalid content " . array_key_first($diff) . " Found",
38 false
44 public function operationOutcomeResourceService(
45 $severity_value,
46 $code_value,
47 $encode = true,
48 $details_value = '',
49 $diagnostics_value = '',
50 $expression = ''
51 ) {
52 $resource = new FHIROperationOutcome();
53 $issue = new FHIROperationOutcomeIssue();
54 $severity = new FHIRIssueSeverity();
55 $severity->setValue($severity_value);
56 $issue->setSeverity($severity);
57 $code = new FHIRIssueType();
58 $code->setValue($code_value);
59 $issue->setCode($code);
60 if ($details_value) {
61 $details = new FHIRCodeableConcept();
62 $details->setText($details_value);
63 $issue->setDetails($details);
65 if ($diagnostics_value) {
66 $diagnostics = new FHIRString();
67 $diagnostics->setValue($diagnostics_value);
68 $issue->setDiagnostics($diagnostics);
70 if ($expression_value) {
71 $expression = new FHIRCodeableConcept();
72 $expression->setText($expression_value);
73 $issue->setExpression($expression);
75 $resource->addIssue($issue);
76 if ($encode) {
77 return json_encode($resource);
78 } else {
79 return $resource;