Passed
Push — master ( 5d2c29...59c75b )
by Mikaël
76:36 queued 50:13
created

Struct::addStructMethodsSetAnnotationBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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\Property as PropertyContainer;
9
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
10
use WsdlToPhp\PackageGenerator\Container\Model\StructAttribute as StructAttributeContainer;
11
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
12
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
13
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
14
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
15
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
16
use WsdlToPhp\PackageGenerator\File\Validation\Rules;
17
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter;
18
19
class Struct extends AbstractModelFile
20
{
21
    /**
22
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants()
23
     */
24 312
    protected function getClassConstants(ConstantContainer $constants)
25
    {
26 312
    }
27
    /**
28
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock()
29
     */
30
    protected function getConstantAnnotationBlock(PhpConstant $constant)
31
    {
32
    }
33
    /**
34
     * @return StructAttributeContainer
35
     */
36 360
    protected function getModelAttributes()
37
    {
38 360
        return $this->getModel()->getProperAttributes(true);
39
    }
40
    /**
41
     * @param PropertyContainer $properties
42
     */
43 360
    protected function getClassProperties(PropertyContainer $properties)
44
    {
45 360
        foreach ($this->getModelAttributes() as $attribute) {
46 300
            $properties->add(new PhpProperty($attribute->getCleanName(), PhpProperty::NO_VALUE));
47 120
        }
48 360
    }
49
    /**
50
     * @return PhpAnnotationBlock $property
51
     */
52 300
    protected function getPropertyAnnotationBlock(PhpProperty $property)
53
    {
54 300
        $annotationBlock = new PhpAnnotationBlock();
55 300
        $annotationBlock->addChild(sprintf('The %s', $property->getName()));
56 300
        $attribute = $this->getModel()->getAttribute($property->getName());
57 300
        if (!$attribute instanceof StructAttributeModel) {
58 6
            $attribute = $this->getModel()->getAttributeByCleanName($property->getName());
59 2
        }
60 300
        if ($attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
61 300
            $this->defineModelAnnotationsFromWsdl($annotationBlock, $attribute);
62 300
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_VAR, $this->getStructAttributeTypeSetAnnotation($attribute, true)));
63 100
        }
64 300
        return $annotationBlock;
65
    }
66 312
    protected function fillClassMethods()
67
    {
68 104
        $this
69 312
            ->addStructMethodConstruct()
70 312
            ->addStructMethodsSetAndGet();
71 312
    }
72
    /**
73
     * @return Struct
74
     */
75 312
    protected function addStructMethodConstruct()
76
    {
77 312
        if (0 < count($parameters = $this->getStructMethodParametersValues())) {
78 300
            $method = new PhpMethod(self::METHOD_CONSTRUCT, $parameters);
79 300
            $this->addStructMethodConstructBody($method);
80 300
            $this->methods->add($method);
81 100
        }
82 312
        return $this;
83
    }
84
    /**
85
     * @param PhpMethod $method
86
     * @return Struct
87
     */
88 300
    protected function addStructMethodConstructBody(PhpMethod $method)
89
    {
90 300
        $count = $this->getModelAttributes()->count();
91 300
        foreach ($this->getModelAttributes() as $index => $attribute) {
92 300
            if ($index === 0) {
93 300
                $method->addChild('$this');
94 100
            }
95 300
            $this->addStructMethodConstructBodyForAttribute($method, $attribute, $count - 1 === $index);
96 100
        }
97 300
        return $this;
98
    }
99
    /**
100
     * @param PhpMethod $method
101
     * @param StructAttributeModel $attribute
102
     * @param bool $isLast
103
     * @return Struct
104
     */
105 300
    protected function addStructMethodConstructBodyForAttribute(PhpMethod $method, StructAttributeModel $attribute, $isLast)
106
    {
107 300
        $uniqueString = $attribute->getUniqueString($attribute->getCleanName(), 'method');
108 300
        $method->addChild($method->getIndentedString(sprintf('->%s($%s)%s', $attribute->getSetterName(), lcfirst($uniqueString), $isLast ? ';' : ''), 1));
109 300
        return $this;
110
    }
111
    /**
112
     * @return PhpFunctionParameter[]
113
     */
114 312
    protected function getStructMethodParametersValues()
115
    {
116 312
        $parametersValues = [];
117 312
        foreach ($this->getModelAttributes() as $attribute) {
118 300
            $parametersValues[] = $this->getStructMethodParameter($attribute);
119 104
        }
120 312
        return $parametersValues;
121
    }
