Completed
Push — develop ( 6dbf52...b257fa )
by Mikaël
48:47
created

StructArray::getStructMethodAnnotationBlock()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 29
cts 29
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 27
nc 8
nop 1
crap 8
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\PhpGenerator\Element\PhpAnnotation;
8
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
9
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
10
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
11
12
class StructArray extends Struct
13
{
14
    /**
15
     * @var string
16
     */
17
    const METHOD_GET_ATTRIBUTE_NAME = 'getAttributeName';
18
    /**
19
     * @var string
20
     */
21
    const METHOD_CURRENT = 'current';
22
    /**
23
     * @var string
24
     */
25
    const METHOD_ITEM = 'item';
26
    /**
27
     * @var string
28
     */
29
    const METHOD_FIRST = 'first';
30
    /**
31
     * @var string
32
     */
33
    const METHOD_LAST = 'last';
34
    /**
35
     * @var string
36
     */
37
    const METHOD_OFFSET_GET = 'offsetGet';
38
    /**
39
     * @var string
40
     */
41
    const METHOD_ADD = 'add';
42
    /**
43
     * @see \WsdlToPhp\PackageGenerator\File\Struct::addStructMethodsSetAndGet()
44
     */
45 48
    public function addStructMethodsSetAndGet(MethodContainer $methods)
46
    {
47 48
        parent::addStructMethodsSetAndGet($methods);
48 48
        $this->addArrayMethodCurrent($methods)
49 48
            ->addArrayMethodItem($methods)
50 48
            ->addArrayMethodFirst($methods)
51 48
            ->addArrayMethodLast($methods)
52 48
            ->addArrayMethodOffsetGet($methods)
53 48
            ->addArrayMethodAdd($methods)
54 48
            ->addArrayMethodGetAttributeName($methods);
55 48
        return $this;
56
    }
57
    /**
58
     * @param MethodContainer $methods
59
     * @return StructArray
60
     */
61 48
    protected function addArrayMethodCurrent(MethodContainer $methods)
62
    {
63 48
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_CURRENT, $this->getArrayMethodBody(self::METHOD_CURRENT));
64
    }
65
    /**
66
     * @param MethodContainer $methods
67
     * @return StructArray
68
     */
69 48
    protected function addArrayMethodItem(MethodContainer $methods)
70
    {
71 48
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_ITEM, $this->getArrayMethodBody(self::METHOD_ITEM, '$index'), array(
72 48
            'index',
73 32
        ));
74
    }
75
    /**
76
     * @param MethodContainer $methods
77
     * @return StructArray
78
     */
79 48
    protected function addArrayMethodFirst(MethodContainer $methods)
80
    {
81 48
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_FIRST, $this->getArrayMethodBody(self::METHOD_FIRST));
82
    }
83
    /**
84
     * @param MethodContainer $methods
85
     * @return StructArray
86
     */
87 48
    protected function addArrayMethodLast(MethodContainer $methods)
88
    {
89 48
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_LAST, $this->getArrayMethodBody(self::METHOD_LAST));
90
    }
91
    /**
92
     * @param MethodContainer $methods
93
     * @return StructArray
94
     */
95 48
    protected function addArrayMethodOffsetGet(MethodContainer $methods)
96
    {
97 48
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_OFFSET_GET, $this->getArrayMethodBody(self::METHOD_OFFSET_GET, '$offset'), array(
98 48
            'offset',
99 32
        ));
100
    }
101
    /**
102
     * @param MethodContainer $methods
103
     * @return StructArray
104
     */
105 48
    protected function addArrayMethodGetAttributeName(MethodContainer $methods)
106
    {
107 48
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_GET_ATTRIBUTE_NAME, sprintf('return \'%s\';', $this->getModel()
108 48
            ->getAttributes()
109 48
            ->offsetGet(0)
110 48
            ->getName()));
111
    }
112
    /**
113
     * @param MethodContainer $methods
114
     * @return StructArray
115
     */
116 48
    protected function addArrayMethodAdd(MethodContainer $methods)
117
    {
118 48
        if ($this->getRestrictionFromStructAttribute() instanceof StructModel) {
119 24
            $method = new PhpMethod(self::METHOD_ADD, array(
120 24
                'item',
121 16
            ));
122 24
            $this->addStructMethodSetBodyForRestriction($method, $this->getModel()->getAttributes()->offsetGet(0), 'item');
123 24
            $method->addChild('return parent::add($item);');
124 24
            $methods->add($method);
125 16
        }
126 48
        return $this;
127
    }
128
    /**
129
     * @param MethodContainer $methods
130
     * @param string $name
131
     * @param string $body
132
     * @param string[] $methodParameters
133
     * @return StructArray
134
     */
135 48
    protected function addArrayMethodGenericMethod(MethodContainer $methods, $name, $body, array $methodParameters = array())
136
    {
137 48
        $method = new PhpMethod($name, $methodParameters);
138 48
        $method->addChild($body);
139 48
        $methods->add($method);
140 48
        return $this;
141
    }
142
    /**
143
     * @return PhpAnnotationBlock
144
     */
