Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
EnumerationRule
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 testConditions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 exceptionMessageOnTestFailure
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getRestrictionModel
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7use WsdlToPhp\PackageGenerator\File\StructEnum;
8use WsdlToPhp\PackageGenerator\Model\Struct;
9
10/**
11 * @see https://www.w3.org/TR/xmlschema-2/#rf-enumeration
12 * Validation Rule: enumeration valid
13 * A value in a ·value space· is facet-valid with respect to ·enumeration· if the value is one of the values specified in {value}
14 */
15final class EnumerationRule extends AbstractRule
16{
17    protected ?Struct $model = null;
18
19    public function name(): string
20    {
21        return 'enumeration';
22    }
23
24    public function testConditions(string $parameterName, $value, bool $itemType = false): string
25    {
26        $test = '';
27        if ($this->getRestrictionModel()) {
28            $test = sprintf('!%s::%s($%s)', $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName);
29        }
30
31        return $test;
32    }
33
34    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
35    {
36        $exceptionMessage = '';
37        if ($restrictionModel = $this->getRestrictionModel()) {
38            $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, $restrictionModel->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES);
39        }
40
41        return $exceptionMessage;
42    }
43
44    protected function getRestrictionModel(): ?Struct
45    {
46        if (!$this->model) {
47            $this->model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
48        }
49
50        return $this->model;
51    }
52}