122
    /**
123
     * @param StructAttributeModel $attribute
124
     * @return PhpFunctionParameter
125
     */
126 300
    protected function getStructMethodParameter(StructAttributeModel $attribute)
127
    {
128
        try {
129 300
            return new PhpFunctionParameter(
130 300
                lcfirst($attribute->getUniqueString($attribute->getCleanName(), 'method')),
131 300
                $attribute->getDefaultValue(),
132 300
                $this->getStructMethodParameterType($attribute),
133 150
                $attribute
134 100
            );
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 300
    protected function getStructMethodParameterType(StructAttributeModel $attribute, $returnArrayType = true)
145
    {
146 300
        return self::getValidType($this->getStructAttributeTypeHint($attribute, $returnArrayType), $this->getGenerator()->getOptionXsdTypesPath(), null);
147
    }
148
    /**
149
     * @return Struct
150
     */
151 312
    protected function addStructMethodsSetAndGet()
152
    {
153 312
        foreach ($this->getModelAttributes() as $attribute) {
154 100
            $this
155 300
                ->addStructMethodGet($attribute)
156 300
                ->addStructMethodSet($attribute)
157 300
                ->addStructMethodAddTo($attribute);
158 104
        }
159 312
        return $this;
160
    }
161
    /**
162
     * @param StructAttributeModel $attribute
163
     * @return Struct
164
     */
165 300
    protected function addStructMethodAddTo(StructAttributeModel $attribute)
166
    {
167 300
        if ($attribute->isArray()) {
168 156
            $method = new PhpMethod(sprintf('addTo%s', ucfirst($attribute->getCleanName())), [
169 156
                new PhpFunctionParameter('item', PhpFunctionParameter::NO_VALUE, $this->getStructMethodParameterType($attribute, false), $attribute),
170 60
            ]);
171 156
            $this->addStructMethodAddToBody($method, $attribute);
172 156
            $this->methods->add($method);
173 52
        }
174 300
        return $this;
175
    }
176
    /**
177
     * @param PhpMethod $method
178
     * @param StructAttributeModel $attribute
179
     * @return Struct
180
     */
181 156
    protected function addStructMethodAddToBody(PhpMethod $method, StructAttributeModel $attribute)
182
    {
183 156
        if ($this->getGenerator()->getOptionValidation()) {
184 156
            $this->applyRules($method, $attribute, 'item', true);
185 52
        }
186
187 156
        if ($attribute->nameIsClean()) {
188 156
            $assignment = sprintf('$this->%s[] = $item;', $attribute->getCleanName());
189 52
        } else {
190
            $assignment = sprintf('$this->%s[] = $this->{\'%s\'}[] = $item;', $attribute->getCleanName(), addslashes($attribute->getName()), $attribute->getCleanName());
191
        }
192
        $method
193 156
            ->addChild($assignment)
194 156
            ->addChild('return $this;');
195 156
        return $this;
196
    }
197
    /**
198
     * @param StructAttributeModel $attribute
199
     * @return Struct
200
     */
201 300
    protected function addStructMethodSet(StructAttributeModel $attribute)
202
    {
203 300
        $method = new PhpMethod($attribute->getSetterName(), [
204 300
            $this->getStructMethodParameter($attribute),
205 100
        ]);
206 300
        $this->addStructMethodSetBody($method, $attribute);
207 300
        $this->methods->add($method);
208 300
        return $this;
209
    }
210
    /**
211
     * @param PhpMethod $method
212
     * @param StructAttributeModel $attribute
213
     * @return Struct
214
     */
215 300
    protected function addStructMethodSetBody(PhpMethod $method, StructAttributeModel $attribute)
216
    {
217 300
        $parameters = $method->getParameters();
218 300
        $parameter = array_shift($parameters);
219 300
        $parameterName = is_string($parameter) ? $parameter : $parameter->getName();
220 300
        if ($this->getGenerator()->getOptionValidation()) {
221 294
            $this->applyRules($method, $attribute, $parameterName);
222 98
        }
223 100
        return $this
224 300
            ->addStructMethodSetBodyAssignment($method, $attribute, $parameterName)
225 300
            ->addStructMethodSetBodyReturn($method);
226
    }
227
    /**
228
     * @param PhpMethod $method
229
     * @param StructAttributeModel $attribute
230
     * @param string $parameterName
231
     * @return Struct
232
     */
233 300
    protected function addStructMethodSetBodyAssignment(PhpMethod $method, StructAttributeModel $attribute, $parameterName)
234
    {
235 300
        if ($attribute->getRemovableFromRequest() || $attribute->isAChoice()) {
236
            $method
237 54
                ->addChild(sprintf('if (is_null($%1$s) || (is_array($%1$s) && empty($%1$s))) {', $parameterName))
238 54
                ->addChild($method->getIndentedString(sprintf('unset($this->%1$s%2$s);', $attribute->getCleanName(), $attribute->nameIsClean() ? '' : sprintf(', $this->{\'%s\'}', addslashes($attribute->getName()))), 1))
239 54
                ->addChild('} else {')
240 54
                ->addChild($method->getIndentedString($this->getStructMethodSetBodyAssignment($attribute, $parameterName), 1))
241 54
                ->addChild('}');
242 18
        } else {
243 288
            $method->addChild($this->getStructMethodSetBodyAssignment($attribute, $parameterName));
244
        }
245 300
        return $this;
246
    }
247
    /**
248
     * @param PhpMethod $method
249
     * @return Struct
250
     */
251 300
    protected function addStructMethodSetBodyReturn(PhpMethod $method)
252
    {
253 300
        $method->addChild('return $this;');
254 300
        return $this;
255
    }
256
    /**
257
     * @param StructAttributeModel $attribute
258
     * @param string $parameterName
259
     * @return string
260
     */
261 300
    protected function getStructMethodSetBodyAssignment(StructAttributeModel $attribute, $parameterName)
262
    {
263 300
        $prefix = '$';
264 300
        if ($this->isAttributeAList($attribute)) {
265 18
            $prefix = '';
266 18
            $parameterName = sprintf('is_array($%1$s) ? implode(\' \', $%1$s) : null', $parameterName);
267 300
        } elseif ($attribute->isXml()) {
268 6
            $prefix = '';
269 6
            $parameterName = sprintf('($%1$s instanceof \DOMDocument) && $%1$s->hasChildNodes() ? $%1$s->saveXML($%1$s->childNodes->item(0)) : $%1$s', $parameterName);
270 2
        }
271 300
        if ($attribute->nameIsClean()) {
272 300
            $assignment = sprintf('$this->%s = %s%s;', $attribute->getName(), $prefix, $parameterName);
273 100
        } else {
274 6
            $assignment = sprintf('$this->%s = $this->{\'%s\'} = %s%s;', $attribute->getCleanName(), addslashes($attribute->getName()), $prefix, $parameterName);
275
        }
276 300
        return $assignment;
277
    }
278
    /**
279
     * @param PhpMethod $method
280
     * @param StructAttributeModel $attribute
281
     * @param string $thisAccess
282
     * @return Struct
283
     */
284 300
    protected function addStructMethodGetBody(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
285
    {
286 300
        return $this->addStructMethodGetBodyReturn($method, $attribute, $thisAccess);
287
    }
288
    /**
289
     * @param PhpMethod $method
290
     * @param StructAttributeModel $attribute
291
     * @param string $thisAccess
292
     * @return Struct
293
     */
294 300
    protected function addStructMethodGetBodyReturn(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
295
    {
296 300
        $return = sprintf('return $this->%s;', $thisAccess);
297 300
        if ($attribute->isXml()) {
298
            $method
299 6
                ->addChild('$domDocument = null;')
300 6
                ->addChild(sprintf('if (!empty($this->%1$s) && !$asString) {', $thisAccess))
301 6
                ->addChild($method->getIndentedString('$domDocument = new \DOMDocument(\'1.0\', \'UTF-8\');', 1))
302 6
                ->addChild($method->getIndentedString(sprintf('$domDocument->loadXML($this->%s);', $thisAccess), 1))
303 6
                ->addChild('}');
304 6
            if ($attribute->getRemovableFromRequest() || $attribute->isAChoice()) {
305
                $return = sprintf('return $asString ? (isset($this->%1$s) ? $this->%1$s : null) : $domDocument;', $thisAccess);
306
            } else {
307 6
                $return = sprintf('return $asString ? $this->%1$s : $domDocument;', $thisAccess);
308
            }
309 300
        } elseif ($attribute->getRemovableFromRequest() || $attribute->isAChoice()) {
310 54
            $return = sprintf('return isset($this->%1$s) ? $this->%1$s : null;', $thisAccess);
311 18
        }
312 300
        $method->addChild($return);
313 300
        return $this;
314
    }
315
    /**
316
     * @param StructAttributeModel $attribute
317
     * @return Struct
318
     */
319 300
    protected function addStructMethodGet(StructAttributeModel $attribute)
320
    {
321 300
        $method = new PhpMethod($attribute->getGetterName(), $this->getStructMethodGetParameters($attribute));
322 300
        if ($attribute->nameIsClean()) {
323 300
            $thisAccess = sprintf('%s', $attribute->getName());
324 100
        } else {
325 6
            $thisAccess = sprintf('{\'%s\'}', addslashes($attribute->getName()));
326
        }
327 300
        $this->addStructMethodGetBody($method, $attribute, $thisAccess);
328 300
        $this->methods->add($method);
329 300
        return $this;
330
    }
331
    /**
332
     * @param StructAttributeModel $attribute
333
     * @return PhpFunctionParameter[]
334
     */
335 300
    protected function getStructMethodGetParameters(StructAttributeModel $attribute)
336
    {
337 300
        $parameters = [];
338 300
        if ($attribute->isXml()) {
339 6
            $parameters[] = new PhpFunctionParameter('asString', true, null, $attribute);
340 2
        }
341 300
        return $parameters;
342
    }
343
    /**
344
     * @return PhpAnnotationBlock|null
345
     */
346 300
    protected function getMethodAnnotationBlock(PhpMethod $method)
347
    {
348 300
        return $this->getStructMethodAnnotationBlock($method);
349
    }
350
    /**
351
     * @param PhpMethod $method
352
     * @return PhpAnnotationBlock|null
353
     */
354 300
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
355
    {
356 300
        $annotationBlock = null;
357 300
        switch ($method->getName()) {
358 300
            case self::METHOD_CONSTRUCT:
359 300
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
360 300
                break;
361 300
            case 0 === mb_strpos($method->getName(), 'get'):
362 300
            case 0 === mb_strpos($method->getName(), 'set'):
363 300
                $annotationBlock = $this->getStructMethodsSetAndGetAnnotationBlock($method);
364 300
                break;
365 198
            case 0 === mb_strpos($method->getName(), 'addTo'):
366 156
                $annotationBlock = $this->getStructMethodsAddToAnnotationBlock($method);
367 156
                break;
368 198
            case false !== mb_strpos($method->getName(), 'ForUnionConstraintsFrom'):
369 12
                $annotationBlock = $this->getStructMethodsValidateUnionAnnotationBlock($method);
370 12
                break;
371 186
            case false !== mb_strpos($method->getName(), 'ForArrayConstraintsFrom'):
372 168
                $annotationBlock = $this->getStructMethodsValidateArrayAnnotationBlock($method);
373 168
                break;
374 36
            case false !== mb_strpos($method->getName(), 'ForChoiceConstraintsFrom'):
375 24
                $annotationBlock = $this->getStructMethodsValidateChoiceAnnotationBlock($method);
376 24
                break;
377 12
            case false !== mb_strpos($method->getName(), 'MaxLengthConstraintFrom'):
378 12
                $annotationBlock = $this->getStructMethodsValidateLengthAnnotationBlock($method, 'max');
379 12
                break;
380 12
            case false !== mb_strpos($method->getName(), 'MinLengthConstraintFrom'):
381 12
                $annotationBlock = $this->getStructMethodsValidateLengthAnnotationBlock($method, 'min');
382 12
                break;
383
            case false !== mb_strpos($method->getName(), 'LengthConstraintFrom'):
384
                $annotationBlock = $this->getStructMethodsValidateLengthAnnotationBlock($method);
385
                break;
386 100
        }
387 300
        return $annotationBlock;
388
    }
389
    /**
390
     * @return PhpAnnotationBlock
391
     */
392 300
    protected function getStructMethodConstructAnnotationBlock()
393
    {
394 300
        $annotationBlock = new PhpAnnotationBlock([
395 300
            sprintf('Constructor method for %s', $this->getModel()->getName()),
396 100
        ]);
397 300
        $this->addStructPropertiesToAnnotationBlock($annotationBlock);
398 300
        return $annotationBlock;
399
    }
400
    /**
401
     * @param PhpMethod $method
402
     * @return PhpAnnotationBlock
403
     */
404 300
    protected function getStructMethodsSetAndGetAnnotationBlock(PhpMethod $method)
405
    {
406 300
        $parameters = $method->getParameters();
407 300
        $setOrGet = mb_strtolower(mb_substr($method->getName(), 0, 3));
408 300
        $parameter = array_shift($parameters);
409
        /**
410
         * Only set parameter must be based on a potential PhpFunctionParameter
411
         */
412 300
        if ($parameter instanceof PhpFunctionParameter && $setOrGet === 'set') {
413 300
            $parameterName = ucfirst($parameter->getName());
414 100
        } else {
415 300
            $parameterName = mb_substr($method->getName(), 3);
416
        }
417
        /**
418
         * 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
419
         */
420 300
        $parameterName = preg_replace('/(_\d+)/', '', $parameterName);
421 300
        $attribute = $this->getModel()->getAttribute($parameterName);
422 300
        if (!$attribute instanceof StructAttributeModel) {
423 120
            $attribute = $this->getModel()->getAttributeByCleanName($parameterName);
424 40
        }
425 300
        if (!$attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
426 120
            $parameterName = lcfirst($parameterName);
427 120
            $attribute = $this->getModel()->getAttribute($parameterName);
428 120
            if (!$attribute instanceof StructAttributeModel) {
429 12
                $attribute = $this->getModel()->getAttributeByCleanName($parameterName);
430 4
            }
431 40
        }
432 300
        $setValueAnnotation = '%s %s value';
433 300
        $annotationBlock = new PhpAnnotationBlock();
434 300
        if ($attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
435 300
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), $parameterName));
436 300
            $this->addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, $annotationBlock, $attribute);
437 100
        } elseif (empty($attribute)) {
438 6
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), lcfirst($parameterName)));
439 6
            $this->addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, $annotationBlock, $parameterName);
