Completed
Push — feature/issue-41 ( 90ee1a...9015ef )
by Mikaël
25:01
created

getStructMethodSetBodyForArrayItemSanityCheck()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.2488

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 17
cts 21
cp 0.8095
rs 8.5906
cc 6
eloc 20
nc 6
nop 1
crap 6.2488
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
19
class Struct extends AbstractModelFile
20
{
21
    /**
22
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants()
23
     */
24 76
    protected function getClassConstants(ConstantContainer $constants)
25
    {
26 76
    }
27
    /**
28
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock()
29
     */
30
    protected function getConstantAnnotationBlock(PhpConstant $constant)
31
    {
32
    }
33
    /**
34
     * @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.
35
     * @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
36
     * @return StructAttributeContainer
37
     */
38 76
    protected function getModelAttributes($includeInheritanceAttributes = false, $requiredFirst = true)
39
    {
40 76
        return $this->getModel()->getAttributes($includeInheritanceAttributes, $requiredFirst);
41
    }
42
    /**
43
     * @param PropertyContainer
44
     */
45 104
    protected function getClassProperties(PropertyContainer $properties)
46
    {
47 104
        if ($this->getModel()->getAttributes()->count() > 0) {
48 76
            foreach ($this->getModelAttributes() as $attribute) {
49 76
                $properties->add(new PhpProperty($attribute->getCleanName(), PhpProperty::NO_VALUE));
50 57
            }
51 57
        }
52 104
    }
53
    /**
54
     * @return PhpAnnotationBlock
55
     */
56 76
    protected function getPropertyAnnotationBlock(PhpProperty $property)
57
    {
58 76
        $annotationBlock = new PhpAnnotationBlock();
59 76
        $annotationBlock->addChild(sprintf('The %s', $property->getName()));
60 76
        if (($attribute = $this->getModel()->getAttribute($property->getName())) instanceof StructAttributeModel) {
61 76
            $this->defineModelAnnotationsFromWsdl($annotationBlock, $attribute);
62 76
            if (($model = $this->getModelFromStructAttribute($attribute)) instanceof StructModel && $model->getName() !== $attribute->getName()) {
63 44
                $this->defineModelAnnotationsFromWsdl($annotationBlock, $model);
64 33
            }
65 76
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_VAR, $this->getStructAttributeTypeSetAnnotation($attribute, true)));
0 ignored issues
show
Unused Code introduced by
The call to Struct::getStructAttributeTypeSetAnnotation() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
66 57
        }
67 76
        return $annotationBlock;
68
    }
69
    /**
70
     * @param MethodContainer
71
     */
72 76
    protected function getClassMethods(MethodContainer $methods)
73
    {
74 57
        $this
75 76
            ->addStructMethodConstruct($methods)
76 76
            ->addStructMethodsSetAndGet($methods)
77 76
            ->addStructMethodSetState($methods);
78 76
    }
79
    /**
80
     * @param MethodContainer $methods
81
     * @return Struct
82
     */
83 76
    protected function addStructMethodConstruct(MethodContainer $methods)
84
    {
85 76
        $method = new PhpMethod(self::METHOD_CONSTRUCT, $this->getStructMethodParametersValues());
86 76
        $this->addStructMethodConstructBody($method);
87 76
        $methods->add($method);
88 76
        return $this;
89
    }
90
    /**
91
     * @param PhpMethod $method
92
     * @return Struct
93
     */
94 76
    protected function addStructMethodConstructBody(PhpMethod $method)
95
    {
96 76
        $count = $this->getModelAttributes()->count();
97 76
        foreach ($this->getModelAttributes() as $index=>$attribute) {
98 76
            if ($index === 0) {
99 76
                $method->addChild('$this');
100 57
            }
101 76
            $this->addStructMethodConstructBodyForAttribute($method, $attribute, $count - 1 === $index);
102 57
        }
103 76
        return $this;
104
    }
