Passed
Pull Request — master (#323)
by Alexander
03:11
created

ObjectDataSet::getPropertyVisibility()   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
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\DataSet;
6
7
use ReflectionAttribute;
8
use ReflectionObject;
9
use ReflectionProperty;
10
use Yiisoft\Validator\AttributeEventInterface;
11
use Yiisoft\Validator\DataSetInterface;
12
use Yiisoft\Validator\ObjectDataSetInterface;
13
use Yiisoft\Validator\RuleInterface;
14
use Yiisoft\Validator\RulesProviderInterface;
15
16
use function array_key_exists;
17
18
/**
19
 * This data set makes use of attributes introduced in PHP 8. It simplifies rules configuration process, especially for
20
 * nested data and relations. Please refer to the guide for examples.
21
 *
22
 * @link https://www.php.net/manual/en/language.attributes.overview.php
23
 */
24
final class ObjectDataSet implements RulesProviderInterface, ObjectDataSetInterface
25
{
26
    private bool $dataSetProvided;
27
    /**
28
     * @var ReflectionProperty[]
29
     */
30
    private ?array $reflectionProperties = null;
31
    private ?iterable $rules = null;
32
33 39
    public function __construct(
34
        private object $object,
35
        private int $propertyVisibility = ReflectionProperty::IS_PRIVATE |
36
        ReflectionProperty::IS_PROTECTED |
37
        ReflectionProperty::IS_PUBLIC
38
    ) {
39 39
        $this->dataSetProvided = $this->object instanceof DataSetInterface;
40
    }
41
42 58
    public function getRules(): iterable
43
    {
44 58
        if ($this->rules !== null) {
45 9
            return $this->rules;
46
        }
47
48 55
        $objectHasRules = $this->object instanceof RulesProviderInterface;
49 55
        $rules = $objectHasRules ? $this->object->getRules() : [];
50
51
        // Providing data set assumes object has its own attributes and rules getting logic. So further parsing of
52
        // Reflection properties and rules is skipped intentionally.
53 55
        if ($this->dataSetProvided || $objectHasRules === true) {
54 19
            $this->rules = $rules;
55
56 19
            return $rules;
57
        }
58
59 36
        foreach ($this->getReflectionProperties() as $property) {
60 35
            $attributes = $property->getAttributes(RuleInterface::class, ReflectionAttribute::IS_INSTANCEOF);
61 35
            foreach ($attributes as $attribute) {
62 33
                $rule = $attribute->newInstance();
63 33
                $rules[$property->getName()][] = $rule;
64
65 33
                if ($rule instanceof AttributeEventInterface) {
66 4
                    $rule->afterInitAttribute($this);
67
                }
68
            }
69
        }
70
71 33
        $this->rules = $rules;
72
73 33
        return $rules;
74
    }
75
76
    /**
77
     * @return ReflectionProperty[] Used to avoid error "Typed property must not be accessed before initialization".
78
     */
79 49
    public function getReflectionProperties(): array
80
    {
81 49
        if ($this->reflectionProperties !== null) {
82 36
            return $this->reflectionProperties;
83
        }
84
85
        // Providing data set assumes object has its own attributes and rules getting logic. So further parsing of
86
        // Reflection properties and rules is skipped intentionally.
87 48
        if ($this->dataSetProvided) {
88
            $this->reflectionProperties = [];
89
90
            return $this->reflectionProperties;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->reflectionProperties returns the type null which is incompatible with the type-hinted return array.
Loading history...
91
        }
92
93
        // TODO: Use Generator to collect attributes
94
95 48
        $reflection = new ReflectionObject($this->object);
96 48
        $reflectionProperties = [];
97
98 48
        foreach ($reflection->getProperties($this->propertyVisibility) as $property) {
99 47
            if (PHP_VERSION_ID < 80100) {
100 47
                $property->setAccessible(true);
101
            }
102
103 47
            $reflectionProperties[$property->getName()] = $property;
104
        }
105
106 48
        $this->reflectionProperties = $reflectionProperties;
107
108 48
        return $reflectionProperties;
109
    }
110
111 12
    public function getPropertyVisibility(): int
112
    {
113 12
        return $this->propertyVisibility;
114
    }
115
116 9
    public function getObject(): object
117
    {
118 9
        return $this->object;
119
    }
120
121 48
    public function getAttributeValue(string $attribute): mixed
122
    {
123 48
        if ($this->dataSetProvided) {
124 12
            return $this->object->getAttributeValue($attribute);
125
        }
126
127 36
        return $this->getData()[$attribute] ?? null;
128
    }
129
130 24
    public function hasAttribute(string $attribute): bool
131
    {
132 24
        return $this->dataSetProvided
133
            ? $this->object->hasAttribute($attribute)
134 24
            : array_key_exists($attribute, $this->getReflectionProperties());
135
    }
136
137 50
    public function getData(): array
138
    {
139 50
        if ($this->dataSetProvided) {
140 13
            return $this->object->getData();
141
        }
142
143 37
        $data = [];
144 37
        foreach ($this->getReflectionProperties() as $name => $property) {
145 37
            $data[$name] = $property->getValue($this->object);
146
        }
147
148 37
        return $data;
149
    }
150
}
151