Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TagChoice
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
14
100.00% covered (success)
100.00%
1 / 1
 parseChoice
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
7
 parseWsdl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 parsingTag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseChoiceChild
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\Parser\Wsdl;
6
7use WsdlToPhp\PackageGenerator\Model\Struct;
8use WsdlToPhp\PackageGenerator\Model\StructAttribute;
9use WsdlToPhp\PackageGenerator\Model\Wsdl;
10use WsdlToPhp\WsdlHandler\Tag\AbstractTag;
11use WsdlToPhp\WsdlHandler\Tag\TagChoice as Choice;
12use WsdlToPhp\WsdlHandler\Wsdl as WsdlDocument;
13
14final class TagChoice extends AbstractTagParser
15{
16    /**
17     * @see https://www.w3schools.com/xml/el_choice.asp
18     * @see https://www.w3.org/TR/xmlschema11-1/#element-choice
19     */
20    public function parseChoice(Choice $choice): void
21    {
22        $parent = $choice->getSuitableParent();
23        $children = $choice->getChildrenElements();
24        if ($parent && count($children) && ($struct = $this->getModel($parent)) instanceof Struct) {
25            $unionNames = [];
26            foreach ($children as $child) {
27                $unionNames[] = $child->getAttributeName() ? $child->getAttributeName() : $child->getAttributeRef();
28            }
29            foreach ($children as $child) {
30                $this->parseChoiceChild($choice, $unionNames, $child, $struct);
31            }
32            unset($unionNames);
33        }
34    }
35
36    protected function parseWsdl(Wsdl $wsdl): void
37    {
38        foreach ($this->getTags() as $tag) {
39            $this->parseChoice($tag);
40        }
41    }
42
43    protected function parsingTag(): string
44    {
45        return WsdlDocument::TAG_CHOICE;
46    }
47
48    protected function parseChoiceChild(Choice $choice, array $choiceNames, AbstractTag $child, Struct $struct): void
49    {
50        $attributeName = $child->getAttributeName();
51        if (empty($attributeName) && ($attributeRef = $child->getAttributeRef())) {
52            $attributeName = $attributeRef;
53        }
54
55        if (($structAttribute = $struct->getAttribute($attributeName)) instanceof StructAttribute) {
56            $structAttribute
57                ->setContainsElements($choice->canOccurSeveralTimes())
58                ->addMeta('choice', $choiceNames)
59                ->addMeta('choiceMaxOccurs', $choice->getMaxOccurs())
60                ->addMeta('choiceMinOccurs', $choice->getMinOccurs())
61            ;
62        }
63    }
64}