Completed
Push — feature/issue-168 ( f01c3b...77e7a9 )
by Mikaël
23:44
created

EnumerationRule::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\File\StructEnum;
6
use WsdlToPhp\PackageGenerator\Model\Struct;
7
8
/**
9
 * Class EnumerationRule
10
 * @link https://www.w3.org/TR/xmlschema-2/#rf-enumeration
11
 * Validation Rule: enumeration valid
12
 * A value in a ·value space· is facet-valid with respect to ·enumeration· if the value is one of the values specified in {value}
13
 */
14
class EnumerationRule extends AbstractRule
15
{
16
17
    /**
18
     * @var Struct
19
     */
20
    protected $model;
21
22
    /**
23
     * @return string
24
     */
25 111
    public function name()
26
    {
27 111
        return 'enumeration';
28
    }
29
30
    /**
31
     * @param string $parameterName
32
     * @param mixed $value
33
     * @param bool $itemType
34
     * @return string
35
     */
36 111
    public function testConditions($parameterName, $value, $itemType = false)
37
    {
38 111
        $test = '';
39 111
        if ($this->getRestrictionModel()) {
40 111
            $test = sprintf('!%s::%s($%s)', $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName);
41 54
        }
42 111
        return $test;
43
    }
44
45
    /**
46
     * @param string $parameterName
47
     * @param mixed $value
48
     * @param bool $itemType
49
     * @return string
50
     */
51 111
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
52
    {
53 111
        $exceptionMessage = '';
54 111
        if ($this->getRestrictionModel()) {
55 111
            $exceptionMessage = sprintf('sprintf(\'Invalid value(s) %%s, please use one of: %%s from enumeration class %2$s\', is_array($%1$s) ? implode(\', \', $%1$s) : var_export($%1$s, true), implode(\', \', %2$s::%3$s()))', $parameterName, $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES);
56 54
        }
57 111
        return $exceptionMessage;
58
    }
59
60
    /**
61
     * @return Struct|null
62
     */
63 111
    protected function getRestrictionModel()
64
    {
65 111
        if (!$this->model) {
66 111
            $this->model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
67 54
        }
68 111
        return $this->model;
69
    }
70
}
71