Completed
Push — develop ( de5d0b...06b47b )
by Mikaël
31:11
created

Struct::addStructMethodSetBodyForArray()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 28
ccs 23
cts 23
cp 1
rs 8.8571
cc 3
eloc 24
nc 3
nop 2
crap 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Struct::addStructMethodGetBodyForXml() 0 15 2
A Struct::addStructMethodGetBody() 0 6 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
6
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel;
7
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
8
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
9
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
10
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
11
use WsdlToPhp\PackageGenerator\Container\Model\StructAttribute as StructAttributeContainer;
12
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
13
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
14
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
15
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
16
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
17
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
18
use WsdlToPhp\PackageGenerator\File\Validation\Rules;
19
20
class Struct extends AbstractModelFile
21
{
22
    /**
23
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants()
24 92
     */
25
    protected function getClassConstants(ConstantContainer $constants)
26 92
    {
27
    }
28
    /**
29
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock()
30
     */
31
    protected function getConstantAnnotationBlock(PhpConstant $constant)
32
    {
33
    }
34
    /**
35
     * @param bool $includeInheritanceAttributes include the attributes of parent class, default parent attributes are not included. If true, then the array is an associative array containing and index "attribute" for the StructAttribute object and an index "model" for the Struct object.
36
     * @param bool $requiredFirst places the required attributes first, then the not required in order to have the _contrust method with the required attribute at first
37
     * @return StructAttributeContainer
38 92
     */
39
    protected function getModelAttributes($includeInheritanceAttributes = false, $requiredFirst = true)
40 92
    {
41
        return $this->getModel()->getAttributes($includeInheritanceAttributes, $requiredFirst);
42
    }
43
    /**
44
     * @param PropertyContainer
45 120
     */
46
    protected function getClassProperties(PropertyContainer $properties)
47 120
    {
48 92
        if ($this->getModel()->getAttributes()->count() > 0) {
49 92
            foreach ($this->getModelAttributes() as $attribute) {
50 69
                $properties->add(new PhpProperty($attribute->getCleanName(), PhpProperty::NO_VALUE));
51 69
            }
52 120
        }
53
    }
54
    /**
55
     * @return PhpAnnotationBlock
56 92
     */
57
    protected function getPropertyAnnotationBlock(PhpProperty $property)
58 92
    {
59 92
        $annotationBlock = new PhpAnnotationBlock();
60 92
        $annotationBlock->addChild(sprintf('The %s', $property->getName()));
61 92
        if (($attribute = $this->getModel()->getAttribute($property->getName())) instanceof StructAttributeModel) {
62 92
            $this->defineModelAnnotationsFromWsdl($annotationBlock, $attribute);
63 69
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_VAR, $this->getStructAttributeTypeSetAnnotation($attribute, true)));
64 92
        }
65
        return $annotationBlock;
66
    }
67
    /**
68
     * @param MethodContainer
69 92
     */
70
    protected function getClassMethods(MethodContainer $methods)
71 69
    {
72 92
        $this
73 92
            ->addStructMethodConstruct($methods)
74 92
            ->addStructMethodsSetAndGet($methods)
75 92
            ->addStructMethodSetState($methods);
76
    }
77
    /**
78
     * @param MethodContainer $methods
79
     * @return Struct
80 92
     */
81
    protected function addStructMethodConstruct(MethodContainer $methods)
82 92
    {
83 92
        $method = new PhpMethod(self::METHOD_CONSTRUCT, $this->getStructMethodParametersValues());
84 92
        $this->addStructMethodConstructBody($method);
85 92
        $methods->add($method);
86
        return $this;
87
    }
88
    /**
89
     * @param PhpMethod $method
90
     * @return Struct
91 92
     */
92
    protected function addStructMethodConstructBody(PhpMethod $method)
93 92
    {
94 92
        $count = $this->getModelAttributes()->count();
95 92
        foreach ($this->getModelAttributes() as $index=>$attribute) {
96 92
            if ($index === 0) {
97 69
                $method->addChild('$this');
98 92
            }
99 69
            $this->addStructMethodConstructBodyForAttribute($method, $attribute, $count - 1 === $index);
100 92
        }
101
        return $this;
102
    }