105
    /**
106
     * @param PhpMethod $method
107
     * @param StructAttributeModel $attribute
108
     * @param bool $isLast
109
     * @return Struct
110
     */
111 76
    protected function addStructMethodConstructBodyForAttribute(PhpMethod $method, StructAttributeModel $attribute, $isLast)
112
    {
113 76
        $method->addChild($method->getIndentedString(sprintf('->%s($%s)%s', $attribute->getSetterName(), lcfirst($attribute->getCleanName()), $isLast ? ';' : ''), 1));
114 76
        return $this;
115
    }
116
    /**
117
     * @return PhpFunctionParameter[]
118
     */
119 76
    protected function getStructMethodParametersValues()
120
    {
121 76
        $parametersValues = array();
122 76
        foreach ($this->getModelAttributes() as $attribute) {
123 76
            $parametersValues[] = $this->getStructMethodParameter($attribute, true);
124 57
        }
125 76
        return $parametersValues;
126
    }
127
    /**
128
     * @param StructAttributeModel $attribute
129
     * @param bool $lowCaseFirstLetter
130
     * @param mixed $defaultValue
131
     * @return PhpFunctionParameter
132
     */
133 76
    protected function getStructMethodParameter(StructAttributeModel $attribute, $lowCaseFirstLetter = false, $defaultValue = null)
134
    {
135
        try {
136 76
            return new PhpFunctionParameter($lowCaseFirstLetter ? lcfirst($attribute->getCleanName()) : $attribute->getCleanName(), isset($defaultValue) ? $defaultValue : $attribute->getDefaultValue(), $this->getStructMethodParameterType($attribute));
137
        } catch (\InvalidArgumentException $exception) {
138
            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);
139
        }
140
    }
141
    /**
142
     * @param StructAttributeModel $attribute
143
     * @return string|null
144
     */
145 56
    protected function getStructMethodParameterType(StructAttributeModel $attribute)
146
    {
147 56
        return self::getValidType($this->getStructAttributeTypeHint($attribute), null);
148
    }
149
    /**
150
     * @param MethodContainer $methods
151
     * @return Struct
152
     */
153 76
    protected function addStructMethodsSetAndGet(MethodContainer $methods)
154
    {
155 76
        foreach ($this->getModelAttributes() as $attribute) {
156 57
            $this
157 76
                ->addStructMethodGet($methods, $attribute)
158 76
                ->addStructMethodSet($methods, $attribute);
159 57
        }
160 76
        return $this;
161
    }
162
    /**
163
     * @param MethodContainer $methods
164
     * @param StructAttributeModel $attribute
165
     * @return Struct
166
     */
167 56
    protected function addStructMethodSet(MethodContainer $methods, StructAttributeModel $attribute)
168
    {
169 56
        $method = new PhpMethod($attribute->getSetterName(), array(
170 56
            $this->getStructMethodParameter($attribute, true, null),
171 42
        ));
172 56
        $this->addStructMethodSetBody($method, $attribute);
173 56
        $methods->add($method);
174 56
        return $this;
175
    }
176
    /**
177
     * @param PhpMethod $method
178
     * @param StructAttributeModel $attribute
179
     * @return Struct
180
     */
181 76
    protected function addStructMethodSetBody(PhpMethod $method, StructAttributeModel $attribute)
182
    {
183 57
        return $this
184 76
            ->addStructMethodSetBodyForRestriction($method, $attribute)
185 76
            ->addStructMethodSetBodyForArray($method, $attribute)
186 76
            ->addStructMethodSetBodyAssignment($method, $attribute)
187 76
            ->addStructMethodSetBodyReturn($method);
188
    }
189
    /**
190
     * @param PhpMethod $method
191
     * @param StructAttributeModel $attribute
192
     * @return Struct
193
     */
194 76
    protected function addStructMethodSetBodyAssignment(PhpMethod $method, StructAttributeModel $attribute)
