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

Value::toDOMElement()   F

Complexity

Conditions 13
Paths 1536

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 43
rs 2.45
c 0
b 0
f 0
cc 13
nc 1536
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * The MIT License
5
 *
6
 * Copyright 2018  Peter Gee <https://github.com/pgee70>.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
/**
28
 *
29
 * @package     i3Soft\CDA
30
 * @author      Peter Gee <https://github.com/pgee70>
31
 * @link        https://github.com/pgee70/cda
32
 *
33
 */
34
35
36
namespace i3Soft\CDA\Elements;
37
38
39
use i3Soft\CDA\ClinicalDocument as CDA;
40
use i3Soft\CDA\DataType\ValueType;
41
use i3Soft\CDA\Elements\Html\ReferenceElement;
42
use i3Soft\CDA\Interfaces\MediaTypeInterface;
43
use i3Soft\CDA\Interfaces\XSITypeInterface;
44
use i3Soft\CDA\Traits\ValueTypeTrait;
45
use i3Soft\CDA\Traits\XSITypeTrait;
46
47
class Value extends AbstractElement implements XSITypeInterface, MediaTypeInterface
48
{
49
    use ValueTypeTrait;
50
    use XSITypeTrait;
51
52
    // attributes
53
54
    /** @var ValueType */
55
    protected $code;
56
    /** @var ValueType */
57
    protected $code_system;
58
    /** @var ValueType */
59
    protected $code_system_name;
60
    /** @var ValueType */
61
    protected $display_name;
62
    /** @var ValueType */
63
    protected $units;
64
    /** @var ReferenceElement */
65
    protected $reference_element;
66
    /** @var  string */
67
    protected $content;
68
    /** @var string */
69
    protected $media_type;
70
    // child elements
71
    /** @var Low */
72
    protected $low;
73
    /** @var High */
74
    protected $high;
75
    /** @var array */
76
    protected $tags;
77
78
    /**
79
     * Value constructor.
80
     * XSI types are required for the value tag
81
     *
82
     * @link http://www.cdapro.com/know/25047
83
     *
84
     * @param string $xsi_type
85
     * @param string $value
86
     */
87
    public function __construct(string $value = '', string $xsi_type = '')
88
    {
89
        $this->media_type = '';
90
        if ($xsi_type === XSITypeInterface::BOOLEAN
91
            && \in_array($value, array('true', 'false'), true) === false) {
92
            throw new \InvalidArgumentException("The value for booleans must be true or false, {$value} supplied!");
93
        }
94
        if ($value && \is_string($value)) {
95
            $this->setValueTypeString($value);
96
        }
97
        $this->setXSIType($xsi_type);
98
        $this->tags = array();
99
    }
100
101
    /**
102
     * @param string $value_type
103
     *
104
     * @return Value
105
     */
106
    public function setValueTypeString(string $value_type): self
107
    {
108
        $this->setValueType(new ValueType($value_type, 'value'));
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $code
114
     * @param string $displayName
115
     *
116
     * @param bool   $xsi_attribute
117
     *
118
     * @return Value
119
     */
120
    public static function SNOMED(string $code, string $displayName, bool $xsi_attribute = true): Value
121
    {
122
        $value = (new Value())
123
          ->setCode($code)
124
          ->setDisplayName($displayName)
125
          ->setCodeSystem('2.16.840.1.113883.6.96')
126
          ->setCodeSystemName('SNOMED CT');
127
        if ($xsi_attribute) {
128
            $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR);
129
        }
130
        return $value;
131
    }
132
133
    public static function NCTISValue(string $code, string $displayName, bool $xsi_attribute = true): Value
134
    {
135
        $value = (new Value())
136
          ->setCode($code)
137
          ->setDisplayName($displayName)
138
          ->setCodeSystem('2.16.840.1.113883.6.96')
139
          ->setCodeSystemName('SNOMED CT');
140
        if ($xsi_attribute) {
141
            $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR);
142
        }
143
        return $value;
144
    }
145
146
    public static function HL7ResultStatus(string $code, string $displayName, bool $xsi_attribute = true): Value
147
    {
148
        $value = (new Value())
149
          ->setCode($code)
150
          ->setDisplayName($displayName)
151
          ->setCodeSystem('2.16.840.1.113883.12.123')
152
          ->setCodeSystemName('HL7 Result Status');
153
        if ($xsi_attribute) {
154
            $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR);
155
        }
156
        return $value;
157
    }
158
159
160
    /**
161
     * @return ValueType
162
     */
163
    public function getCode(): ValueType
164
    {
165
        return $this->code;
166
    }
167
168
    /**
169
     * @param string $code
170
     *
171
     * @return Value
172
     */
173
    public function setCode(string $code): self
174
    {
175
        $this->code = new ValueType ($code, 'code');
176
        return $this;
177
    }
178
179
    /**
180
     * @return ValueType
181
     */
182
    public function getCodeSystem(): ValueType
183
    {
184
        return $this->code_system;
185
    }
186
187
    /**
188
     * @param string $code_system
189
     *
190
     * @return Value
191
     */
192
    public function setCodeSystem(string $code_system): self
193
    {
194
        $this->code_system = new ValueType ($code_system, 'codeSystem');
195
        return $this;
196
    }
197
198
    /**
199
     * @return ValueType
200
     */
201
    public function getCodeSystemName(): ValueType
202
    {
203
        return $this->code_system_name;
204
    }
205
206
    /**
207
     * @param string $code_system_name
208
     *
209
     * @return Value
210
     */
211
    public function setCodeSystemName(string $code_system_name): self
212
    {
213
        $this->code_system_name = new ValueType ($code_system_name, 'codeSystemName');
214
        return $this;
215
    }
