Completed
Push — feature/issue-49 ( 9a3dca...dba810 )
by Mikaël
28:22
created

Struct::setModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

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