Completed
Push — master ( 9b8f8b...ad8621 )
by Mikaël
29:42
created

MaxOccursRule::symbol()   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
/**
6
 * Class LengthRule
7
 * @link https://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#p-max_occurs
8
 * Validation Rule: Element Sequence Locally Valid (Particle)
9
 * For a sequence (possibly empty) of element information items to be locally ·valid· with respect to a particle the appropriate case among the following must be true:
10
 *  - 1 If the {term} is a wildcard, then all of the following must be true:
11
 *   - 1.1 The length of the sequence must be greater than or equal to the {min occurs}.
12
 *   - 1.2 If {max occurs} is a number, the length of the sequence must be less than or equal to the {max occurs}.
13
 *   - 1.3 Each element information item in the sequence must be ·valid· with respect to the wildcard as defined by Item Valid (Wildcard) (§3.10.4).
14
 *  - 2 If the {term} is an element declaration, then all of the following must be true:
15
 *   - 2.1 The length of the sequence must be greater than or equal to the {min occurs}.
16
 *   - 2.2 If {max occurs} is a number, the length of the sequence must be less than or equal to the {max occurs}.
17
 *   - 2.3 For each element information item in the sequence one of the following must be true:
18
 *    - 2.3.1 The element declaration is local (i.e. its {scope} must not be global), its {abstract} is false, the element information item's [namespace name] is identical to the element declaration's {target namespace} (where an ·absent· {target namespace} is taken to be identical to a [namespace name] with no value) and the element information item's [local name] matches the element declaration's {name}.
19
 *            In this case the element declaration is the ·context-determined declaration· for the element information item with respect to Schema-Validity Assessment (Element) (§3.3.4) and Assessment Outcome (Element) (§3.3.5).
20
 *    - 2.3.2 The element declaration is top-level (i.e. its {scope} is global), {abstract} is false, the element information item's [namespace name] is identical to the element declaration's {target namespace} (where an ·absent· {target namespace} is taken to be identical to a [namespace name] with no value) and the element information item's [local name] matches the element declaration's {name}.
21
 *            In this case the element declaration is the ·context-determined declaration· for the element information item with respect to Schema-Validity Assessment (Element) (§3.3.4) and Assessment Outcome (Element) (§3.3.5).
22
 *    - 2.3.3 The element declaration is top-level (i.e. its {scope} is global), its {disallowed substitutions} does not contain substitution, the [local ] and [namespace name] of the element information item resolve to an element declaration, as defined in QName resolution (Instance) (§3.15.4) -- [Definition:]  call this declaration the substituting declaration and the ·substituting declaration· together with the particle's element declaration's {disallowed substitutions} is validly substitutable for the particle's element declaration as defined in Substitution Group OK (Transitive) (§3.3.6).
23
 *            In this case the ·substituting declaration· is the ·context-determined declaration· for the element information item with respect to Schema-Validity Assessment (Element) (§3.3.4) and Assessment Outcome (Element) (§3.3.5).
24
 *  - 3 If the {term} is a model group, then all of the following must be true:
25
 *   - 3.1 There is a ·partition· of the sequence into n sub-sequences such that n is greater than or equal to {min occurs}.
26
 *   - 3.2 If {max occurs} is a number, n must be less than or equal to {max occurs}.
27
 *   - 3.3 Each sub-sequence in the ·partition· is ·valid· with respect to that model group as defined in Element Sequence Valid (§3.8.4).
28
 * Note: Clauses clause 1 and clause 2.3.3 do not interact: an element information item validatable by a declaration with a substitution group head in a different namespace is not validatable by a wildcard which accepts the head's namespace but not its own.
29
 */
30
class MaxOccursRule extends AbstractMinMaxRule
31
{
32
33
    /**
34
     * @return string
35
     */
36 48
    public function name()
37
    {
38 48
        return 'maxOccurs';
39
    }
40
41
    /**
42
     * @return string
43
     */
44 48
    public function symbol()
45
    {
46 48
        return self::SYMBOL_MAX_INCLUSIVE;
47
    }
48
49
    /**
50
     * @param string $parameterName
51
     * @param mixed $value
52
     * @param bool $itemType
53
     * @return string
54
     */
55 192
    final public function testConditions($parameterName, $value, $itemType = false)
56
    {
57 192
        $test = '';
58 192
        if ('unbounded' !== $value && $this->getAttribute()->isArray()) {
59 48
            if ($itemType) {
60 48
                $test = 'is_array($this->%1$s) && count($this->%1$s) %3$s %2$d';
61 48
                $symbol = self::SYMBOL_MAX_EXCLUSIVE;
62 24
            } else {
63 48
                $test = 'is_array($%4$s) && count($%4$s) %3$s %2$d';
64 48
                $symbol = $this->symbol();
65
            }
66 48
            $test = sprintf($test, $this->getAttribute()->getCleanName(), $value, $symbol, $parameterName);
67 24
        }
68 192
        return $test;
69
    }
70
71
    /**
72
     * @param string $parameterName
73
     * @param mixed $value
74
     * @param bool $itemType
75
     * @return string
76
     */
77 48
    final public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
78
    {
79 48
        if ($itemType) {
80 48
            $message = 'sprintf(\'You can\\\'t add anymore element to this property that already contains %%s elements, the number of elements contained by the property must be %1$s %2$s\', count($this->%4$s))';
81 24
        } else {
82 48
            $message = 'sprintf(\'Invalid count of %%s, the number of elements contained by the property must be %1$s %2$s\', count($%3$s))';
83
        }
84 48
        return sprintf($message, $this->comparisonString(), $value, $parameterName, $this->getAttribute()->getCleanName());
85
    }
86
}
87