Issues (113)

lib/Component/SingleComponent.php (2 issues)

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\Component;
27
28
use i3Soft\CDA\Component\SingleComponent\Section;
29
use i3Soft\CDA\Interfaces\TypeCodeInterface;
30
use i3Soft\CDA\Traits\TypeCodeTrait;
31
32
/**
33
 * Single component. Must be included in a XMLBodyComponent.
34
 *
35
 * Will return a `<component>` element node.
36
 *
37
 * @author Julien Fastré <[email protected]>
38
 */
39
class SingleComponent extends AbstractComponent implements TypeCodeInterface
40
{
41
  use TypeCodeTrait;
42
43
  /** @var Section[] */
44
  private $sections;
45
46
  /**
47
   * SingleComponent constructor.
48
   *
49
   * @param null $sections
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $sections is correct as it would always require null to be passed?
Loading history...
50
   */
51
  public function __construct ($sections = NULL)
52
  {
53
    $this->setAcceptableTypeCodes(['', TypeCodeInterface::COMPONENT])
54
      ->setTypeCode(TypeCodeInterface::COMPONENT);
55
    $this->sections = array();
56
    if (\is_array($sections))
0 ignored issues
show
The condition is_array($sections) is always false.
Loading history...
57
    {
58
      $this->setSections($sections);
59
    }
60
    elseif ($sections instanceof Section)
61
    {
62
      $this->addSection($sections);
63
    }
64
  }
65
66
  /**
67
   *
68
   * @param Section $section
69
   *
70
   * @return self
71
   */
72
  public function addSection (Section $section): self
73
  {
74
    $this->sections[] = $section;
75
76
    return $this;
77
  }
78
79
  /**
80
   * @param \DOMDocument $doc
81
   *
82
   * @return \DOMElement
83
   */
84
  public function toDOMElement (\DOMDocument $doc): \DOMElement
85
  {
86
    $component = $this->createElement($doc);
87
    foreach ($this->getSections() as $section)
88
    {
89
      $component->appendChild($section->toDOMElement($doc));
90
    }
91
    return $component;
92
  }
93
94
  /**
95
   *
96
   * @return Section[]
97
   */
98
  public function getSections (): array
99
  {
100
    return $this->sections;
101
  }
102
103
  /**
104
   *
105
   * @param Section[] $sections
106
   *
107
   * @return self
108
   */
109
  public function setSections ($sections): self
110
  {
111
    $this->sections = array();
112
113
    foreach ($sections as $section)
114
    {
115
      if ($section instanceof Section)
116
      {
117
        $this->addSection($section);
118
      }
119
    }
120
121
    return $this;
122
  }
123
124
  /**
125
   * get the element tag name
126
   *
127
   * @return string
128
   */
129
  protected function getElementTag (): string
130
  {
131
    return 'component';
132
  }
133
}
134