103
    /**
104
     * @param PhpMethod $method
105
     * @param StructAttributeModel $attribute
106
     * @param bool $isLast
107
     * @return Struct
108 92
     */
109
    protected function addStructMethodConstructBodyForAttribute(PhpMethod $method, StructAttributeModel $attribute, $isLast)
110 92
    {
111 92
        $method->addChild($method->getIndentedString(sprintf('->%s($%s)%s', $attribute->getSetterName(), lcfirst($attribute->getCleanName()), $isLast ? ';' : ''), 1));
112
        return $this;
113
    }
114
    /**
115
     * @return PhpFunctionParameter[]
116 92
     */
117
    protected function getStructMethodParametersValues()
118 92
    {
119 92
        $parametersValues = array();
120 92
        foreach ($this->getModelAttributes() as $attribute) {
121 69
            $parametersValues[] = $this->getStructMethodParameter($attribute, true);
122 92
        }
123
        return $parametersValues;
124
    }
125
    /**
126
     * @param StructAttributeModel $attribute
127
     * @param bool $lowCaseFirstLetter
128
     * @param mixed $defaultValue
129
     * @return PhpFunctionParameter
130 92
     */
131
    protected function getStructMethodParameter(StructAttributeModel $attribute, $lowCaseFirstLetter = false, $defaultValue = null)
132
    {
133 92
        try {
134
            return new PhpFunctionParameter($lowCaseFirstLetter ? lcfirst($attribute->getCleanName()) : $attribute->getCleanName(), isset($defaultValue) ? $defaultValue : $attribute->getDefaultValue(), $this->getStructMethodParameterType($attribute));
135
        } catch (\InvalidArgumentException $exception) {
136
            throw new \InvalidArgumentException(sprintf('Unable to create function parameter for struct "%s" with type "%s" for attribute "%s"', $this->getModel()->getName(), var_export($this->getStructMethodParameterType($attribute), true), $attribute->getName()), __LINE__, $exception);
137
        }
138
    }
139
    /**
140
     * @param StructAttributeModel $attribute
141
     * @param bool $returnArrayType
142
     * @return string|null
143 92
     */
144
    protected function getStructMethodParameterType(StructAttributeModel $attribute, $returnArrayType = true)
145 92
    {
146
        return self::getValidType($this->getStructAttributeTypeHint($attribute, $returnArrayType), null);
147
    }
148
    /**
149
     * @param MethodContainer $methods
150
     * @return Struct
151 92
     */
152
    protected function addStructMethodsSetAndGet(MethodContainer $methods)
153 92
    {
154 69
        foreach ($this->getModelAttributes() as $attribute) {
155 92
            $this
156 92
                ->addStructMethodGet($methods, $attribute)
157 92
                ->addStructMethodSet($methods, $attribute)
158 69
                ->addStructMethodAddTo($methods, $attribute);
159 92
        }
160
        return $this;
161
    }
162
    /**
163
     * @param MethodContainer $methods
164
     * @param StructAttributeModel $attribute
165
     * @return Struct
166 92
     */
167
    protected function addStructMethodAddTo(MethodContainer $methods, StructAttributeModel $attribute)
168 92
    {
169 48
        if ($attribute->isArray()) {
170 48
            $method = new PhpMethod(sprintf('addTo%s', ucfirst($attribute->getCleanName())), array(
171 36
                new PhpFunctionParameter('item', PhpFunctionParameter::NO_VALUE, $this->getStructMethodParameterType($attribute, false)),
172 48
            ));
173 48
            $this->addStructMethodAddToBody($method, $attribute);
174 36
            $methods->add($method);
175 92
        }
176
        return $this;
177
    }
178
    /**
179
     * @param PhpMethod $method
180
     * @param StructAttributeModel $attribute
181
     * @return Struct
182 48
     */
