Completed
Push — master ( ab3100...ab2db9 )
by Mikaël
57:47 queued 20:08
created

Struct::getStructMethodAnnotationBlock()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

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