Completed
Push — 2.x ( 043669...4fa139 )
by Mikaël
65:52 queued 32:03
created

Struct::addStructMethodsSetAndGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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