|
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
|
59 |
|
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
|
59 |
|
$this->dataSetProvided = $this->object instanceof DataSetInterface; |
|
32
|
59 |
|
$this->rulesProvided = $this->object instanceof RulesProviderInterface; |
|
33
|
59 |
|
$this->parser = new ObjectParser( |
|
34
|
|
|
object: $object, |
|
35
|
|
|
propertyVisibility: $propertyVisibility, |
|
36
|
|
|
useCache: $useCache |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
55 |
|
public function getRules(): iterable |
|
41
|
|
|
{ |
|
42
|
55 |
|
if ($this->rulesProvided) { |
|
43
|
|
|
/** @var RulesProviderInterface $object */ |
|
44
|
11 |
|
$object = $this->object; |
|
45
|
|
|
|
|
46
|
11 |
|
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
|
44 |
|
if ($this->dataSetProvided) { |
|
52
|
5 |
|
return []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
39 |
|
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
|
36 |
|
public function getData(): mixed |
|
81
|
|
|
{ |
|
82
|
36 |
|
if ($this->dataSetProvided) { |
|
83
|
|
|
/** @var DataSetInterface $object */ |
|
84
|
13 |
|
$object = $this->object; |
|
85
|
13 |
|
return $object->getData(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
23 |
|
return $this->parser->getData(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|