1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* The MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright 2017 Julien Fastré <[email protected]>. |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
* THE SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
namespace PHPHealth\CDA\Helper; |
26
|
|
|
|
27
|
|
|
use PHPHealth\CDA\Elements\ReferenceElement; |
28
|
|
|
use PHPHealth\CDA\Elements\ReferenceType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Manages references inside a document. |
32
|
|
|
* |
33
|
|
|
* Each `ClinicalDocument` has its own `ReferenceManager`, which help to manage references across documents. |
34
|
|
|
* |
35
|
|
|
* `ReferenceType` may be added on some elements to create a reference : |
36
|
|
|
* ``` |
37
|
|
|
* $doc = new ClinicalDocument(); |
38
|
|
|
* |
39
|
|
|
* $refManager = $doc->getReferenceManager(); |
40
|
|
|
* |
41
|
|
|
* // create an element 'element' which may have a reference |
42
|
|
|
* |
43
|
|
|
* $element->setReference($refManager->getReferenceType('my_reference')); |
44
|
|
|
* |
45
|
|
|
* // will create <element ID="my_reference">blabla</element> |
46
|
|
|
* |
47
|
|
|
* // add the reference in a text |
48
|
|
|
* |
49
|
|
|
* $text->setText($refManager->getReferenceElement('my_reference')); |
50
|
|
|
* // will create <text><reference value="my_reference" /></text> |
51
|
|
|
* |
52
|
|
|
* ``` |
53
|
|
|
* |
54
|
|
|
* @author Julien Fastré <[email protected]> |
55
|
|
|
*/ |
56
|
|
|
class ReferenceManager |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @var ReferenceType[] |
61
|
|
|
*/ |
62
|
|
|
private $typeReferences = array(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @var ReferenceElement[] |
67
|
|
|
*/ |
68
|
|
|
private $elementReferences = array(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Will create a reference inside the manager. |
72
|
|
|
* |
73
|
|
|
* |
74
|
|
|
* @param string $name will be replaced by an unique id if not set |
75
|
|
|
*/ |
76
|
|
|
public function createReference($name = null) |
77
|
|
|
{ |
78
|
|
|
$ref = $name === null ? \uniqid() : $name; |
79
|
|
|
|
80
|
|
|
$this->typeReferences[$ref] = new ReferenceType($ref); |
81
|
|
|
$this->elementReferences[$ref] = new ReferenceElement($ref); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the Reference type for the given $ref |
86
|
|
|
* |
87
|
|
|
* If $ref does not exist as reference, it will be created. |
88
|
|
|
* |
89
|
|
|
* @param string $ref |
90
|
|
|
* @return ReferenceType |
91
|
|
|
*/ |
92
|
|
|
public function getReferenceType($ref) |
93
|
|
|
{ |
94
|
|
|
if (! array_key_exists($ref, $this->typeReferences)) { |
95
|
|
|
$this->createReference($ref); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->typeReferences[$ref]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the ReferenceElement for the given $ref |
103
|
|
|
* |
104
|
|
|
* If $ref does not exists as reference, it will be created. |
105
|
|
|
* |
106
|
|
|
* @param string $ref |
107
|
|
|
* @return ReferenceElement |
108
|
|
|
*/ |
109
|
|
|
public function getReferenceElement($ref) |
110
|
|
|
{ |
111
|
|
|
if (! array_key_exists($ref, $this->elementReferences)) { |
112
|
|
|
$this->createReference($ref); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->elementReferences[$ref]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|