183
    protected function addStructMethodAddToBody(PhpMethod $method, StructAttributeModel $attribute)
184 48
    {
185 16
        if ($this->getGenerator()->getOptionValidation()) {
186 12
            $rules = new Rules($this, $method, $attribute);
187 44
            $rules->applyRules('item', true);
188
        }
189
        $method
190 48
            ->addChild(sprintf('$this->%s[] = $item;', $attribute->getCleanName()))
191 48
            ->addChild('return $this;');
192 48
        return $this;
193
    }
194
    /**
195
     * @param MethodContainer $methods
196
     * @param StructAttributeModel $attribute
197
     * @return Struct
198
     */
199 44
    protected function addStructMethodSet(MethodContainer $methods, StructAttributeModel $attribute)
200
    {
201
        $method = new PhpMethod($attribute->getSetterName(), array(
202 44
            $this->getStructMethodParameter($attribute, true, null),
203 44
        ));
204 44
        $this->addStructMethodSetBody($method, $attribute);
205 44
        $methods->add($method);
206
        return $this;
207
    }
208
    /**
209
     * @param PhpMethod $method
210
     * @param StructAttributeModel $attribute
211
     * @return Struct
212 92
     */
213
    protected function addStructMethodSetBody(PhpMethod $method, StructAttributeModel $attribute)
214 92
    {
215 92
        if ($this->getGenerator()->getOptionValidation()) {
216 69
            $rules = new Rules($this, $method, $attribute);
217 92
            $rules->applyRules(lcfirst($attribute->getCleanName()));
218 92
        }
219 92
        return $this
220
            ->addStructMethodSetBodyAssignment($method, $attribute)
221
            ->addStructMethodSetBodyReturn($method);
222
    }
223
    /**
224
     * @param PhpMethod $method
225
     * @param StructAttributeModel $attribute
226 92
     * @return Struct
227
     */
228 92
    protected function addStructMethodSetBodyAssignment(PhpMethod $method, StructAttributeModel $attribute)
229 48
    {
230 36
        $parameterName = lcfirst($attribute->getCleanName());
231 64
        if ($attribute->getRemovableFromRequest()) {
232
            $method
233 69
                ->addChild(sprintf('if (is_null($%s)) {', $parameterName))
234 92
                    ->addChild($method->getIndentedString(sprintf('unset($this->%1$s%2$s);', $attribute->getCleanName(), $attribute->nameIsClean() ? '' : sprintf(', $this->{\'%s\'}', addslashes($attribute->getName()))), 1))
235 92
                ->addChild('} else {')
236
                    ->addChild($method->getIndentedString($this->getStructMethodSetBodyAssignment($attribute, $parameterName), 1))
237
                ->addChild('}');
238
        } else {
239
            $method->addChild($this->getStructMethodSetBodyAssignment($attribute, $parameterName));
240
        }
241
        return $this;
242 92
    }
243
    /**
244 92
     * @param PhpMethod $method
245 92
     * @return Struct
246
     */
247 20
    protected function addStructMethodSetBodyReturn(PhpMethod $method)
248 20
    {
249 20
        $method->addChild('return $this;');
250 20
        return $this;
251 20
    }
252 15
    /**
253 88
     * @param PhpMethod $method
254
     * @param StructAttributeModel $attribute
255 92
     * @param string $parameterName
256
     * @return Struct
257
     */
258
    protected function addStructMethodSetBodyForRestriction(PhpMethod $method, StructAttributeModel $attribute, $parameterName = null)
259
    {
260
        if (($model = $this->getRestrictionFromStructAttribute($attribute)) instanceof StructModel) {
261 92
            $parameterName = empty($parameterName) ? lcfirst($attribute->getCleanName()) : $parameterName;
262
            $method
263 92
                ->addChild(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName))
264 92
                    ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'Value "%%s" is invalid, please use one of: %%s\', $%s, implode(\', \', %s::%s())), __LINE__);', $parameterName, $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES), 1))
265
                ->addChild('}');
266
        }
