Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
StructEnum
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
10 / 10
18
100.00% covered (success)
100.00%
1 / 1
 setModel
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 addClassElement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fillClassConstants
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 defineUseStatements
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConstantAnnotationBlock
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 fillClassMethods
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethodAnnotationBlock
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getEnumMethodGetValidValues
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getEnumMethodValues
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getEnumGetValidValuesAnnotationBlock
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\File;
6
7use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
8use WsdlToPhp\PackageGenerator\Model\AbstractModel;
9use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
10use WsdlToPhp\PackageGenerator\Model\StructValue as StructValueModel;
11use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
12use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
13use WsdlToPhp\PhpGenerator\Element\PhpConstant;
14use WsdlToPhp\PhpGenerator\Element\PhpMethod;
15
16final class StructEnum extends Struct
17{
18    public const METHOD_VALUE_IS_VALID = 'valueIsValid';
19    public const METHOD_GET_VALID_VALUES = 'getValidValues';
20
21    public function setModel(AbstractModel $model): self
22    {
23        if ($model instanceof StructModel && !$model->isRestriction()) {
24            throw new \InvalidArgumentException('Model must be a restriction containing values', __LINE__);
25        }
26
27        return parent::setModel($model);
28    }
29
30    protected function addClassElement(): AbstractModelFile
31    {
32        return AbstractModelFile::addClassElement();
33    }
34
35    protected function fillClassConstants(ConstantContainer $constants): void
36    {
37        /** @var StructModel $model */
38        $model = $this->getModel();
39        foreach ($model->getValues() as $value) {
40            $constants->add(new PhpConstant($value->getCleanName(), $value->getValue()));
41        }
42    }
43
44    protected function defineUseStatements(): self
45    {
46        return AbstractModelFile::defineUseStatements();
47    }
48
49    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
50    {
51        $block = new PhpAnnotationBlock([
52            sprintf('Constant for value \'%s\'', $constant->getValue()),
53        ]);
54
55        /** @var StructModel $model */
56        $model = $this->getModel();
57        if (($value = $model->getValue($constant->getValue())) instanceof StructValueModel) {
58            $this->defineModelAnnotationsFromWsdl($block, $value);
59        }
60        $block->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('string \'%s\'', $constant->getValue())));
61
62        return $block;
63    }
64
65    protected function fillClassMethods(): void
66    {
67        $this->methods->add($this->getEnumMethodGetValidValues());
68    }
69
70    protected function getMethodAnnotationBlock(PhpMethod $method): ?PhpAnnotationBlock
71    {
72        $block = null;
73
74        switch ($method->getName()) {
75            case self::METHOD_GET_VALID_VALUES:
76                $block = $this->getEnumGetValidValuesAnnotationBlock();
77
78                break;
79        }
80
81        return $block;
82    }
83
84    protected function getEnumMethodGetValidValues(): PhpMethod
85    {
86        $method = new PhpMethod(self::METHOD_GET_VALID_VALUES, [], self::TYPE_ARRAY, PhpMethod::ACCESS_PUBLIC, false, true);
87        $validValues = $this->getEnumMethodValues();
88        $method->addChild('return [');
89        foreach ($validValues as $validValue) {
90            $method->addChild(sprintf('%s,', $method->getIndentedString($validValue, 1)));
91        }
92        $method->addChild('];');
93
94        return $method;
95    }
96
97    protected function getEnumMethodValues(): array
98    {
99        $values = [];
100
101        /** @var StructModel $model */
102        $model = $this->getModel();
103        foreach ($model->getValues() as $value) {
104            $values[] = sprintf('self::%s', $value->getCleanName());
105        }
106
107        return $values;
108    }
109
110    protected function getEnumGetValidValuesAnnotationBlock(): PhpAnnotationBlock
111    {
112        $annotationBlock = new PhpAnnotationBlock([
113            'Return allowed values',
114        ]);
115        foreach ($this->getEnumMethodValues() as $value) {
116            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, $value));
117        }
118        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'string[]'));
119
120        return $annotationBlock;
121    }
122}