Passed
Pull Request — master (#373)
by Sergei
03:44 queued 51s
created

ObjectDataSet::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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 52
    public function __construct(
25
        private object $object,
26
        private int $propertyVisibility = ReflectionProperty::IS_PRIVATE |
27
        ReflectionProperty::IS_PROTECTED |
28
        ReflectionProperty::IS_PUBLIC,
29
        bool $useCache = true
30
    ) {
31 52
        $this->dataSetProvided = $this->object instanceof DataSetInterface;
32 52
        $this->rulesProvided = $this->object instanceof RulesProviderInterface;
33 52
        $this->parser = new ObjectParser(
34
            object: $object,
35
            propertyVisibility: $propertyVisibility,
36
            useCache: $useCache
37
        );
38
    }
39
40 54
    public function getRules(): iterable
41
    {
42 54
        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 41
        if ($this->dataSetProvided) {
52 5
            return [];
53
        }
54
55 36
        return $this->parser->getRules();
56
    }
57
58 46
    public function getAttributeValue(string $attribute): mixed
59
    {
60 46
        if ($this->dataSetProvided) {
61
            /** @var DataSetInterface $object */
62 8
            $object = $this->object;
63 8
            return $object->getAttributeValue($attribute);
64
        }
65
66 38
        return $this->parser->getAttributeValue($attribute);
67
    }
68
69 29
    public function hasAttribute(string $attribute): bool
70
    {
71 29
        if ($this->dataSetProvided) {
72
            /** @var DataSetInterface $object */
73 1
            $object = $this->object;
74 1
            return $object->hasAttribute($attribute);
75
        }
76
77 28
        return $this->parser->hasAttribute($attribute);
78
    }
79
80 34
    public function getData(): mixed
81
    {
82 34
        if ($this->dataSetProvided) {
83
            /** @var DataSetInterface $object */
84 13
            $object = $this->object;
85 13
            return $object->getData();
86
        }
87
88 21
        return $this->parser->getData();
89
    }
90
}
91