Completed
Push — 1.x ( 257780...f31d1d )
by Mikaël
42:33 queued 33:38
created

Struct::addStructMethodConstructBody()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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