Passed
Push — develop ( 39dbcd...b263cf )
by Mikaël
24:14
created

Struct::fillClassMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
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 306
    protected function getClassConstants(ConstantContainer $constants)
25
    {
26 306
    }
27
    /**
28
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock()
29
     */
30
    protected function getConstantAnnotationBlock(PhpConstant $constant)
31
    {
32
    }
33
    /**
34
     * @return StructAttributeContainer
35
     */
36 354
    protected function getModelAttributes()
37
    {
38 354
        return $this->getModel()->getProperAttributes(true);
39
    }
40
    /**
41
     * @param PropertyContainer $properties
42
     */
43 354
    protected function getClassProperties(PropertyContainer $properties)
44
    {
45 354
        foreach ($this->getModelAttributes() as $attribute) {
46 294
            $properties->add(new PhpProperty($attribute->getCleanName(), PhpProperty::NO_VALUE));
47 118
        }
48 354
    }
49
    /**
50
     * @return PhpAnnotationBlock $property
51
     */
52 294
    protected function getPropertyAnnotationBlock(PhpProperty $property)
53
    {
54 294
        $annotationBlock = new PhpAnnotationBlock();
55 294
        $annotationBlock->addChild(sprintf('The %s', $property->getName()));
56 294
        $attribute = $this->getModel()->getAttribute($property->getName());
57 294
        if (!$attribute instanceof StructAttributeModel) {
58 6
            $attribute = $this->getModel()->getAttributeByCleanName($property->getName());
59 2
        }
60 294
        if ($attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
61 294
            $this->defineModelAnnotationsFromWsdl($annotationBlock, $attribute);
62 294
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_VAR, $this->getStructAttributeTypeSetAnnotation($attribute, true)));
63 98
        }
64 294
        return $annotationBlock;
65
    }
66 306
    protected function fillClassMethods()
67
    {
68 102
        $this
69 306
            ->addStructMethodConstruct()
70 306
            ->addStructMethodsSetAndGet();
71 306
    }
72
    /**
73
     * @return Struct
74
     */
75 306
    protected function addStructMethodConstruct()
76
    {
77 306
        if (0 < count($parameters = $this->getStructMethodParametersValues())) {
78 294
            $method = new PhpMethod(self::METHOD_CONSTRUCT, $parameters);
79 294
            $this->addStructMethodConstructBody($method);
80 294
            $this->methods->add($method);
81 98
        }
82 306
        return $this;
83
    }
84
    /**
85
     * @param PhpMethod $method
86
     * @return Struct
87
     */
88 294
    protected function addStructMethodConstructBody(PhpMethod $method)
89
    {
90 294
        $count = $this->getModelAttributes()->count();
91 294
        foreach ($this->getModelAttributes() as $index => $attribute) {
92 294
            if ($index === 0) {
93 294
                $method->addChild('$this');
94 98
            }
95 294
            $this->addStructMethodConstructBodyForAttribute($method, $attribute, $count - 1 === $index);
96 98
        }
97 294
        return $this;
98
    }
99
    /**
100
     * @param PhpMethod $method
101
     * @param StructAttributeModel $attribute
102
     * @param bool $isLast
103
     * @return Struct
104
     */
105 294
    protected function addStructMethodConstructBodyForAttribute(PhpMethod $method, StructAttributeModel $attribute, $isLast)
106
    {
107 294
        $uniqueString = $attribute->getUniqueString($attribute->getCleanName(), 'method');
108 294
        $method->addChild($method->getIndentedString(sprintf('->%s($%s)%s', $attribute->getSetterName(), lcfirst($uniqueString), $isLast ? ';' : ''), 1));
109 294
        return $this;
110
    }
111
    /**
112
     * @return PhpFunctionParameter[]
113
     */
114 306
    protected function getStructMethodParametersValues()
115
    {
116 306
        $parametersValues = [];
117 306
        foreach ($this->getModelAttributes() as $attribute) {
118 294
            $parametersValues[] = $this->getStructMethodParameter($attribute);
119 102
        }
120 306
        return $parametersValues;
121
    }
122
    /**
123
     * @param StructAttributeModel $attribute
124
     * @return PhpFunctionParameter
125
     */
126 294
    protected function getStructMethodParameter(StructAttributeModel $attribute)