440 2
        }
441 300
        return $annotationBlock;
442
    }
443
    /**
444
     * @param string $setOrGet
445
     * @param PhpAnnotationBlock $annotationBlock
446
     * @param StructAttributeModel $attribute
447
     * @return Struct
448
     */
449 300
    protected function addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
450
    {
451 100
        switch ($setOrGet) {
452 300
            case 'set':
453 300
                if ($attribute->getRemovableFromRequest()) {
454 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');
455 10
                }
456 300
                if ($attribute->isAChoice()) {
457 24
                    $annotationBlock->addChild('This property belongs to a choice that allows only one property to exist. It is therefore removable from the request, consequently if the value assigned to this property is null, the property is removed from this object');
458 8
                }
459 300
                if ($attribute->isXml()) {
460
                    $annotationBlock
461 6
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::hasChildNodes()'))
462 6
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::saveXML()'))
463 6
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMNode::item()'));
464 2
                }
465 300
                if ($this->getGenerator()->getOptionValidation()) {
466 294
                    if ($attribute->isAChoice()) {
467 24
                        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
468 8
                    }
469 294
                    if (($model = $this->getRestrictionFromStructAttribute($attribute)) instanceof StructModel) {
470
                        $annotationBlock
471 138
                            ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
472 138
                            ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
473 138
                            ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
474 290
                    } elseif ($attribute->isArray()) {
475 150
                        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
476 50
                    }
477 98
                }
478 300
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()));
479 300
                break;
