Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
45 / 50 |
|
83.33% |
10 / 12 |
CRAP | |
0.00% |
0 / 1 |
| AbstractTagParser | |
90.00% |
45 / 50 |
|
83.33% |
10 / 12 |
29.84 | |
0.00% |
0 / 1 |
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getModel | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| getStructByName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStructByNameAndType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMethodByName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseSchema | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseTagAttributes | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
| getParseTagAttributeMethod | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| parseTagAttributeType | |
66.67% |
8 / 12 |
|
0.00% |
0 / 1 |
12.00 | |||
| parseTagAttributeName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseTagAttributeAbstract | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseTagAttributeValue | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace WsdlToPhp\PackageGenerator\Parser\Wsdl; |
| 6 | |
| 7 | use WsdlToPhp\DomHandler\AbstractAttributeHandler; |
| 8 | use WsdlToPhp\DomHandler\AttributeHandler; |
| 9 | use WsdlToPhp\PackageGenerator\Model\AbstractModel; |
| 10 | use WsdlToPhp\PackageGenerator\Model\Method; |
| 11 | use WsdlToPhp\PackageGenerator\Model\Schema; |
| 12 | use WsdlToPhp\PackageGenerator\Model\Struct; |
| 13 | use WsdlToPhp\PackageGenerator\Model\StructAttribute; |
| 14 | use WsdlToPhp\PackageGenerator\Model\StructValue; |
| 15 | use WsdlToPhp\PackageGenerator\Model\Wsdl; |
| 16 | use WsdlToPhp\WsdlHandler\Tag\AbstractTag as Tag; |
| 17 | use WsdlToPhp\WsdlHandler\Wsdl as WsdlDocument; |
| 18 | |
| 19 | abstract class AbstractTagParser extends AbstractParser |
| 20 | { |
| 21 | public function getName(): string |
| 22 | { |
| 23 | return $this->parsingTag(); |
| 24 | } |
| 25 | |
| 26 | protected function getModel(Tag $tag, string $type = ''): ?AbstractModel |
| 27 | { |
| 28 | switch ($tag->getName()) { |
| 29 | case WsdlDocument::TAG_OPERATION: |
| 30 | $model = $this->getMethodByName($tag->getAttributeName()); |
| 31 | |
| 32 | break; |
| 33 | |
| 34 | default: |
| 35 | if (empty($type)) { |
| 36 | $model = $this->getStructByName($tag->getAttributeName()); |
| 37 | } else { |
| 38 | $model = $this->getStructByNameAndType($tag->getAttributeName(), $type); |
| 39 | } |
| 40 | |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | return $model; |
| 45 | } |
| 46 | |
| 47 | protected function getStructByName(string $name): ?Struct |
| 48 | { |
| 49 | return $this->generator->getStructByName($name); |
| 50 | } |
| 51 | |
| 52 | protected function getStructByNameAndType(string $name, string $type): ?Struct |
| 53 | { |
| 54 | return $this->generator->getStructByNameAndType($name, $type); |
| 55 | } |
| 56 | |
| 57 | protected function getMethodByName(string $name): ?Method |
| 58 | { |
| 59 | return $this->generator->getServiceMethod($name); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Most of he time, this method is not used, even if it used, |
| 64 | * for now, knowing that we are in a schema is not a useful information, |
| 65 | * so we can simply parse the tag with only the wsdl as parameter. |
| 66 | */ |
| 67 | protected function parseSchema(Wsdl $wsdl, Schema $schema): void |
| 68 | { |
| 69 | $this->parseWsdl($wsdl); |
| 70 | } |
| 71 | |
| 72 | protected function parseTagAttributes(Tag $tag, AbstractModel $model = null, StructAttribute $structAttribute = null): void |
| 73 | { |
| 74 | $model = $model instanceof AbstractModel ? $model : $this->getModel($tag); |
| 75 | if (!$model) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | /** @var AbstractAttributeHandler $attribute */ |
| 80 | foreach ($tag->getAttributes() as $attribute) { |
| 81 | $methodToCall = $this->getParseTagAttributeMethod($attribute->getName()); |
| 82 | if (is_array($methodToCall)) { |
| 83 | call_user_func_array($methodToCall, [ |
| 84 | $attribute, |
| 85 | $model, |
| 86 | $structAttribute, |
| 87 | ]); |
| 88 | } else { |
| 89 | ($structAttribute ?? $model)->addMeta($attribute->getName(), $attribute->getValue(true)); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | protected function getParseTagAttributeMethod(string $tagName): ?array |
| 95 | { |
| 96 | $methodName = sprintf('parseTagAttribute%s', ucfirst($tagName)); |
| 97 | if (method_exists($this, $methodName)) { |
| 98 | return [ |
| 99 | $this, |
| 100 | $methodName, |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | protected function parseTagAttributeType(AttributeHandler $tagAttribute, AbstractModel $model, StructAttribute $structAttribute = null): void |
| 108 | { |
| 109 | if ($structAttribute instanceof StructAttribute) { |
| 110 | $type = $tagAttribute->getValue(); |
| 111 | if (is_null($type)) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $typeModel = $this->generator->getStructByName($type); |
| 116 | $modelAttributeType = $structAttribute->getType(); |
| 117 | |
| 118 | if ($typeModel instanceof Struct && (empty($modelAttributeType) || 'unknown' === mb_strtolower($modelAttributeType))) { |
| 119 | if ($typeModel->isRestriction()) { |
| 120 | $structAttribute->setType($typeModel->getName()); |
| 121 | } elseif (!$typeModel->isStruct() && $typeModel->getInheritance()) { |
| 122 | $structAttribute->setType($typeModel->getInheritance()); |
| 123 | } |
| 124 | } |
| 125 | } else { |
| 126 | $model->addMeta($tagAttribute->getName(), $tagAttribute->getValue(true)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Avoid the "name" attribute to be added as meta. |
| 132 | */ |
| 133 | protected function parseTagAttributeName(): void |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | protected function parseTagAttributeAbstract(AttributeHandler $tagAttribute, AbstractModel $model): void |
| 138 | { |
| 139 | $model->setAbstract($tagAttribute->getValue(false, true, 'bool')); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Enumeration does not need its own value as meta information, it's like the name for struct attribute. |
| 144 | */ |
| 145 | protected function parseTagAttributeValue(AttributeHandler $tagAttribute, AbstractModel $model): void |
| 146 | { |
| 147 | if ($model instanceof StructValue) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | $model->addMeta($tagAttribute->getName(), $tagAttribute->getValue(true)); |
| 152 | } |
| 153 | } |