195
    {
196 76
        $method->addChild($this->getStructMethodSetBodyAssignment($attribute));
197 76
        return $this;
198
    }
199
    /**
200
     * @param PhpMethod $method
201
     * @return Struct
202
     */
203 76
    protected function addStructMethodSetBodyReturn(PhpMethod $method)
204
    {
205 76
        $method->addChild('return $this;');
206 76
        return $this;
207
    }
208
    /**
209
     * @param PhpMethod $method
210
     * @param StructAttributeModel $attribute
211
     * @return Struct
212
     */
213 56
    protected function addStructMethodSetBodyForRestriction(PhpMethod $method, StructAttributeModel $attribute)
214
    {
215 56
        if (($model = $this->getModelFromStructAttribute($attribute)) instanceof StructModel && $model->getIsRestriction()) {
216 36
            $parameterName = lcfirst($attribute->getCleanName());
217
            $method
218 36
                ->addChild(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName))
219 36
                    ->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))
220 36
                ->addChild('}');
221 27
        }
222 56
        return $this;
223
    }
224
    /**
225
     * @param PhpMethod $method
226
     * @param StructAttributeModel $attribute
227
     * @return Struct
228
     */
229 76
    protected function addStructMethodSetBodyForArray(PhpMethod $method, StructAttributeModel $attribute)
230
    {
231 76
        if ((!($model = $this->getModelFromStructAttribute($attribute)) instanceof StructModel || !$model->getIsRestriction()) && $attribute->isArray()) {
232 12
            $parameterName = lcfirst($attribute->getCleanName());
233
            $method
234 12
                ->addChild(sprintf('array_walk($%s, function($item) {', $parameterName))
235 12
                    ->addChild($method->getIndentedString(sprintf('if (%s) {', $this->getStructMethodSetBodyForArrayItemSanityCheck($attribute)), 1))
236 12
                        ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'The %s property can only contain items of %s, "%%s" given\', is_object($item) ? get_class($item) : gettype($item)), __LINE__);', $attribute->getCleanName(), $this->getStructAttributeType($attribute, true)), 2))
237 12
                    ->addChild($method->getIndentedString('}', 1))
238 12
                ->addChild('});');
239 9
        }
240 76
        return $this;
241
    }
242
    /**
243
     * The second case which used PHP native functions is volontary limited by the native functions provided by PHP,
244
     * and the possible types defined in xsd_types.yml
245
     * @param StructAttributeModel $attribute
246
     */
247 12
    protected function getStructMethodSetBodyForArrayItemSanityCheck(StructAttributeModel $attribute) {
248 12
        $model = $this->getModelFromStructAttribute($attribute);
249 12
        $sanityCheck = 'false';
250 12
        if ($model instanceof StructModel) {
251 8
            $sanityCheck = sprintf('!$item instanceof %s', $this->getStructAttributeType($attribute, true));
252 6
        } else {
253 4
            switch (self::getPhpType($attribute->getType())) {
254 3
                case 'int';
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
255 4
                    $sanityCheck = 'is_int($item)';
256 4
                    break;
257 3
                case 'bool';
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
258
                    $sanityCheck = 'is_bool($item)';
259
                    break;
260 3
                case 'float';
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
261
                    $sanityCheck = 'is_float($item)';
262
                    break;
263 3
                case 'string';
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
264 4
                    $sanityCheck = 'is_string($item)';
265 4
                    break;
266 3
            }
267
        }
268 12
        return $sanityCheck;
269
    }
270
    /**
271
     * @param StructAttributeModel $attribute
272
     * @return string
273
     */
274 76
    protected function getStructMethodSetBodyAssignment(StructAttributeModel $attribute)
275
    {
276 76
        if ($attribute->nameIsClean()) {
277 76
            $assignment = sprintf('$this->%s = $%s;', $attribute->getName(), lcfirst($attribute->getCleanName()));
278 57
        } else {
279
            $assignment = sprintf('$this->%s = $this->{\'%s\'} = $%s;', $attribute->getCleanName(), addslashes($attribute->getName()), lcfirst($attribute->getCleanName()));
280
        }
281 76
        return $assignment;
282
    }