267
        return $this;
268
    }
269
    /**
270
     * @param StructAttributeModel $attribute
271
     * @param string $parameterName
272 72
     * @return string
273
     */
274 72
    protected function getStructMethodSetBodyAssignment(StructAttributeModel $attribute, $parameterName)
275 48
    {
276
        if ($attribute->nameIsClean()) {
277 48
            $assignment = sprintf('$this->%s = $%s;', $attribute->getName(), $parameterName);
278 48
        } else {
279 48
            $assignment = sprintf('$this->%s = $this->{\'%s\'} = $%s;', $attribute->getCleanName(), addslashes($attribute->getName()), $parameterName);
280 36
        }
281 72
        return $assignment;
282
    }
283
    /**
284
     * @param PhpMethod $method
285
     * @param StructAttributeModel $attribute
286
     * @param string $thisAccess
287
     * @return Struct
288 48
     */
289
    protected function addStructMethodGetBody(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
290 48
    {
291 48
        return $this
292 48
            ->addStructMethodGetBodyForXml($method, $attribute, $thisAccess)
293 48
            ->addStructMethodGetBodyReturn($method, $attribute, $thisAccess);
294 48
    }
295
    /**
296 16
     * @param PhpMethod $method
297 16
     * @param StructAttributeModel $attribute
298 16
     * @param string $thisAccess
299 16
     * @return Struct
300 16
     */
301 16
    protected function addStructMethodGetBodyForXml(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
302 16
    {
303 16
        if ($attribute->isXml()) {
304 16
            $method
305 12
                ->addChild(sprintf('if (!empty($this->%1$s) && !($this->%1$s instanceof \DOMDocument)) {', $thisAccess))
306
                    ->addChild($method->getIndentedString('$dom = new \DOMDocument(\'1.0\', \'UTF-8\');', 1))
307 44
                    ->addChild($method->getIndentedString('$dom->formatOutput = true;', 1))
308 44
                    ->addChild($method->getIndentedString(sprintf('if ($dom->loadXML($this->%s)) {', $thisAccess), 1))
309 44
                    ->addChild($method->getIndentedString(sprintf('$this->%s($dom);', $attribute->getSetterName()), 2))
310 44
                    ->addChild($method->getIndentedString('}', 1))
311 44
                    ->addChild($method->getIndentedString('unset($dom);', 1))
312
                ->addChild('}');
313 36
        }
314 48
        return $this;
315
    }
316
    /**
317
     * @param PhpMethod $method
318
     * @param StructAttributeModel $attribute
319
     * @param string $thisAccess
320
     * @return Struct
321
     */
322 44
    protected function addStructMethodGetBodyReturn(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
323
    {
324 44
        $return = sprintf('return $this->%s;', $thisAccess);
325 44
        if ($attribute->isXml()) {
326 44
            if ($attribute->getRemovableFromRequest()) {
327 32
                $return = sprintf('return ($asString && isset($this->%1$s) && ($this->%1$s instanceof \DOMDocument) && $this->%1$s->hasChildNodes()) ? $this->%1$s->saveXML($this->%1$s->childNodes->item(0)) : (isset($this->%1$s) ? $this->%1$s : null);', $thisAccess);
328 24
            } else {
329 24
                $return = sprintf('return ($asString && ($this->%1$s instanceof \DOMDocument) && $this->%1$s->hasChildNodes()) ? $this->%1$s->saveXML($this->%1$s->childNodes->item(0)) : $this->%1$s;', $thisAccess);
330 18
            }
331 16
        } elseif ($attribute->getRemovableFromRequest()) {
332 16
            $return = sprintf('return isset($this->%1$s) ? $this->%1$s : null;', $thisAccess);
333 12
        }
334
        $method->addChild($return);
335
        return $this;
336 12
    }
337
    /**
338
     * @param MethodContainer $methods
339 12
     * @param StructAttributeModel $attribute
340 16
     * @return Struct
341 16
     */
342 18
    protected function addStructMethodGet(MethodContainer $methods, StructAttributeModel $attribute)
343 24
    {
344
        $method = new PhpMethod($attribute->getGetterName(), $this->getStructMethodGetParameters($attribute));
345 44
        if ($attribute->nameIsClean()) {
346
            $thisAccess = sprintf('%s', $attribute->getName());
347
        } else {
348
            $thisAccess = sprintf('{\'%s\'}', addslashes($attribute->getName()));
349
        }
350
        $this->addStructMethodGetBody($method, $attribute, $thisAccess);
351
        $methods->add($method);
352 92
        return $this;
353
    }
354 92
    /**
355 92
     * @param StructAttributeModel $attribute
356 69
     * @return PhpFunctionParameter[]
357
     */
358
    protected function getStructMethodGetParameters(StructAttributeModel $attribute)
359 92
    {
360
        $parameters = array();
361
        if ($attribute->isXml()) {
362
            $parameters[] = new PhpFunctionParameter('asString', true);
363
        }
364
        return $parameters;
365
    }
366
    /**
367 92
     * @param MethodContainer $methods
368
     * @return Struct
369 69
     */
370 92
    protected function addStructMethodSetState(MethodContainer $methods)
371 92
    {
372
        $method = new PhpMethod(self::METHOD_SET_STATE, array(
373
            new PhpFunctionParameter('array', PhpFunctionParameter::NO_VALUE, 'array'),
374
        ), PhpMethod::ACCESS_PUBLIC, false, true);
375
        $method->addChild(sprintf('return parent::__set_state($array);'));
376
        $methods->add($method);
377
        return $this;
378
    }
379 92
    /**
380
     * @return PhpAnnotationBlock|null
381 92
     */
382
    protected function getMethodAnnotationBlock(PhpMethod $method)
383 4
    {
384 4
        return $this->getStructMethodAnnotationBlock($method);
385 4
    }
386 4
    /**
387 4
     * @param PhpMethod $method
388 4
     * @return PhpAnnotationBlock|null
389 4
     */
390 4
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
391 3
    {
392 92
        $annotationBlock = null;
393
        switch ($method->getName()) {
394
            case self::METHOD_CONSTRUCT:
395
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
396
                break;
397
            case self::METHOD_SET_STATE:
398
                $annotationBlock = $this->getStructMethodSetStateAnnotationBlock();
399
                break;
400 92
            case strpos($method->getName(), 'get') === 0:
401
            case strpos($method->getName(), 'set') === 0:
402 92
                $annotationBlock = $this->getStructMethodsSetAndGetAnnotationBlock($method);
403 92
                break;
404 4
            case strpos($method->getName(), 'addTo') === 0:
405
                $annotationBlock = $this->getStructMethodsAddToAnnotationBlock($method);
406
                break;
407 4
        }
408
        return $annotationBlock;
409 92
    }
410 20
    /**
411 15
     * @return PhpAnnotationBlock
412 92
     */
413 92
    protected function getStructMethodConstructAnnotationBlock()
414
    {
415
        $annotationBlock = new PhpAnnotationBlock(array(
416
            sprintf('Constructor method for %s', $this->getModel()->getName()),
417
        ));
418
        $this->addStructPropertiesToAnnotationBlock($annotationBlock);
419
        return $annotationBlock;
420 92
    }
421
    /**
422 92
     * @return PhpAnnotationBlock
423 92
     */
424 92
    protected function getStructMethodSetStateAnnotationBlock()
425 69
    {
426
        return new PhpAnnotationBlock(array(
427
            'Method called when an object has been exported with var_export() functions',
428 92
            'It allows to return an object instantiated with the values',
429 92
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
430 92
            new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
431
            new PhpAnnotation(self::ANNOTATION_PARAM, 'array $array the exported values'),
432
            new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)),
433
        ));
434
    }
435
    /**
436 92
     * @param PhpMethod $method
437
     * @return PhpAnnotationBlock
438 92
     */
439 92
    protected function getStructMethodsSetAndGetAnnotationBlock(PhpMethod $method)
440 4
    {
441 3
        $parameters = $method->getParameters();
442 92
        $setOrGet = strtolower(substr($method->getName(), 0, 3));
443
        $parameter = array_shift($parameters);
444
        /**
445
         * Only set parameter must be based on a potential PhpFunctionParameter
446
         */
447
        if ($parameter instanceof PhpFunctionParameter && $setOrGet === 'set') {
448 92
            $parameterName = ucfirst($parameter->getName());
449
        } else {
450 92
            $parameterName = substr($method->getName(), 3);
451 92
        }
452 92
        $attribute = $this->getModel()->getAttribute($parameterName);
453 92
        if (!$attribute instanceof StructAttributeModel) {
454 92
            $parameterName = lcfirst($parameterName);
455 92
            $attribute = $this->getModel()->getAttribute($parameterName);
456
        }
457
        $setValueAnnotation = '%s %s value';
458
        $annotationBlock = new PhpAnnotationBlock();
459
        if ($attribute instanceof StructAttributeModel) {
460 92
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), $parameterName));
461
            $this->addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, $annotationBlock, $attribute);
