Set::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 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
namespace i3Soft\CDA\DataType\Collection;
28
29
use i3Soft\CDA\ClinicalDocument as CDA;
30
use i3Soft\CDA\DataType\AnyType;
31
use i3Soft\CDA\DataType\Identifier\InstanceIdentifier;
32
use i3Soft\CDA\Interfaces\ElementInterface;
33
use i3Soft\CDA\Interfaces\UseAttributeInterface;
34
35
/**
36
 * Set of elements.
37
 *
38
 * This class restrict the sub element to the same element name. This name
39
 * cannot be changed after the construction of this class.
40
 *
41
 * Example of initializsation :
42
 *
43
 * ```
44
 * use i3Soft\CDA\DataType\Name\PersonName;
45
 *
46
 * new Set(PersonName::class);
47
 * ```
48
 *
49
 * @author Julien Fastré <[email protected]>
50
 */
51
class Set extends AnyType implements \IteratorAggregate
52
{
53
  /**
54
   * The contained elements
55
   *
56
   * @var mixed[]
57
   */
58
  protected $elements = array();
59
60
  /**
61
   * @var string
62
   */
63
  private $elementName;
64
65
  /**
66
   *
67
   * @param string $elementName the class of the element to restrict
68
   */
69
  public function __construct ($elementName)
70
  {
71
    $this->elementName = $elementName;
72
  }
73
74
  public static function fromString ($root, $extension = NULL)
75
  {
76
    $set = new Set(InstanceIdentifier::class);
77
    $set->add(new InstanceIdentifier($root, $extension));
78
    return $set;
79
  }
80
81
  /**
82
   * @param $el
83
   *
84
   * @return self
85
   */
86
  public function add ($el): self
87
  {
88
    if (!$el instanceof $this->elementName)
89
    {
90
      throw new \InvalidArgumentException(sprintf('The given element should be '
91
                                                  . 'an instance of %s, %s given', $this->elementName, \get_class($el)));
92
    }
93
94
    $this->elements[] = $el;
95
96
    return $this;
97
  }
98
99
  /**
100
   * check that the Set contains element, or throws an \InvalidArgumentException
101
   *
102
   * Example usage :
103
   *
104
   * ```
105
   * public function setIds(Set $ids)
106
   * {
107
   *      $ids->checkContainsOrThrow(InstanceIdentifier::class);
108
   *      $this->ids = $ids;
109
   *
110
   *      return $this;
111
   * }
112
   * ```
113
   *
114
   * @param string $name
115
   *
116
   * @return boolean
117
   * @throws \InvalidArgumentException
118
   */
119
  public function checkContainsOrThrow ($name): bool
120
  {
121
    if ($name !== $this->getElementName())
122
    {
123
      throw new \InvalidArgumentException(sprintf('The Set should countains %s'
124
                                                  . ' but contains %s', $name, $this->getElementName()));
125
    }
126
127
    return TRUE;
128
  }
129
130
  /**
131
   * @return string
132
   */
133
  public function getElementName (): string
134
  {
135
    return $this->elementName;
136
  }
137
138
  /**
139
   * @param \DOMElement       $el
140
   * @param \DOMDocument|NULL $doc
141
   */
142
  public function setValueToElement (\DOMElement $el, \DOMDocument $doc)
143
  {
144
    if (\count($this->elements) === 0)
145
    {
146
      return;
147
    }
148
149
    if ($this->elements[0] instanceof AnyType)
150
    {
151
      foreach ($this->elements as $sub)
152
      {
153
        $sub->setValueToElement($el, $doc);
154
      }
155
    }
156
    elseif ($this->elements[0] instanceof ElementInterface)
157
    {
158
      foreach ($this->elements as $sub)
159
      {
160
        $el->appendChild($sub->toDOMElement($doc));
161
        if ($sub instanceof UseAttributeInterface
162
            && FALSE === empty($sub->getUseAttribute()))
163
        {
164
          $el->setAttribute(CDA::getNS() . 'use', $sub->getUseAttribute());
165
        }
166
      }
167
    }
168
    else
169
    {
170
      throw new \LogicException(sprintf(
171
        'the elements added to set are '
172
        . 'not instance of %s nor %s',
173
        AnyType::class,
174
        ElementInterface::class
175
      ));
176
    }
177
  }
178
179
  /**
180
   * @return \Traversable
181
   */
182
  public function getIterator (): \Traversable
183
  {
184
    foreach ($this->get() as $el)
185
    {
186
      yield $el;
187
    }
188
  }
189
190
  /**
191
   * @return mixed[]
192
   */
193
  public function get (): array
194
  {
195
    return $this->elements;
196
  }
197
}
198