480 300
            case 'get':
481 300
                if ($attribute->getRemovableFromRequest()) {
482 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)');
483 10
                }
484 100
                $this
485 300
                    ->addStructMethodsGetAnnotationBlockFromXmlAttribute($annotationBlock, $attribute)
486 300
                    ->addStructMethodsGetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeGetAnnotation($attribute));
487 300
                break;
488
        }
489 300
        return $this;
490
    }
491
    /**
492
     * @param string $setOrGet
493
     * @param PhpAnnotationBlock $annotationBlock
494
     * @param string $attributeName
495
     * @return Struct
496
     */
497 6
    protected function addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, PhpAnnotationBlock $annotationBlock, $attributeName)
498
    {
499 2
        switch ($setOrGet) {
500 6
            case 'set':
501 6
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, lcfirst($attributeName), lcfirst($attributeName));
502 6
                break;
503 6
            case 'get':
504 6
                $this->addStructMethodsGetAnnotationBlock($annotationBlock, lcfirst($attributeName));
505 6
                break;
506
        }
507 6
        return $this;
508
    }
509
    /**
510
     * @param PhpAnnotationBlock $annotationBlock
511
     * @param string $type
512
     * @param string $name
513
     * @return Struct
514
     */
515 300
    protected function addStructMethodsSetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $type, $name)