283
    /**
284
     * @param PhpMethod $method
285
     * @param StructAttributeModel $attribute
286
     * @param string $thisAccess
287
     * @return Struct
288
     */
289 76
    protected function addStructMethodGetBody(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
290
    {
291 57
        return $this
292 76
            ->addStructMethodGetBodyForXml($method, $attribute, $thisAccess)
293 76
            ->addStructMethodGetBodyReturn($method, $attribute, $thisAccess);
294
    }
295
    /**
296
     * @param PhpMethod $method
297
     * @param StructAttributeModel $attribute
298
     * @param string $thisAccess
299
     * @return Struct
300
     */
301 76
    protected function addStructMethodGetBodyForXml(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
302
    {
303 76
        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 76
        return $this;
315
    }
316
    /**
317
     * @param PhpMethod $method
318
     * @param StructAttributeModel $attribute
319
     * @param string $thisAccess
320
     * @return Struct
321
     */
322 76
    protected function addStructMethodGetBodyReturn(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
323
    {
324 76
        $return = sprintf('return $this->%s;', $thisAccess);
325 76
        if ($attribute->isXml()) {
326 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);
327 3
        }
328 76
        $method->addChild($return);
329 76
        return $this;
330
    }
331
    /**
332
     * @param MethodContainer $methods
333
     * @param StructAttributeModel $attribute
334
     * @return Struct
335
     */
336 76
    protected function addStructMethodGet(MethodContainer $methods, StructAttributeModel $attribute)
337
    {
338 76
        $method = new PhpMethod($attribute->getGetterName(), $this->getStructMethodGetParameters($attribute));
339 76
        if ($attribute->nameIsClean()) {
340 76
            $thisAccess = sprintf('%s', $attribute->getName());
341 57
        } else {
342
            $thisAccess = sprintf('{\'%s\'}', addslashes($attribute->getName()));
343
        }
344 76
        $this->addStructMethodGetBody($method, $attribute, $thisAccess);
345 76
        $methods->add($method);
346 76
        return $this;
347
    }
348
    /**
349
     * @param StructAttributeModel $attribute
350
     * @return PhpFunctionParameter[]
351
     */
352 76
    protected function getStructMethodGetParameters(StructAttributeModel $attribute)
353
    {
354 76
        $parameters = array();
355 76
        if ($attribute->isXml()) {
356 4
            $parameters[] = new PhpFunctionParameter('asString', true);
357 3
        }
358 76
        return $parameters;
359
    }
360
    /**
361
     * @param MethodContainer $methods
362
     * @return Struct
363
     */
364 76
    protected function addStructMethodSetState(MethodContainer $methods)
365
    {
366 76
        $method = new PhpMethod(self::METHOD_SET_STATE, array(
367 76
            new PhpFunctionParameter('array', PhpFunctionParameter::NO_VALUE, 'array'),
368 76
        ), PhpMethod::ACCESS_PUBLIC, false, true);
369 76
        $method->addChild(sprintf('return parent::__set_state($array);'));
370 76
        $methods->add($method);
371 76
        return $this;
372
    }
373
    /**
374
     * @return PhpAnnotationBlock|null
375
     */
376 76
    protected function getMethodAnnotationBlock(PhpMethod $method)
377
    {
378 76
        return $this->getStructMethodAnnotationBlock($method);
379
    }
380
    /**
381
     * @param PhpMethod $method
382
     * @return PhpAnnotationBlock|null
383
     */
384 76
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
385
    {
386 76
        $annotationBlock = null;
387 76
        switch ($method->getName()) {
388 76
            case self::METHOD_CONSTRUCT:
389 56
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
390 56
                break;
391 76
            case self::METHOD_SET_STATE:
392 56
                $annotationBlock = $this->getStructMethodSetStateAnnotationBlock();
393 56
                break;
394 76
            case strpos($method->getName(), 'get') === 0:
395 76
            case strpos($method->getName(), 'set') === 0:
396 76
                $annotationBlock = $this->getStructMethodsSetAndGetAnnotationBlock($method);
397 76
                break;
398 57
        }
399 76
        return $annotationBlock;
400
    }
401
    /**
402
     * @return PhpAnnotationBlock
403
     */
404 76
    protected function getStructMethodConstructAnnotationBlock()
405
    {
406 76
        $annotationBlock = new PhpAnnotationBlock(array(
407 76
            sprintf('Constructor method for %s', $this->getModel()->getName()),
408 57
        ));
409 76
        $this->addStructPropertiesToAnnotationBlock($annotationBlock);
410 76
        return $annotationBlock;
411
    }
412
    /**
413
     * @return PhpAnnotationBlock
414
     */
415 76
    protected function getStructMethodSetStateAnnotationBlock()
416
    {
417 76
        return new PhpAnnotationBlock(array(
418 76
            'Method called when an object has been exported with var_export() functions',
419 76
            'It allows to return an object instantiated with the values',
420 76
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
421 76
            new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
422 76
            new PhpAnnotation(self::ANNOTATION_PARAM, 'array $array the exported values'),
423 76
            new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)),
424 57
        ));
425
    }
