Passed
Pull Request — master (#373)
by Sergei
12:00
created

ObjectDataSet::getRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\DataSet;
6
7
use ReflectionProperty;
8
use Yiisoft\Validator\DataSetInterface;
9
use Yiisoft\Validator\Helper\ObjectParser;
10
use Yiisoft\Validator\RulesProviderInterface;
11
12
/**
13
 * This data set makes use of attributes introduced in PHP 8. It simplifies rules configuration process, especially for
14
 * nested data and relations. Please refer to the guide for examples.
15
 *
16
 * @link https://www.php.net/manual/en/language.attributes.overview.php
17
 */
18
final class ObjectDataSet implements RulesProviderInterface, DataSetInterface
19
{
20
    private bool $dataSetProvided;
21
    private bool $rulesProvided;
22
    private ObjectParser $parser;
23
24 58
    public function __construct(
25
        private object $object,
26
        int $propertyVisibility = ReflectionProperty::IS_PRIVATE |
27
        ReflectionProperty::IS_PROTECTED |
28
        ReflectionProperty::IS_PUBLIC,
29
        bool $useCache = true
30
    ) {
31 58
        $this->dataSetProvided = $this->object instanceof DataSetInterface;
32 58
        $this->rulesProvided = $this->object instanceof RulesProviderInterface;
33 58
        $this->parser = new ObjectParser(
34
            object: $object,
35
            propertyVisibility: $propertyVisibility,
36
            useCache: $useCache
37
        );
38
    }
39
40 56
    public function getRules(): iterable
41
    {
42 56
        if ($this->rulesProvided) {
43
            /** @var RulesProviderInterface $object */
44 13
            $object = $this->object;
45
46 13
            return $object->getRules();
47
        }
48
49
        // Providing data set assumes object has its own attributes and rules getting logic. So further parsing of
50
        // Reflection properties and rules is skipped intentionally.
51 43
        if ($this->dataSetProvided) {
52 5
            return [];
53
        }
54
55 38
        return $this->parser->getRules();
56
    }
57
58 48
    public function getAttributeValue(string $attribute): mixed
59
    {
60 48
        if ($this->dataSetProvided) {
61
            /** @var DataSetInterface $object */
62 8
            $object = $this->object;
63 8
            return $object->getAttributeValue($attribute);
64
        }
65
66 40
        return $this->parser->getAttributeValue($attribute);
67
    }
68
69 31
    public function hasAttribute(string $attribute): bool
70
    {
71 31
        if ($this->dataSetProvided) {
72
            /** @var DataSetInterface $object */
73 1
            $object = $this->object;
74 1
            return $object->hasAttribute($attribute);
75
        }
76
77 30
        return $this->parser->hasAttribute($attribute);
78
    }
79
80 38
    public function getData(): mixed
81
    {
82 38
        if ($this->dataSetProvided) {
83
            /** @var DataSetInterface $object */
84 15
            $object = $this->object;
85 15
            return $object->getData();
86
        }
87
88 23
        return $this->parser->getData();
89
    }
90
}
91