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