127
    {
128
        try {
129 294
            return new PhpFunctionParameter(
130 294
                lcfirst($attribute->getUniqueString($attribute->getCleanName(), 'method')),
131 294
                $attribute->getDefaultValue(),
132 294
                $this->getStructMethodParameterType($attribute),
133 147
                $attribute
134 98
            );
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 294
    protected function getStructMethodParameterType(StructAttributeModel $attribute, $returnArrayType = true)
145
    {
146 294
        return self::getValidType($this->getStructAttributeTypeHint($attribute, $returnArrayType), $this->getGenerator()->getOptionXsdTypesPath(), null);
147
    }
148
    /**
149
     * @return Struct
150
     */
151 306
    protected function addStructMethodsSetAndGet()
152
    {
153 306
        foreach ($this->getModelAttributes() as $attribute) {
154 98
            $this
155 294
                ->addStructMethodGet($attribute)
156 294
                ->addStructMethodSet($attribute)
157 294
                ->addStructMethodAddTo($attribute);
158 102
        }
159 306
        return $this;
160
    }
161
    /**
162
     * @param StructAttributeModel $attribute
163
     * @return Struct
164
     */
165 294
    protected function addStructMethodAddTo(StructAttributeModel $attribute)
166
    {
167 294
        if ($attribute->isArray()) {
168 150
            $method = new PhpMethod(sprintf('addTo%s', ucfirst($attribute->getCleanName())), [
169 150
                new PhpFunctionParameter('item', PhpFunctionParameter::NO_VALUE, $this->getStructMethodParameterType($attribute, false), $attribute),
170 58
            ]);
171 150
            $this->addStructMethodAddToBody($method, $attribute);
172 150
            $this->methods->add($method);
173 50
        }
174 294
        return $this;
175
    }
176
    /**
177
     * @param PhpMethod $method
178
     * @param StructAttributeModel $attribute
179
     * @return Struct
180
     */
181 150
    protected function addStructMethodAddToBody(PhpMethod $method, StructAttributeModel $attribute)
182
    {
183 150
        if ($this->getGenerator()->getOptionValidation()) {
184 150
            $this->applyRules($method, $attribute, 'item', true);
185 50
        }
186
187 150
        if ($attribute->nameIsClean()) {
188 150
            $assignment = sprintf('$this->%s[] = $item;', $attribute->getCleanName());
189 50
        } else {
190
            $assignment = sprintf('$this->%s[] = $this->{\'%s\'}[] = $item;', $attribute->getCleanName(), addslashes($attribute->getName()), $attribute->getCleanName());
191
        }
192
        $method
193 150
            ->addChild($assignment)
194 150
            ->addChild('return $this;');
195 150
        return $this;
196
    }
197
    /**
198
     * @param StructAttributeModel $attribute
199
     * @return Struct
200
     */
201 294
    protected function addStructMethodSet(StructAttributeModel $attribute)
202
    {
203 294
        $method = new PhpMethod($attribute->getSetterName(), [
204 294
            $this->getStructMethodParameter($attribute),
205 98
        ]);
206 294
        $this->addStructMethodSetBody($method, $attribute);
207 294
        $this->methods->add($method);
208 294
        return $this;
209
    }
210
    /**
211
     * @param PhpMethod $method
212
     * @param StructAttributeModel $attribute
213
     * @return Struct
214
     */
215 294
    protected function addStructMethodSetBody(PhpMethod $method, StructAttributeModel $attribute)
216
    {
217 294
        $parameters = $method->getParameters();
218 294
        $parameter = array_shift($parameters);
219 294
        $parameterName = is_string($parameter) ? $parameter : $parameter->getName();
220 294
        if ($this->getGenerator()->getOptionValidation()) {
221 288
            $this->applyRules($method, $attribute, $parameterName);
222 96
        }
223 98
        return $this
224 294
            ->addStructMethodSetBodyAssignment($method, $attribute, $parameterName)
225 294
            ->addStructMethodSetBodyReturn($method);
226
    }
227
    /**
228
     * @param PhpMethod $method
229
     * @param StructAttributeModel $attribute
230
     * @param string $parameterName
231
     * @return Struct
232
     */
233 294
    protected function addStructMethodSetBodyAssignment(PhpMethod $method, StructAttributeModel $attribute, $parameterName)
234
    {
235 294
        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 282
            $method->addChild($this->getStructMethodSetBodyAssignment($attribute, $parameterName));
244
        }
245 294
        return $this;
246
    }
247
    /**
248
     * @param PhpMethod $method
249
     * @return Struct
250
     */
251 294
    protected function addStructMethodSetBodyReturn(PhpMethod $method)
252
    {
253 294
        $method->addChild('return $this;');
254 294
        return $this;
255
    }
256
    /**
257
     * @param StructAttributeModel $attribute
258
     * @param string $parameterName
259
     * @return string
260
     */
261 294
    protected function getStructMethodSetBodyAssignment(StructAttributeModel $attribute, $parameterName)
262
    {
263 294
        $prefix = '$';
264 294
        if ($this->isAttributeAList($attribute)) {
265 18
            $prefix = '';
266 18
            $parameterName = sprintf('is_array($%1$s) ? implode(\' \', $%1$s) : null', $parameterName);
267 294
        } 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 294
        if ($attribute->nameIsClean()) {
272 294
            $assignment = sprintf('$this->%s = %s%s;', $attribute->getName(), $prefix, $parameterName);
273 98
        } else {
274 6
            $assignment = sprintf('$this->%s = $this->{\'%s\'} = %s%s;', $attribute->getCleanName(), addslashes($attribute->getName()), $prefix, $parameterName);
275
        }
276 294
        return $assignment;
277
    }
278
    /**
279
     * @param PhpMethod $method
280
     * @param StructAttributeModel $attribute
281
     * @param string $thisAccess
282
     * @return Struct
283
     */
284 294
    protected function addStructMethodGetBody(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
285
    {
286 294
        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 294
    protected function addStructMethodGetBodyReturn(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
295
    {
296 294
        $return = sprintf('return $this->%s;', $thisAccess);
297 294
        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 294
        } elseif ($attribute->getRemovableFromRequest() || $attribute->isAChoice()) {
310 54
            $return = sprintf('return isset($this->%1$s) ? $this->%1$s : null;', $thisAccess);
311 18
        }
312 294
        $method->addChild($return);
313 294
        return $this;
314
    }
315
    /**
316
     * @param StructAttributeModel $attribute
317
     * @return Struct
318
     */
319 294
    protected function addStructMethodGet(StructAttributeModel $attribute)
320
    {
321 294
        $method = new PhpMethod($attribute->getGetterName(), $this->getStructMethodGetParameters($attribute));
322 294
        if ($attribute->nameIsClean()) {
323 294
            $thisAccess = sprintf('%s', $attribute->getName());
324 98
        } else {
325 6
            $thisAccess = sprintf('{\'%s\'}', addslashes($attribute->getName()));
326
        }
327 294
        $this->addStructMethodGetBody($method, $attribute, $thisAccess);
328 294
        $this->methods->add($method);
329 294
        return $this;
330
    }
331
    /**
332
     * @param StructAttributeModel $attribute
333
     * @return PhpFunctionParameter[]
334
     */
335 294
    protected function getStructMethodGetParameters(StructAttributeModel $attribute)
336
    {
337 294
        $parameters = [];
338 294
        if ($attribute->isXml()) {
339 6
            $parameters[] = new PhpFunctionParameter('asString', true, null, $attribute);
340 2
        }
341 294
        return $parameters;
342
    }
343
    /**
344
     * @return PhpAnnotationBlock|null
345
     */
346 294
    protected function getMethodAnnotationBlock(PhpMethod $method)
347
    {
348 294
        return $this->getStructMethodAnnotationBlock($method);
349
    }
350
    /**
351
     * @param PhpMethod $method
352
     * @return PhpAnnotationBlock|null
353
     */
354 294
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
355
    {
356 294
        $annotationBlock = null;
357 294
        switch ($method->getName()) {
358 294
            case self::METHOD_CONSTRUCT:
359 294
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
360 294
                break;
361 294
            case 0 === mb_strpos($method->getName(), 'get'):
362 294
            case 0 === mb_strpos($method->getName(), 'set'):
363 294
                $annotationBlock = $this->getStructMethodsSetAndGetAnnotationBlock($method);
364 294
                break;
365 192
            case 0 === mb_strpos($method->getName(), 'addTo'):
366 150
                $annotationBlock = $this->getStructMethodsAddToAnnotationBlock($method);
367 150
                break;
368 192
            case false !== mb_strpos($method->getName(), 'ForUnionConstraintsFrom'):
369 12
                $annotationBlock = $this->getStructMethodsValidateUnionAnnotationBlock($method);
370 12
                break;
371 180
            case false !== mb_strpos($method->getName(), 'ForArrayConstraintsFrom'):
372 162
                $annotationBlock = $this->getStructMethodsValidateArrayAnnotationBlock($method);
373 162
                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 98
        }
387 294
        return $annotationBlock;
388
    }
389
    /**
390
     * @return PhpAnnotationBlock
391
     */
392 294
    protected function getStructMethodConstructAnnotationBlock()
393
    {
394 294
        $annotationBlock = new PhpAnnotationBlock([
395 294
            sprintf('Constructor method for %s', $this->getModel()->getName()),
396 98
        ]);
397 294
        $this->addStructPropertiesToAnnotationBlock($annotationBlock);
398 294
        return $annotationBlock;
399
    }
400
    /**
401
     * @param PhpMethod $method
402
     * @return PhpAnnotationBlock
403
     */
404 294
    protected function getStructMethodsSetAndGetAnnotationBlock(PhpMethod $method)
405
    {
406 294
        $parameters = $method->getParameters();
407 294
        $setOrGet = mb_strtolower(mb_substr($method->getName(), 0, 3));
408 294
        $parameter = array_shift($parameters);
409
        /**
410
         * Only set parameter must be based on a potential PhpFunctionParameter
411
         */
412 294
        if ($parameter instanceof PhpFunctionParameter && $setOrGet === 'set') {
413 294
            $parameterName = ucfirst($parameter->getName());
414 98
        } else {
415 294
            $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 294
        $parameterName = preg_replace('/(_\d+)/', '', $parameterName);
421 294
        $attribute = $this->getModel()->getAttribute($parameterName);
422 294
        if (!$attribute instanceof StructAttributeModel) {
423 120
            $attribute = $this->getModel()->getAttributeByCleanName($parameterName);
424 40
        }
425 294
        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 294
        $setValueAnnotation = '%s %s value';
433 294
        $annotationBlock = new PhpAnnotationBlock();
434 294
        if ($attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
435 294
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), $parameterName));
436 294
            $this->addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, $annotationBlock, $attribute);
437 98
        } elseif (empty($attribute)) {
438 6
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), lcfirst($parameterName)));
439 6
            $this->addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, $annotationBlock, $parameterName);
