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