516
    {
517 300
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $type, $name)))->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
518 300
        return $this;
519
    }
520
    /**
521
     * @param PhpAnnotationBlock $annotationBlock
522
     * @param string $attributeType
523
     * @return Struct
524
     */
525 300
    protected function addStructMethodsGetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $attributeType)
526
    {
527 300
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $attributeType));
528 300
        return $this;
529
    }
530
    /**
531
     * @param PhpAnnotationBlock $annotationBlock
532
     * @param StructAttributeModel $attribute
533
     * @return Struct
534
     */
535 300
    protected function addStructMethodsGetAnnotationBlockFromXmlAttribute(PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
536
    {
537 300
        if ($attribute->isXml()) {
538
            $annotationBlock
539 6
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::loadXML()'))
540 6
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, 'bool $asString true: returns XML string, false: returns \DOMDocument'));
541 2
        }
542 300
        return $this;
543
    }
544
    /**
545
     * @param PhpAnnotationBlock $annotationBlock
546
     * @return Struct
547
     */
548 300
    protected function addStructPropertiesToAnnotationBlock(PhpAnnotationBlock $annotationBlock)
549
    {
550 300
        return $this->addStructPropertiesToAnnotationBlockUses($annotationBlock)->addStructPropertiesToAnnotationBlockParams($annotationBlock);
551
    }
