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

SingleComponent   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toDOMElement() 0 7 2
A __construct() 0 9 3
A getElementTag() 0 3 1
A addSection() 0 5 1
A getSections() 0 3 1
A setSections() 0 11 3
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)) {
57
            $this->setSections($sections);
58
        } elseif ($sections instanceof Section) {
59
            $this->addSection($sections);
60
        }
61
    }
62
63
    /**
64
     *
65
     * @param Section $section
66
     *
67
     * @return self
68
     */
69
    public function addSection(Section $section): self
70
    {
71
        $this->sections[] = $section;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param \DOMDocument $doc
78
     *
79
     * @return \DOMElement
80
     */
81
    public function toDOMElement(\DOMDocument $doc): \DOMElement
82
    {
83
        $component = $this->createElement($doc);
84
        foreach ($this->getSections() as $section) {
85
            $component->appendChild($section->toDOMElement($doc));
86
        }
87
        return $component;
88
    }
89
90
    /**
91
     *
92
     * @return Section[]
93
     */
94
    public function getSections(): array
95
    {
96
        return $this->sections;
97
    }
98
99
    /**
100
     *
101
     * @param Section[] $sections
102
     *
103
     * @return self
104
     */
105
    public function setSections($sections): self
106
    {
107
        $this->sections = array();
108
109
        foreach ($sections as $section) {
110
            if ($section instanceof Section) {
111
                $this->addSection($section);
112
            }
113
        }
114
115
        return $this;
116
    }
117
118
    /**
119
     * get the element tag name
120
     *
121
     * @return string
122
     */
123
    protected function getElementTag(): string
124
    {
125
        return 'component';
126
    }
127
}
128