Set::getElementName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Julien Fastré <[email protected]>.
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 NONINFRINGEMENT. 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
namespace PHPHealth\CDA\DataType\Collection;
28
29
use PHPHealth\CDA\DataType\AnyType;
30
31
/**
32
 * Set of elements.
33
 *
34
 * This class restrict the sub element to the same element name. This name
35
 * cannot be changed after the construction of this class.
36
 *
37
 * Example of initializsation :
38
 *
39
 * ```
40
 * use PHPHealth\CDA\DataType\Name\PersonName;
41
 *
42
 * new Set(PersonName::class);
43
 * ```
44
 *
45
 * @author Julien Fastré <[email protected]>
46
 */
47
class Set extends AnyType implements \IteratorAggregate
48
{
49
    /**
50
     * The contained elements
51
     *
52
     * @var mixed[]
53
     */
54
    protected $elements = array();
55
    
56
    private $elementName;
57
    
58
    /**
59
     *
60
     * @param string $elementName the class of the element to restrict
61
     */
62
    public function __construct($elementName)
63
    {
64
        $this->elementName = $elementName;
65
    }
66
    
67
    public function getElementName()
68
    {
69
        return $this->elementName;
70
    }
71
        
72
    public function add($el)
73
    {
74
        if (!$el instanceof $this->elementName) {
75
            throw new \InvalidArgumentException(sprintf("The given element should be "
76
                    . "an instance of %s, %s given", $this->elementName, get_class($el)));
77
        }
78
        
79
        $this->elements[] = $el;
80
        
81
        return $this;
82
    }
83
    
84
    /**
85
     * check that the Set contains element, or throws an \InvalidArgumentException
86
     * 
87
     * Example usage :
88
     * 
89
     * ```
90
     * public function setIds(Set $ids)
91
     * {
92
     *      $ids->checkContainsOrThrow(InstanceIdentifier::class);
93
     *      $this->ids = $ids;
94
     * 
95
     *      return $this;
96
     * }
97
     * ```
98
     * 
99
     * @param string $name
100
     * @return boolean
101
     * @throws \InvalidArgumentException
102
     */
103
    public function checkContainsOrThrow($name)
104
    {
105
        if  ($name !== $this->getElementName()) {
106
            throw new \InvalidArgumentException(sprintf("The Set should countains %s"
107
                . " but contains %s", $name, $this->getElementName()));
108
        } else {
109
            return true;
110
        }
111
    }
112
    
113
    /**
114
     * @return mixed[]
115
     */
116
    public function get()
117
    {
118
        return $this->elements;
119
    }
120
    
121
    public function setValueToElement(\DOMElement &$el, \DOMDocument $doc = null)
122
    {
123
        if (count($this->elements) === 0) {
124
            return;
125
        }
126
        
127
        if ($this->elements[0] instanceof AnyType) {
128
            foreach ($this->elements as $sub) {
129
                $sub->setValueToElement($el, $doc);
130
            }
131
        } elseif ($this->elements[0] instanceof \PHPHealth\CDA\ElementInterface) {
132
            foreach ($this->elements as $sub) {
133
                $el->appendChild($sub->toDOMElement($doc));
134
            }
135
        } else {
136
            throw new \LogicException(sprintf(
137
                "the elements added to set are "
138
                    . "not instance of %s nor %s",
139
                AnyType::class,
140
                \PHPHealth\CDA\ElementInterface::class
141
            ));
142
        }
143
    }
144
145
    public function getIterator(): \Traversable
146
    {
147
        foreach ($this->get() as $el) {
148
            yield $el;
149
        }
150
    }
151
}
152