Completed
Push — master ( d2fe44...54d794 )
by Mikaël
659:50 queued 657:21
created

Struct::addStructMethodConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

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