145 48
    protected function getArrayMethodGetAttributeNameAnnotationBlock()
146
    {
147 48
        return new PhpAnnotationBlock(array(
148 48
            'Returns the attribute name',
149 48
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::%s()', $this->getModel()->getExtends(true), self::METHOD_GET_ATTRIBUTE_NAME)),
150 48
            new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('string %s', $this->getModel()->getAttributes()->offsetGet(0)->getName())),
151 32
        ));
152
    }
153
    /**
154
     * @return PhpAnnotationBlock
155
     */
156 48
    protected function getArrayMethodCurrentAnnotationBlock()
157
    {
158 48
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_CURRENT, 'Returns the current element');
159
    }
160
    /**
161
     * @return PhpAnnotationBlock
162
     */
163 48
    protected function getArrayMethodFirstAnnotationBlock()
164
    {
165 48
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_FIRST, 'Returns the first element');
166
    }
167
    /**
168
     * @return PhpAnnotationBlock
169
     */
170 48
    protected function getArrayMethodLastAnnotationBlock()
171
    {
172 48
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_LAST, 'Returns the last element');
173
    }
174
    /**
175
     * @return PhpAnnotationBlock
176
     */
177 48
    protected function getArrayMethodItemAnnotationBlock()
178
    {
179 48
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_ITEM, 'Returns the indexed element', 'int $index');
180
    }
181
    /**
182
     * @return PhpAnnotationBlock
183
     */
184 48
    protected function getArrayMethodOffsetGetAnnotationBlock()
185
    {
186 48
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_OFFSET_GET, 'Returns the element at the offset', 'int $offset');
187
    }
188
    /**
189
     * @return PhpAnnotationBlock
190
     */
191 24
    protected function getArrayMethodAddAnnotationBlock()
192
    {
193 24
        return new PhpAnnotationBlock(array(
194 24
            'Add element to array',
195 24
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::add()', $this->getModel()->getExtends(true))),
196 24
            new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'),
197 24
            new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::valueIsValid()', $this->getModelFromStructAttribute()->getPackagedName(true))),
198 24
            new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeType())),
199 24
            new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('%s', $this->getModel()->getPackagedName(true))),
200 16
        ));
201
    }
202
    /**
203
     * @param string $name
204
     * @param string $description
205
     * @param string $param
206
     * @return PhpAnnotationBlock
207
     */
208 48
    protected function getArrayMethodGenericAnnotationBlock($name, $description, $param = null)
209
    {
210 48
        $annotationBlock = new PhpAnnotationBlock(array(
211 48
            $description,
212 48
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::%s()', $this->getModel()->getExtends(true), $name)),
213 32
        ));
214 48
        if (!empty($param)) {
215 48
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, $param));
216 32
        }
217 48
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getStructAttributeTypeGetAnnotation(null, false)));
218 48
        return $annotationBlock;
219
    }
220
    /**
221
     * @param PhpMethod $method
222
     * @return PhpAnnotationBlock|null
223
     */
224 48
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
225
    {
226 48
        switch ($method->getName()) {
227 48
            case self::METHOD_GET_ATTRIBUTE_NAME:
228 48
                $annotationBlock = $this->getArrayMethodGetAttributeNameAnnotationBlock();
229 48
                break;
230 48
            case self::METHOD_CURRENT:
231 48
                $annotationBlock = $this->getArrayMethodCurrentAnnotationBlock();
232 48
                break;
233 48
            case self::METHOD_FIRST:
234 48
                $annotationBlock = $this->getArrayMethodFirstAnnotationBlock();
235 48
                break;
236 48
            case self::METHOD_ITEM:
237 48
                $annotationBlock = $this->getArrayMethodItemAnnotationBlock();
238 48
                break;
239 48
            case self::METHOD_LAST:
240 48
                $annotationBlock = $this->getArrayMethodLastAnnotationBlock();
241 48
                break;
242 48
            case self::METHOD_OFFSET_GET:
243 48
                $annotationBlock = $this->getArrayMethodOffsetGetAnnotationBlock();
244 48
                break;
245 48
            case self::METHOD_ADD:
246 48
                $annotationBlock = $this->getArrayMethodAddAnnotationBlock();
247 48
                break;
248 48
            default:
249 48
                $annotationBlock = parent::getStructMethodAnnotationBlock($method);
250 48
                break;
251 48
        }
252 24
        return $annotationBlock;
253 24
    }
254 32
    /**
255 48
     * @param string $method
256 48
     * @param string $var
257 32
     * @return string
258 48
     */
259
    protected function getArrayMethodBody($method, $var = '')
260
    {
261
        return sprintf('return parent::%s(%s);', $method, $var);
262
    }
263
    /**
264
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
265 48
     * @throws \InvalidArgumentException
266
     * @param AbstractModel $model
267 48
     * @return StructArray
268
     */
269
    public function setModel(AbstractModel $model)
270
    {
271
        if ($model instanceof StructModel && !$model->isArray()) {
272
            throw new \InvalidArgumentException('The model is not a valid array struct (name must contain Array and the model must contain only one property', __LINE__);
273
        }
274
        return parent::setModel($model);
275 66
    }
276
}
277