462 92
        } elseif (empty($attribute)) {
463
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), lcfirst($parameterName)));
464
            $this->addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, $annotationBlock, $parameterName);
465
        }
466
        return $annotationBlock;
467
    }
468 92
    /**
469
     * @param string $setOrGet
470 92
     * @param PhpAnnotationBlock $annotationBlock
471 92
     * @param StructAttributeModel $attribute
472 92
     * @return Struct
473 72
     */
474 72
    protected function addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
475 92
    {
476 72
        switch ($setOrGet) {
477 72
            case 'set':
478 92
                if ($attribute->getRemovableFromRequest()) {
479 92
                    $annotationBlock->addChild('This property is removable from request (nillable=true+minOccurs=0), therefore if the value assigned to this property is null, it is removed from this object');
480 92
                }
481 92
                if (($model = $this->getRestrictionFromStructAttribute($attribute)) instanceof StructModel && !$this->getModel()->isArray()) {
482 48
                    $annotationBlock
483 48
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
484 48
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
485 69
                        ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
486 92
                } elseif ($attribute->isArray()) {
487
                    $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
488
                }
489
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()));
490
                break;
491 92
            case 'get':
492
                if ($attribute->getRemovableFromRequest()) {
493 92
                    $annotationBlock->addChild('An additional test has been added (isset) before returning the property value as this property may have been unset before, due to the fact that this property is removable from the request (nillable=true+minOccurs=0)');
494 92
                }
