Completed
Push — develop ( de5d0b...06b47b )
by Mikaël
31:11
created

EnumerationRule::applyRule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 13
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\File\StructEnum;
6
use WsdlToPhp\PackageGenerator\Model\Struct;
7
8
class EnumerationRule extends AbstractRule
9
{
10
    /**
11
     * @see \WsdlToPhp\PackageGenerator\File\Validation\AbstractValidation::addRule()
12
     * @param string $parameterName
13
     * @param mixed $value
14
     * @param bool $itemType
15
     * @return EnumerationRule
16
     */
17
    public function applyRule($parameterName, $value, $itemType = false)
18
    {
19
        $attribute = $this->getAttribute();
20
        if (($model = $this->getFile()->getRestrictionFromStructAttribute($attribute)) instanceof Struct) {
21
            $this
22
                ->getMethod()
23
                    ->addChild('// validation for constraint: enumeration')
24
                    ->addChild(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName))
25
                        ->addChild($this->getMethod()->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))
26
                    ->addChild('}');
27
        }
28
        return $this;
29
    }
30
}
31