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($object, $propertyVisibility, $useCache); |
34
|
|
|
} |
35
|
|
|
|
36
|
54 |
|
public function getRules(): iterable |
37
|
|
|
{ |
38
|
54 |
|
if ($this->rulesProvided) { |
39
|
|
|
/** @var RulesProviderInterface $object */ |
40
|
13 |
|
$object = $this->object; |
41
|
|
|
|
42
|
13 |
|
return $object->getRules(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Providing data set assumes object has its own attributes and rules getting logic. So further parsing of |
46
|
|
|
// Reflection properties and rules is skipped intentionally. |
47
|
41 |
|
if ($this->dataSetProvided) { |
48
|
5 |
|
return []; |
49
|
|
|
} |
50
|
|
|
|
51
|
36 |
|
return $this->parser->getRules(); |
52
|
|
|
} |
53
|
|
|
|
54
|
46 |
|
public function getAttributeValue(string $attribute): mixed |
55
|
|
|
{ |
56
|
46 |
|
if ($this->dataSetProvided) { |
57
|
|
|
/** @var DataSetInterface $object */ |
58
|
8 |
|
$object = $this->object; |
59
|
8 |
|
return $object->getAttributeValue($attribute); |
60
|
|
|
} |
61
|
|
|
|
62
|
38 |
|
return $this->parser->getAttributeValue($attribute); |
63
|
|
|
} |
64
|
|
|
|
65
|
29 |
|
public function hasAttribute(string $attribute): bool |
66
|
|
|
{ |
67
|
29 |
|
if ($this->dataSetProvided) { |
68
|
|
|
/** @var DataSetInterface $object */ |
69
|
1 |
|
$object = $this->object; |
70
|
1 |
|
return $object->hasAttribute($attribute); |
71
|
|
|
} |
72
|
|
|
|
73
|
28 |
|
return $this->parser->hasAttribute($attribute); |
74
|
|
|
} |
75
|
|
|
|
76
|
34 |
|
public function getData(): mixed |
77
|
|
|
{ |
78
|
34 |
|
if ($this->dataSetProvided) { |
79
|
|
|
/** @var DataSetInterface $object */ |
80
|
13 |
|
$object = $this->object; |
81
|
13 |
|
return $object->getData(); |
82
|
|
|
} |
83
|
|
|
|
84
|
21 |
|
return $this->parser->getData(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|