495 69
                $this
496 92
                    ->addStructMethodsGetAnnotationBlockFromXmlAttribute($annotationBlock, $attribute)
497 92
                    ->addStructMethodsGetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeGetAnnotation($attribute));
498
                break;
499
        }
500
        return $this;
501
    }
502 92
    /**
503
     * @param string $setOrGet
504 92
     * @param PhpAnnotationBlock $annotationBlock
505 92
     * @param string $attributeName
506 92
     * @return Struct
507 92
     */
508 92
    protected function addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, PhpAnnotationBlock $annotationBlock, $attributeName)
509 92
    {
510 92
        switch ($setOrGet) {
511 69
            case 'set':
512
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, lcfirst($attributeName), lcfirst($attributeName));
513
                break;
514
            case 'get':
515
                $this->addStructMethodsGetAnnotationBlock($annotationBlock, lcfirst($attributeName));
516
                break;
517 92
        }
518
        return $this;
519 92
    }
520 92
    /**
521 92
     * @param PhpAnnotationBlock $annotationBlock
522
     * @param string $type
523
     * @param string $name
524
     * @return Struct
525 92
     */
526 92
    protected function addStructMethodsSetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $type, $name)
527 69
    {
528 92
        $annotationBlock
529
            ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $type, $name)))
530 92
            ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
531 92
        return $this;
532 48
    }
533 48
    /**
534 36
     * @param PhpAnnotationBlock $annotationBlock
535 92
     * @param string $attributeType
536 92
     * @return Struct
537 92
     */
