Completed
Push — develop ( 2b7e50...d1ae17 )
by Mikaël
23:57
created

StructEnum::getClassMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
6
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
7
use WsdlToPhp\PackageGenerator\Model\StructValue as StructValueModel;
8
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
9
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
10
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
11
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
12
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
13
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
14
15
class StructEnum extends Struct
16
{
17
    /**
18
     * @var string
19
     */
20
    const METHOD_VALUE_IS_VALID = 'valueIsValid';
21
    /**
22
     * @var string
23
     */
24
    const METHOD_GET_VALID_VALUES = 'getValidValues';
25
    /**
26
     * @param ConstantContainer $constants
27
     */
28 78
    protected function getClassConstants(ConstantContainer $constants)
29
    {
30 78
        foreach ($this->getModel()->getValues() as $value) {
31 78
            $constants->add(new PhpConstant($value->getCleanName(), $value->getValue()));
32 39
        }
33 78
    }
34
    /**
35
     * @param PhpConstant $constant
36
     * @return PhpAnnotationBlock
37
     */
38 78
    protected function getConstantAnnotationBlock(PhpConstant $constant)
39
    {
40 78
        $block = new PhpAnnotationBlock([
41 78
            sprintf('Constant for value \'%s\'', $constant->getValue()),
42 39
        ]);
43 78
        if (($value = $this->getModel()->getValue($constant->getValue())) instanceof StructValueModel) {
44 78
            $this->defineModelAnnotationsFromWsdl($block, $value);
45 39
        }
46 78
        $block->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('string \'%s\'', $constant->getValue())));
47 78
        return $block;
48
    }
49 78
    protected function fillClassMethods()
50
    {
51 78
        $this->methods->add($this->getEnumMethodValueIsValid())->add($this->getEnumMethodGetValidValues());
52 78
    }
53
    /**
54
     * @param PhpMethod $method
55
     * @return PhpAnnotationBlock|null
56
     */
57 78
    protected function getMethodAnnotationBlock(PhpMethod $method)
58
    {
59 78
        $block = null;
60 78
        switch ($method->getName()) {
61 78
            case self::METHOD_GET_VALID_VALUES:
62 78
                $block = $this->getEnumGetValidValuesAnnotationBlock();
63 78
                break;
64 78
            case self::METHOD_VALUE_IS_VALID:
65 78
                $block = $this->getEnumValueIsValidAnnotationBlock();
66 78
                break;
67 39
        }
68 78
        return $block;
69
    }
70
    /**
71
     * @return PhpMethod
72
     */
73 78
    protected function getEnumMethodValueIsValid()
74
    {
75 78
        $method = new PhpMethod(self::METHOD_VALUE_IS_VALID, [
76 78
            'value',
77 78
        ], PhpMethod::ACCESS_PUBLIC, false, true);
78 78
        $method->addChild(sprintf('return ($value === null) || in_array($value, self::%s(), true);', self::METHOD_GET_VALID_VALUES));
79 78
        return $method;
80
    }
81
    /**
82
     * @return PhpMethod
83
     */
84 78
    protected function getEnumMethodGetValidValues()
85
    {
86 78
        $method = new PhpMethod(self::METHOD_GET_VALID_VALUES, [], PhpMethod::ACCESS_PUBLIC, false, true);
87 78
        $validValues = $this->getEnumMethodValues();
88 78
        $method->addChild('return array(');
89 78
        foreach ($validValues as $validValue) {
90 78
            $method->addChild(sprintf('%s,', $method->getIndentedString($validValue, 1)));
91 39
        }
92 78
        $method->addChild(');');
93 78
        return $method;
94
    }
95
    /**
96
     * @return string[]
97
     */
98 78
    protected function getEnumMethodValues()
99
    {
100 78
        $values = [];
101 78
        foreach ($this->getModel()->getValues() as $value) {
102 78
            $values[] = sprintf('self::%s', $value->getCleanName());
103 39
        }
104 78
        return $values;
105
    }
106
    /**
107
     * @return PhpAnnotationBlock
108
     */
109 78
    protected function getEnumValueIsValidAnnotationBlock()
110
    {
111 78
        $annotationBlock = new PhpAnnotationBlock([
112 78
            'Return true if value is allowed',
113 39
        ]);
114 78
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('self::%s()', self::METHOD_GET_VALID_VALUES)))->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, 'mixed $value value'))->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'bool true|false'));
115 78
        return $annotationBlock;
116
    }
117
    /**
118
     * @return PhpAnnotationBlock
119
     */
120 78
    protected function getEnumGetValidValuesAnnotationBlock()
121
    {
122 78
        $annotationBlock = new PhpAnnotationBlock([
123 78
            'Return allowed values',
124 39
        ]);
125 78
        foreach ($this->getEnumMethodValues() as $value) {
126 78
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, $value));
127 39
        }
128 78
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'string[]'));
129 78
        return $annotationBlock;
130
    }
131
    /**
132
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
133
     * @throws \InvalidArgumentException
134
     * @param AbstractModel $model
135
     * @return StructArray
136
     */
137 90
    public function setModel(AbstractModel $model)
138
    {
139 90
        if ($model instanceof StructModel && !$model->isRestriction()) {
140 6
            throw new \InvalidArgumentException('Model must be a restriction containing values', __LINE__);
141
        }
142 84
        return parent::setModel($model);
143
    }
144
}
145