440 2
        }
441 294
        return $annotationBlock;
442
    }
443
    /**
444
     * @param string $setOrGet
445
     * @param PhpAnnotationBlock $annotationBlock
446
     * @param StructAttributeModel $attribute
447
     * @return Struct
448
     */
449 294
    protected function addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
450
    {
451 98
        switch ($setOrGet) {
452 294
            case 'set':
453 294
                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 294
                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 294
                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 294
                if ($this->getGenerator()->getOptionValidation()) {
466 288
                    if ($attribute->isAChoice()) {
467 24
                        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
468 8
                    }
469 288
                    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 284
                    } elseif ($attribute->isArray()) {
475 144
                        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
476 48
                    }
477 96
                }
478 294
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()));
479 294
                break;
480 294
            case 'get':
481 294
                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 98
                $this
485 294
                    ->addStructMethodsGetAnnotationBlockFromXmlAttribute($annotationBlock, $attribute)
486 294
                    ->addStructMethodsGetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeGetAnnotation($attribute));
487 294
                break;
488
        }
489 294
        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 294
    protected function addStructMethodsSetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $type, $name)
516
    {
517 294
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $type, $name)))->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
518 294
        return $this;
519
    }
520
    /**
521
     * @param PhpAnnotationBlock $annotationBlock
522
     * @param string $attributeType
523
     * @return Struct
524
     */
