Completed
Push — master ( 650bf0...3755f4 )
by Neomerx
03:55
created

EachExpression::__construct()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Limoncello\Validation\Expressions;
2
3
/**
4
 * Copyright 2015-2016 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Validation\Contracts\ErrorAggregatorInterface;
20
use Limoncello\Validation\Contracts\RuleInterface;
21
22
/**
23
 * @package Limoncello\Validation
24
 */
25
class EachExpression extends BaseExpression
26
{
27
    /**
28
     * @var RuleInterface
29
     */
30
    private $rule;
31
32
    /**
33
     * @param RuleInterface $rule
34
     */
35 1
    public function __construct(RuleInterface $rule)
36
    {
37 1
        $rule->setParentRule($this);
38 1
        $this->rule = $rule;
39 1
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 1
    public function validate($input)
45
    {
46 1
        foreach ($input as $value) {
47 1
            foreach ($this->getRule()->validate($value) as $error) {
48 1
                yield $error;
49
            }
50
        }
51 1
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function isStateless()
57
    {
58
        return $this->getRule()->isStateless();
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 1
    public function onFinish(ErrorAggregatorInterface $aggregator)
65
    {
66 1
        $this->getRule()->onFinish($aggregator);
67 1
    }
68
69
    /**
70
     * @return RuleInterface
71
     */
72 1
    protected function getRule()
73
    {
74 1
        return $this->rule;
75
    }
76
}
77