552
    /**
553
     * @param PhpAnnotationBlock $annotationBlock
554
     * @return Struct
555
     */
556 300
    protected function addStructPropertiesToAnnotationBlockUses(PhpAnnotationBlock $annotationBlock)
557
    {
558 300
        foreach ($this->getModelAttributes() as $attribute) {
559 300
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(), $attribute->getSetterName())));
560 100
        }
561 300
        return $this;
562
    }
563
    /**
564
     * @param PhpAnnotationBlock $annotationBlock
565
     * @return Struct
566
     */
567 300
    protected function addStructPropertiesToAnnotationBlockParams(PhpAnnotationBlock $annotationBlock)
568
    {
569 300
        foreach ($this->getModelAttributes() as $attribute) {
570 300
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()))));
571 100
        }
572 300
        return $this;
573
    }
574
    /**
575
     * @param PhpMethod $method
576
     * @return PhpAnnotationBlock
577
     */
578 156
    protected function getStructMethodsAddToAnnotationBlock(PhpMethod $method)
579
    {
580 156
        $methodParameters = $method->getParameters();
581 156
        $firstParameter = array_shift($methodParameters);
582 156
        $attribute = $this->getModel()->getAttribute($firstParameter->getModel()->getName());
583 156
        $annotationBlock = new PhpAnnotationBlock();
584 156
        if ($attribute instanceof StructAttributeModel) {
585 156
            $model = $this->getRestrictionFromStructAttribute($attribute);
586 156
            $annotationBlock->addChild(sprintf('Add item to %s value', $attribute->getCleanName()));
587 156
            if ($model instanceof StructModel) {
588
                $annotationBlock
589 24
                    ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
590 24
                    ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)));
591 8
            }
592
            $annotationBlock
593 156
                ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'))
594 156
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeTypeSetAnnotation($attribute, false))))
595 156
                ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
596 52
        }
597 156
        return $annotationBlock;
598
    }
599
    /**
600
     * @param PhpMethod $method
601
     * @return PhpAnnotationBlock
602
     */
603 168
    protected function getStructMethodsValidateArrayAnnotationBlock(PhpMethod $method)
604
    {
605 168
        $methodName = lcfirst(mb_substr($method->getName(), mb_strpos($method->getName(), 'ForArrayConstraintsFrom') + mb_strlen('ForArrayConstraintsFrom')));
606 168
        return new PhpAnnotationBlock([
607 168
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is responsible for validating the values passed to the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
608 168
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is willingly generated in order to preserve the one-line inline validation within the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
609 168
            new PhpAnnotation(self::ANNOTATION_PARAM, 'array $values'),
610 168
            new PhpAnnotation(self::ANNOTATION_RETURN, 'string A non-empty message if the values does not match the validation rules'),
611 56
        ]);
612
    }