426
    /**
427
     * @param PhpMethod $method
428
     * @return PhpAnnotationBlock
429
     */
430 76
    protected function getStructMethodsSetAndGetAnnotationBlock(PhpMethod $method)
431
    {
432 76
        $parameters = $method->getParameters();
433 76
        $setOrGet = strtolower(substr($method->getName(), 0, 3));
434 76
        $parameter = array_shift($parameters);
435
        /**
436
         * Only set parameter must be based on a potential PhpFunctionParameter
437
         */
438 76
        if ($parameter instanceof PhpFunctionParameter && $setOrGet === 'set') {
439 76
            $parameterName = ucfirst($parameter->getName());
440 57
        } else {
441 76
            $parameterName = substr($method->getName(), 3);
442
        }
443 76
        $attribute = $this->getModel()->getAttribute($parameterName);
444 76
        if (!$attribute instanceof StructAttributeModel) {
445 40
            $parameterName = lcfirst($parameterName);
446 40
            $attribute = $this->getModel()->getAttribute($parameterName);
447 30
        }
448 76
        $setValueAnnotation = '%s %s value';
449 76
        $annotationBlock = new PhpAnnotationBlock();
450 76
        if ($attribute instanceof StructAttributeModel) {
451 76
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), $parameterName));
452 76
            $this->addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, $annotationBlock, $attribute);
453 57
        } elseif (empty($attribute)) {
454 4
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), lcfirst($parameterName)));
455 4
            $this->addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, $annotationBlock, $parameterName);
456 3
        }
457 76
        return $annotationBlock;
458
    }
459
    /**
460
     * @param string $setOrGet
461
     * @param PhpAnnotationBlock $annotationBlock
462
     * @param StructAttributeModel $attribute
463
     * @return Struct
464
     */
465 76
    protected function addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
466
    {
467
        switch ($setOrGet) {
468 76
            case 'set':
469 76
                if (($model = $this->getModelFromStructAttribute($attribute)) instanceof StructModel && $model->getIsRestriction() && !$this->getModel()->isArray()) {
470
                    $annotationBlock
471 36
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
472 36
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
473 36
                        ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
474 76
                } elseif ($attribute->isArray()) {
475 12
                    $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
476 9
                }
477 76
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()));
478 76
                break;
479 57
            case 'get':
480 76
                $this->addStructMethodsGetAnnotationBlockFromXmlAttribute($annotationBlock, $attribute);
481 76
                $this->addStructMethodsGetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeGetAnnotation($attribute));
