Passed
Push — master ( 26e0e4...4a85b9 )
by Peter
10:37
created

ReferenceManager::createReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 NON INFRINGEMENT. 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
26
namespace i3Soft\CDA\Helper;
27
28
use i3Soft\CDA\DataType\ReferenceType;
29
use i3Soft\CDA\Elements\Html\ReferenceElement;
30
31
/**
32
 * Manages references inside a document.
33
 *
34
 * Each `ClinicalDocument` has its own `ReferenceManager`, which help to manage references across documents.
35
 *
36
 * `ReferenceType` may be added on some elements to create a reference :
37
 * ```
38
 * $doc = new ClinicalDocument();
39
 *
40
 * $refManager = $doc->getReferenceManager();
41
 *
42
 * // create an element 'element' which may have a reference
43
 *
44
 * $element->setReference($refManager->getReferenceType('my_reference'));
45
 *
46
 * // will create <element ID="my_reference">blabla</element>
47
 *
48
 * // add the reference in a text
49
 *
50
 * $text->setText($refManager->getReferenceElement('my_reference'));
51
 * // will create <text><reference value="my_reference" /></text>
52
 *
53
 * ```
54
 *
55
 * @author Julien Fastré <[email protected]>
56
 */
57
class ReferenceManager
58
{
59
    /**
60
     *
61
     * @var ReferenceType[]
62
     */
63
    private $typeReferences = array();
64
65
    /**
66
     *
67
     * @var ReferenceElement[]
68
     */
69
    private $elementReferences = array();
70
71
    /**
72
     * Get the Reference type for the given $ref
73
     *
74
     * If $ref does not exist as reference, it will be created.
75
     *
76
     * @param string $ref
77
     *
78
     * @return ReferenceType
79
     */
80
    public function getReferenceType($ref): ReferenceType
81
    {
82
        if (!array_key_exists($ref, $this->typeReferences)) {
83
            $this->createReference($ref);
84
        }
85
86
        return $this->typeReferences[$ref];
87
    }
88
89
    /**
90
     * Will create a reference inside the manager.
91
     *
92
     *
93
     * @param string $name will be replaced by an unique id if not set
94
     */
95
    public function createReference($name = null)
96
    {
97
        $ref                           = $name ?? \uniqid(__CLASS__, true);
98
        $this->typeReferences[$ref]    = new ReferenceType($ref);
99
        $this->elementReferences[$ref] = new ReferenceElement($ref);
100
    }
101
102
    /**
103
     * Get the ReferenceElement for the given $ref
104
     *
105
     * If $ref does not exists as reference, it will be created.
106
     *
107
     * @param string $ref
108
     *
109
     * @return ReferenceElement
110
     */
111
    public function getReferenceElement($ref): ReferenceElement
112
    {
113
        if (!array_key_exists($ref, $this->elementReferences)) {
114
            $this->createReference($ref);
115
        }
116
117
        return $this->elementReferences[$ref];
118
    }
119
}
120