613
    /**
614
     * @param PhpMethod $method
615
     * @return PhpAnnotationBlock
616
     */
617 12
    protected function getStructMethodsValidateUnionAnnotationBlock(PhpMethod $method)
618
    {
619 12
        $methodName = lcfirst(mb_substr($method->getName(), mb_strpos($method->getName(), 'ForUnionConstraintsFrom') + mb_strlen('ForUnionConstraintsFrom')));
620 12
        return new PhpAnnotationBlock([
621 12
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is responsible for validating the value passed to the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
622 12
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is willingly generated in order to preserve the one-line inline validation within the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
623 12
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This is a set of validation rules based on the union types associated to the property being set by the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
624 12
            new PhpAnnotation(self::ANNOTATION_PARAM, 'mixed $value'),
625 12
            new PhpAnnotation(self::ANNOTATION_RETURN, 'string A non-empty message if the values does not match the validation rules'),
626 4
        ]);
627
    }
628
    /**
629
     * @param PhpMethod $method
630
     * @return PhpAnnotationBlock
631
     */
632 24
    protected function getStructMethodsValidateChoiceAnnotationBlock(PhpMethod $method)
633
    {
634 24
        $methodName = lcfirst(mb_substr($method->getName(), mb_strpos($method->getName(), 'ForChoiceConstraintsFrom') + mb_strlen('ForChoiceConstraintsFrom')));
635 24
        return new PhpAnnotationBlock([
636 24
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is responsible for validating the value passed to the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
637 24
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is willingly generated in order to preserve the one-line inline validation within the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
638 24
            new PhpAnnotation(PhpAnnotation::NO_NAME, 'This has to validate that the property which is being set is the only one among the given choices', self::ANNOTATION_LONG_LENGTH),
639 24
            new PhpAnnotation(self::ANNOTATION_PARAM, 'mixed $value'),
640 24
            new PhpAnnotation(self::ANNOTATION_RETURN, 'string A non-empty message if the values does not match the validation rules'),
641 8
        ]);
642
    }
643
    /**
644
     * @param PhpMethod $method
645
     * @param string $type
646
     * @return PhpAnnotationBlock
647
     */
648 12
    protected function getStructMethodsValidateLengthAnnotationBlock(PhpMethod $method, $type = '')
649
    {
650 12
        $replace = sprintf('%sLengthConstraintFrom', ucfirst($type));
651 12
        $methodName = lcfirst(mb_substr($method->getName(), mb_strpos($method->getName(), $replace) + mb_strlen($replace)));
652 12
        return new PhpAnnotationBlock([
653 12
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is responsible for validating the value passed to the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
654 12
            new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('This method is willingly generated in order to preserve the one-line inline validation within the %s method', $methodName), self::ANNOTATION_LONG_LENGTH),
655 12
            new PhpAnnotation(PhpAnnotation::NO_NAME, 'This has to validate that the items contained by the array match the length constraint', self::ANNOTATION_LONG_LENGTH),
656 12
            new PhpAnnotation(self::ANNOTATION_PARAM, 'mixed $values'),
657 12
            new PhpAnnotation(self::ANNOTATION_RETURN, 'string A non-empty message if the values does not match the validation rules'),
658 4
        ]);
659
    }
660
    /**
661
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel()
662
     * @return StructModel
663
     */
664 390
    public function getModel()
665
    {
666 390
        return parent::getModel();
667
    }
668
    /**
669
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
670
     * @throws \InvalidArgumentException
671
     * @param AbstractModel $model
672
     * @return StructArray
673
     */
674 390
    public function setModel(AbstractModel $model)
675
    {
676 390
        if (!$model instanceof StructModel) {
677 6
            throw new \InvalidArgumentException('Model must be an instance of a Struct', __LINE__);
678
        }
679 384
        return parent::setModel($model);
680
    }
681
    /**
682
     * @param PhpMethod $method
683
     * @param StructAttributeModel $attribute
684
     * @param $parameterName
685
     * @param bool $itemType
686
     */
687 294
    protected function applyRules(PhpMethod $method, StructAttributeModel $attribute, $parameterName, $itemType = false)
688
    {
689 294
        if ($this->getGenerator()->getOptionValidation()) {
690 294
            $rules = new Rules($this, $method, $attribute, $this->methods);
691 294
            $rules->applyRules($parameterName, $itemType);
692 98
        }
693 294
    }
694
}
695