Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Value
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * @license MIT
5 * @author hazuki3417<hazuki3417@gmail.com>
6 * @copyright 2022 hazuki3417 all rights reserved.
7 */
8
9namespace Selen\MongoDB\Validator;
10
11use Selen\Data\ArrayPath;
12use Selen\MongoDB\Validator\Model\ValidateResult;
13use Selen\MongoDB\Validator\Model\ValidatorResult;
14
15class Value
16{
17    /** @var ArrayPath */
18    private $arrayPath;
19
20    /** @var \ReflectionAttribute<object>[] */
21    private $attributeValueValidates = [];
22
23    /**
24     * インスタンスを生成します
25     *
26     * @param \ReflectionAttribute<object>[] $attributeValueValidates
27     */
28    public function __construct(ArrayPath $arrayPath, array $attributeValueValidates)
29    {
30        $this->arrayPath               = $arrayPath;
31        $this->attributeValueValidates = $attributeValueValidates;
32    }
33
34    /**
35     * 値の検証を実行します
36     *
37     * @param string             $key   検証する値を保持しているキー名を渡します
38     * @param array<mixed,mixed> $input 検証する値を渡します
39     */
40    public function execute(string $key, array $input): ValidatorResult
41    {
42        $this->arrayPath->setCurrentPath($key);
43        $arrayPathStr   = ArrayPath::toString($this->arrayPath->getPaths());
44        $validateResult = new ValidateResult();
45        $validateResult->setArrayPath($arrayPathStr);
46
47        foreach ($this->attributeValueValidates as $attributeValueValidate) {
48            $valueValidateInstance = $attributeValueValidate->newInstance();
49            $validateResult        = $valueValidateInstance->execute($input[$key], $validateResult);
50
51            if (!$validateResult->getResult()) {
52                return new ValidatorResult($validateResult);
53            }
54        }
55        return new ValidatorResult();
56    }
57}