525 294
    protected function addStructMethodsGetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $attributeType)
526
    {
527 294
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $attributeType));
528 294
        return $this;
529
    }
530
    /**
531
     * @param PhpAnnotationBlock $annotationBlock
532
     * @param StructAttributeModel $attribute
533
     * @return Struct
534
     */
535 294
    protected function addStructMethodsGetAnnotationBlockFromXmlAttribute(PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
536
    {
537 294
        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 294
        return $this;
543
    }
544
    /**
545
     * @param PhpAnnotationBlock $annotationBlock
546
     * @return Struct
547
     */
548 294
    protected function addStructPropertiesToAnnotationBlock(PhpAnnotationBlock $annotationBlock)
549
    {
550 294
        return $this->addStructPropertiesToAnnotationBlockUses($annotationBlock)->addStructPropertiesToAnnotationBlockParams($annotationBlock);
551
    }
552
    /**
553
     * @param PhpAnnotationBlock $annotationBlock
554
     * @return Struct
555
     */
556 294
    protected function addStructPropertiesToAnnotationBlockUses(PhpAnnotationBlock $annotationBlock)
557
    {
558 294
        foreach ($this->getModelAttributes() as $attribute) {
559 294
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(), $attribute->getSetterName())));
560 98
        }
561 294
        return $this;
