Completed
Push — develop ( 06b47b...d99803 )
by Mikaël
30:22
created

Struct::getStructMethodSetStateAnnotationBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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