1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\XML\XSDReader\Schema\Element; |
3
|
|
|
|
4
|
|
|
use DOMElement; |
5
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItemTrait; |
6
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Schema; |
7
|
|
|
use GoetasWebservices\XML\XSDReader\SchemaReader; |
8
|
|
|
use GoetasWebservices\XML\XSDReader\SchemaReaderLoadAbstraction; |
9
|
|
|
|
10
|
|
|
class Group implements ElementItem, ElementContainer |
11
|
|
|
{ |
12
|
|
|
use AttributeItemTrait; |
13
|
|
|
use ElementContainerTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @var Schema |
18
|
|
|
*/ |
19
|
|
|
protected $schema; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
protected $doc; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $name |
28
|
|
|
*/ |
29
|
135 |
|
public function __construct(Schema $schema, $name) |
30
|
|
|
{ |
31
|
135 |
|
$this->schema = $schema; |
32
|
135 |
|
$this->name = $name; |
33
|
135 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string|null |
37
|
|
|
*/ |
38
|
|
|
public function getDoc() |
39
|
|
|
{ |
40
|
|
|
return $this->doc; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $doc |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
135 |
|
public function setDoc($doc) |
49
|
|
|
{ |
50
|
135 |
|
$this->doc = $doc; |
51
|
135 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return Schema |
56
|
|
|
*/ |
57
|
135 |
|
public function getSchema() |
58
|
|
|
{ |
59
|
135 |
|
return $this->schema; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Group|GroupRef |
64
|
|
|
*/ |
65
|
135 |
|
protected static function loadGroupBeforeCheckingChildNodes( |
66
|
|
|
Schema $schema, |
67
|
|
|
DOMElement $node |
68
|
|
|
) { |
69
|
135 |
|
$group = new Group($schema, $node->getAttribute("name")); |
70
|
135 |
|
$group->setDoc(SchemaReader::getDocumentation($node)); |
71
|
|
|
|
72
|
135 |
|
if ($node->hasAttribute("maxOccurs")) { |
73
|
|
|
/** |
74
|
|
|
* @var GroupRef $group |
75
|
|
|
*/ |
76
|
|
|
$group = SchemaReader::maybeSetMax(new GroupRef($group), $node); |
77
|
|
|
} |
78
|
135 |
|
if ($node->hasAttribute("minOccurs")) { |
79
|
|
|
/** |
80
|
|
|
* @var GroupRef $group |
81
|
|
|
*/ |
82
|
|
|
$group = SchemaReader::maybeSetMin( |
83
|
|
|
$group instanceof GroupRef ? $group : new GroupRef($group), |
84
|
|
|
$node |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
135 |
|
$schema->addGroup($group); |
89
|
|
|
|
90
|
135 |
|
return $group; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return \Closure |
95
|
|
|
*/ |
96
|
135 |
|
public static function loadGroup( |
97
|
|
|
SchemaReaderLoadAbstraction $reader, |
98
|
|
|
Schema $schema, |
99
|
|
|
DOMElement $node |
100
|
|
|
) { |
101
|
135 |
|
$group = static::loadGroupBeforeCheckingChildNodes( |
102
|
135 |
|
$schema, |
103
|
90 |
|
$node |
104
|
45 |
|
); |
105
|
90 |
|
static $methods = [ |
106
|
|
|
'sequence' => 'loadSequence', |
107
|
|
|
'choice' => 'loadSequence', |
108
|
|
|
'all' => 'loadSequence', |
109
|
45 |
|
]; |
110
|
|
|
|
111
|
135 |
|
return function () use ($reader, $group, $node, $methods) { |
112
|
135 |
|
$limit = $node->childNodes->length; |
113
|
135 |
View Code Duplication |
for ($i = 0; $i < $limit; $i += 1) { |
|
|
|
|
114
|
135 |
|
$childNode = $node->childNodes->item($i); |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var string[] $methods |
118
|
|
|
*/ |
119
|
135 |
|
$methods = $methods; |
120
|
|
|
|
121
|
135 |
|
$reader->maybeCallMethod( |
122
|
135 |
|
$methods, |
123
|
135 |
|
(string) $childNode->localName, |
124
|
135 |
|
$childNode, |
125
|
135 |
|
$group, |
126
|
90 |
|
$childNode |
127
|
45 |
|
); |
128
|
45 |
|
} |
129
|
135 |
|
}; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.