482 76
                break;
483
        }
484 76
        return $this;
485
    }
486
    /**
487
     * @param string $setOrGet
488
     * @param PhpAnnotationBlock $annotationBlock
489
     * @param string $attributeName
490
     * @return Struct
491
     */
492 4
    protected function addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, PhpAnnotationBlock $annotationBlock, $attributeName)
493
    {
494
        switch ($setOrGet) {
495 4
            case 'set':
496
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, lcfirst($attributeName), lcfirst($attributeName));
497
                break;
498 3
            case 'get':
499 4
                $this->addStructMethodsGetAnnotationBlock($annotationBlock, lcfirst($attributeName));
500 4
                break;
501
        }
502 4
        return $this;
503
    }
504
    /**
505
     * @param PhpAnnotationBlock $annotationBlock
506
     * @param string $type
507
     * @param string $name
508
     * @return Struct
509
     */
510 76
    protected function addStructMethodsSetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $type, $name)
511
    {
512
        $annotationBlock
513 76
            ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $type, $name)))
514 76
            ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
515 76
        return $this;
516
    }
517
    /**
518
     * @param PhpAnnotationBlock $annotationBlock
519
     * @param string $attribute
520
     * @return Struct
521
     */
522 76
    protected function addStructMethodsGetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $attribute)
523
    {
524 76
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $attribute));
525 76
        return $this;
526
    }
527
    /**
528
     * @param PhpAnnotationBlock $annotationBlock
529
     * @param StructAttributeModel $attribute
530
     */
531 76
    protected function addStructMethodsGetAnnotationBlockFromXmlAttribute(PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
532
    {
533 76
        if ($attribute->isXml()) {
534
            $annotationBlock
535 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::loadXML()'))
536 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::hasChildNodes()'))
537 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::saveXML()'))
538 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMNode::item()'))
539 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(true), $attribute->getSetterName())))
540 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, 'bool $asString true: returns XML string, false: returns \DOMDocument'));
541 3
        }
542 76
    }
543
    /**
544
     * @param PhpAnnotationBlock $annotationBlock
545
     * @return Struct
546
     */
547 76
    protected function addStructPropertiesToAnnotationBlock(PhpAnnotationBlock $annotationBlock)
548
    {
549 57
        return $this
550 76
            ->addStructPropertiesToAnnotationBlockUses($annotationBlock)
551 76
            ->addStructPropertiesToAnnotationBlockParams($annotationBlock);
552
    }
553
    /**
554
     * @param PhpAnnotationBlock $annotationBlock
555
     * @return Struct
556
     */
557 76
    protected function addStructPropertiesToAnnotationBlockUses(PhpAnnotationBlock $annotationBlock)
558
    {
559 76
        foreach ($this->getModelAttributes() as $attribute) {
560 76
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(), $attribute->getSetterName())));
561 57
        }
562 76
        return $this;
563
    }
564
    /**
565
     * @param PhpAnnotationBlock $annotationBlock
566
     * @return Struct
567
     */
568 76
    protected function addStructPropertiesToAnnotationBlockParams(PhpAnnotationBlock $annotationBlock)
569
    {
570 76
        foreach ($this->getModelAttributes() as $attribute) {
571 76
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()))));
572 57
        }
573 76
        return $this;
574
    }
575
    /**
576
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel()
577
     * @return StructModel
578
     */
579 124
    public function getModel()
580
    {
581 124
        return parent::getModel();
582
    }
583
    /**
584
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
585
     * @throws \InvalidaArgumentException
586
     * @param AbstractModel $model
587
     * @return StructArray
588
     */
589 124
    public function setModel(AbstractModel $model)
590
    {
591 124
        if (!$model instanceof StructModel) {
592 4
            throw new \InvalidArgumentException('Model must be an instance of a Struct', __LINE__);
593
        }
594 120
        return parent::setModel($model);
595
    }
596
}
597