216
217
    /**
218
     * @return ValueType
219
     */
220
    public function getDisplayName(): ValueType
221
    {
222
        return $this->display_name;
223
    }
224
225
    /**
226
     * @param string $display_name
227
     *
228
     * @return Value
229
     */
230
    public function setDisplayName(string $display_name): self
231
    {
232
        $this->display_name = new ValueType ($display_name, 'displayName');
233
        return $this;
234
    }
235
236
    /**
237
     * @return ValueType
238
     */
239
    public function getUnits(): ValueType
240
    {
241
        return $this->units;
242
    }
243
244
    /**
245
     * @param string $units
246
     *
247
     * @return Value
248
     */
249
    public function setUnits(string $units): self
250
    {
251
        $this->units = new ValueType ($units, 'unit');
252
        return $this;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function getElementTag(): string
259
    {
260
        return 'value';
261
    }
262
263
264
    /**
265
     * @param \DOMDocument $doc
266
     *
267
     * @return \DOMElement
268
     */
269
    public function toDOMElement(\DOMDocument $doc): \DOMElement
270
    {
271
        $attributes = array();
272
        if ($this->hasValueType()) {
273
            $attributes[] = 'value_type';
274
        }
275
        if ($this->hasCode()) {
276
            $attributes[] = 'code';
277
        }
278
        if ($this->hasCodeSystem()) {
279
            $attributes[] = 'code_system';
280
        }
281
        if ($this->hasCodeSystemName()) {
282
            $attributes[] = 'code_system_name';
283
        }
284
        if ($this->hasDisplayName()) {
285
            $attributes[] = 'display_name';
286
        }
287
        if ($this->hasUnits()) {
288
            $attributes[] = 'units';
289
        }
290
291
        $el = $this->createElement($doc, $attributes);
292
        if ($this->content) {
293
            $el->appendChild(new \DOMText($this->getContent()));
294
        } elseif ($this->hasReferenceElement()) {
295
            $el->appendChild($this->getReferenceElement()->toDOMElement($doc));
296
        }
297
298
        if ($this->hasLow()) {
299
            $el->appendChild($this->getLow()->toDOMElement($doc));
300
        }
301
        if ($this->hasHigh()) {
302
            $el->appendChild($this->getHigh()->toDOMElement($doc));
303
        }
304
305
        if ($this->hasTags()) {
306
            foreach ($this->getTags() as $tag => $value) {
307
                $el->appendChild($doc->createElement(CDA::NS_CDA . $tag, $value));
308
            }
309
        }
310
311
        return $el;
312
    }
313
314
    public function hasCode(): bool
315
    {
316
        return null !== $this->code;
317
    }
318
319
    public function hasCodeSystem(): bool
320
    {
321
        return null !== $this->code_system;
322
    }
323
324
    public function hasCodeSystemName(): bool
325
    {
326
        return null !== $this->code_system_name;
327
    }
328
329
    public function hasDisplayName(): bool
330
    {
331
        return null !== $this->display_name;
332
    }
333
334
    public function hasUnits(): bool
335
    {
336
        return null !== $this->units;
337
    }
338
339
    /**
340
     * @return string
341
     */
342
    public function getContent(): string
343
    {
344
        return $this->content;
345
    }
346
347
    /**
348
     * @param string $content
349
     *
350
     * @return Value
351
     */
352
    public function setContent(string $content): self
353
    {
354
        $this->content = $content;
355
        return $this;
356
    }
357
358
    public function hasReferenceElement(): bool
359
    {
360
        return null !== $this->reference_element;
361
    }
362
363
    /**
364
     * @return ReferenceElement
365
     */
366
    public function getReferenceElement(): ReferenceElement
367
    {
368
        return $this->reference_element;
369
    }
370
371
    /**
372
     * @param ReferenceElement $reference_element
373
     *
374
     * @return Value
375
     */
376
    public function setReferenceElement(ReferenceElement $reference_element): Value
377
    {
378
        $this->reference_element = $reference_element;
379
        return $this;
380
    }
381
382
    public function hasLow(): bool
383
    {
384
        return null !== $this->low;
385
    }
386
387
    /**
388
     * @return Low
389
     */
390
    public function getLow(): Low
391
    {
392
        return $this->low;
393
    }
394
395
    /**
396
     * @param Low $low
397
     *
398
     * @return self
399
     */
400
    public function setLow(Low $low): self
401
    {
402
        $this->low = $low;
403
        return $this;
404
    }
405
406
    public function hasHigh(): bool
407
    {
408
        return null !== $this->high;
409
    }
410
411
    /**
412
     * @return High
413
     */
414
    public function getHigh(): High
415
    {
416
        return $this->high;
417
    }
418
419
    /**
420
     * @param High $high
421
     *
422
     * @return self
423
     */
424
    public function setHigh(High $high): self
425
    {
426
        $this->high = $high;
427
        return $this;
428
    }
429
430
    public function hasTags(): bool
431
    {
432
        return \count($this->tags) > 0;
433
    }
434
435
    /**
436
     * @return array
437
     */
438
    public function getTags(): array
439
    {
440
        return $this->tags;
441
    }
442
443
    /**
444
     * @param $tag
445
     * @param $value
446
     *
447
     * @return Value
448
     */
449
    public function addTagValue($tag, $value): Value
450
    {
451
        $this->tags[$tag] = $value;
452
        return $this;
453
    }
454
455
    /**
456
     * @inheritDoc
457
     */
458
    public function getMediaType(): string
459
    {
460
        return $this->media_type;
461
    }
462
463
    public function setMediaType(string $media_type): self
464
    {
465
        $this->media_type = $media_type;
466
        return $this;
467
    }
468
}