538 92
    protected function addStructMethodsGetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $attributeType)
539 92
    {
540 69
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $attributeType));
541 4
        return $this;
542 4
    }
543 3
    /**
544 92
     * @param PhpAnnotationBlock $annotationBlock
545
     * @param StructAttributeModel $attribute
546
     * @return Struct
547
     */
548
    protected function addStructMethodsGetAnnotationBlockFromXmlAttribute(PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
549
    {
550
        if ($attribute->isXml()) {
551
            $annotationBlock
552 92
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::loadXML()'))
553
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::hasChildNodes()'))
554
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::saveXML()'))
555 92
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMNode::item()'))
556 92
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(true), $attribute->getSetterName())))
557 20
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, 'bool $asString true: returns XML string, false: returns \DOMDocument'));
558 15
        }
559 92
        return $this;
560
    }
561 44
    /**
562 44
     * @param PhpAnnotationBlock $annotationBlock
563 44
     * @return Struct
564 92
     */
565 48
    protected function addStructPropertiesToAnnotationBlock(PhpAnnotationBlock $annotationBlock)
566 36
    {
567 92
        return $this
568 92
            ->addStructPropertiesToAnnotationBlockUses($annotationBlock)
569 69
            ->addStructPropertiesToAnnotationBlockParams($annotationBlock);
570 92
    }
571 20
    /**
572 15
     * @param PhpAnnotationBlock $annotationBlock
573 69
     * @return Struct
574 92
     */
575 92
    protected function addStructPropertiesToAnnotationBlockUses(PhpAnnotationBlock $annotationBlock)
576 92
    {
577
        foreach ($this->getModelAttributes() as $attribute) {
578 92
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(), $attribute->getSetterName())));
579
        }
580
        return $this;
581
    }
582
    /**
583
     * @param PhpAnnotationBlock $annotationBlock
584
     * @return Struct
585
     */
586 4
    protected function addStructPropertiesToAnnotationBlockParams(PhpAnnotationBlock $annotationBlock)
587
    {
588
        foreach ($this->getModelAttributes() as $attribute) {
589 4
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()))));
590
        }
591
        return $this;
592 3
    }
593 4
    /**
594 4
     * @param PhpMethod $method
595
     * @return PhpAnnotationBlock
596 4
     */
597
    protected function getStructMethodsAddToAnnotationBlock(PhpMethod $method)
598
    {
599
        $attributeName = str_replace('addTo', '', $method->getName());
600
        $attribute = $this->getModel()->getAttribute($attributeName);
601
        if (!$attribute instanceof StructAttributeModel) {
602
            $attribute = $this->getModel()->getAttribute(lcfirst($attributeName));
603
        }
604 92
        $model = $this->getRestrictionFromStructAttribute($attribute);
605
        $annotationBlock = new PhpAnnotationBlock();
606
        if ($attribute instanceof StructAttributeModel) {
607 92
            $annotationBlock->addChild(sprintf('Add item to %s value', $attribute->getCleanName()));
608 92
            if ($model instanceof StructModel) {
609 92
                $annotationBlock
610
                    ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
611
                    ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)));
612
            }
613
            $annotationBlock
614
                ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'))
615
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeTypeSetAnnotation($attribute, false))))
616 92
                ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
617
        }
618 92
        return $annotationBlock;
619 92
    }
620
    /**
621
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel()
622
     * @return StructModel
623
     */
624
    public function getModel()
625
    {
626 92
        return parent::getModel();
627
    }
628 92
    /**
629
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
630 4
     * @throws \InvalidaArgumentException
631 4
     * @param AbstractModel $model
632 4
     * @return StructArray
633 4
     */
634 4
    public function setModel(AbstractModel $model)
635 4
    {
636 3
        if (!$model instanceof StructModel) {
637 92
            throw new \InvalidArgumentException('Model must be an instance of a Struct', __LINE__);
638
        }
639
        return parent::setModel($model);
640
    }
641
}
642