562
    }
563
    /**
564
     * @param PhpAnnotationBlock $annotationBlock
565
     * @return Struct
566
     */
567 294
    protected function addStructPropertiesToAnnotationBlockParams(PhpAnnotationBlock $annotationBlock)
568
    {
569 294
        foreach ($this->getModelAttributes() as $attribute) {
570 294
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()))));
571 98
        }
572 294
        return $this;
573
    }
574
    /**
575
     * @param PhpMethod $method
576
     * @return PhpAnnotationBlock
577
     */
578 150
    protected function getStructMethodsAddToAnnotationBlock(PhpMethod $method)
579
    {
580 150
        $methodParameters = $method->getParameters();
581 150
        $firstParameter = array_shift($methodParameters);
582 150
        $attribute = $this->getModel()->getAttribute($firstParameter->getModel()->getName());
583 150
        $annotationBlock = new PhpAnnotationBlock();
584 150
        if ($attribute instanceof StructAttributeModel) {
585 150
            $model = $this->getRestrictionFromStructAttribute($attribute);
586 150
            $annotationBlock->addChild(sprintf('Add item to %s value', $attribute->getCleanName()));
587 150
            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 150
                ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'))
594 150
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeTypeSetAnnotation($attribute, false))))
595 150
                ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
596 50
        }
597 150
        return $annotationBlock;
598
    }
599
    /**
600
     * @param PhpMethod $method
601
     * @return PhpAnnotationBlock
602
     */
603 162
    protected function getStructMethodsValidateArrayAnnotationBlock(PhpMethod $method)
604
    {
605 162
        $methodName = lcfirst(mb_substr($method->getName(), mb_strpos($method->getName(), 'ForArrayConstraintsFrom') + mb_strlen('ForArrayConstraintsFrom')));
606 162
        return new PhpAnnotationBlock([
607 162
            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 162
            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 162
            new PhpAnnotation(self::ANNOTATION_PARAM, 'array $values'),
610 162
            new PhpAnnotation(self::ANNOTATION_RETURN, 'string A non-empty message if the values does not match the validation rules'),
611 54
        ]);
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 384
    public function getModel()
665
    {
666 384
        return parent::getModel();
667
    }
668
    /**
669
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
670
     * @throws \InvalidArgumentException
671
     * @param AbstractModel $model
672
     * @return StructArray
673
     */
674 384
    public function setModel(AbstractModel $model)
675
    {
676 384
        if (!$model instanceof StructModel) {
677 6
            throw new \InvalidArgumentException('Model must be an instance of a Struct', __LINE__);
678
        }
679 378
        return parent::setModel($model);
680
    }
681
    /**
682
     * @param PhpMethod $method
683
     * @param StructAttributeModel $attribute
684
     * @param $parameterName
685
     * @param bool $itemType
686
     */
687 288
    protected function applyRules(PhpMethod $method, StructAttributeModel $attribute, $parameterName, $itemType = false)
688
    {
689 288
        if ($this->getGenerator()->getOptionValidation()) {
690 288
            $rules = new Rules($this, $method, $attribute, $this->methods);
691 288
            $rules->applyRules($parameterName, $itemType);
692 96
        }
693 288
    }
694
}
695