|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\DataSet; |
|
6
|
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
|
8
|
|
|
use Yiisoft\Validator\ObjectDataSetInterface; |
|
9
|
|
|
use Yiisoft\Validator\RulesProviderInterface; |
|
10
|
|
|
|
|
11
|
|
|
use function get_class; |
|
12
|
|
|
|
|
13
|
|
|
final class CacheObjectDataSetDecorator implements ObjectDataSetInterface, RulesProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private string $cacheKey; |
|
16
|
|
|
|
|
17
|
|
|
#[ArrayShape([ |
|
18
|
|
|
[ |
|
19
|
|
|
'rules' => 'iterable', |
|
20
|
|
|
'reflectionProperties' => 'array', |
|
21
|
|
|
'data' => 'array', |
|
22
|
|
|
], |
|
23
|
|
|
])] |
|
24
|
|
|
private static array $cache = []; |
|
25
|
|
|
|
|
26
|
5 |
|
public function __construct(private ObjectDataSetInterface|RulesProviderInterface $decorated) |
|
27
|
|
|
{ |
|
28
|
5 |
|
$this->cacheKey = get_class($this->decorated->getObject()); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
5 |
|
$this->deleteCacheItem('data'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
11 |
|
public function getAttributeValue(string $attribute): mixed |
|
34
|
|
|
{ |
|
35
|
11 |
|
return $this->decorated->getAttributeValue($attribute); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
12 |
|
public function getData(): mixed |
|
39
|
|
|
{ |
|
40
|
12 |
|
if ($this->hasCacheItem('data')) { |
|
41
|
9 |
|
return $this->getCacheItem('data'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
9 |
|
$data = $this->decorated->getData(); |
|
|
|
|
|
|
45
|
9 |
|
$this->updateCacheItem('data', $data); |
|
46
|
|
|
|
|
47
|
9 |
|
return $data; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function hasAttribute(string $attribute): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->decorated->hasAttribute($attribute); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
12 |
|
public function getRules(): iterable |
|
56
|
|
|
{ |
|
57
|
|
|
if ( |
|
58
|
12 |
|
$this->hasCacheItem('rules') && |
|
59
|
12 |
|
$this->hasCacheItem('propertyVisibility') && |
|
60
|
12 |
|
$this->decorated->getPropertyVisibility() === $this->getCacheItem('propertyVisibility') |
|
|
|
|
|
|
61
|
|
|
) { |
|
62
|
9 |
|
return $this->getCacheItem('rules'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
9 |
|
$rules = $this->decorated->getRules(); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
9 |
|
$this->updateCacheItem('rules', $rules); |
|
68
|
9 |
|
$this->updateCacheItem('propertyVisibility', $this->getPropertyVisibility()); |
|
69
|
|
|
|
|
70
|
9 |
|
return $rules; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getReflectionProperties(): array |
|
74
|
|
|
{ |
|
75
|
|
|
if ( |
|
76
|
|
|
$this->hasCacheItem('reflectionProperties') && |
|
77
|
|
|
$this->hasCacheItem('propertyVisibility') && |
|
78
|
|
|
$this->decorated->getPropertyVisibility() === $this->getCacheItem('propertyVisibility') |
|
79
|
|
|
) { |
|
80
|
|
|
return $this->getCacheItem('reflectionProperties'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$reflectionProperties = $this->decorated->getReflectionProperties(); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$this->updateCacheItem('reflectionProperties', $reflectionProperties); |
|
86
|
|
|
$this->updateCacheItem('propertyVisibility', $this->getPropertyVisibility()); |
|
87
|
|
|
|
|
88
|
|
|
return $reflectionProperties; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
public function getPropertyVisibility(): int |
|
92
|
|
|
{ |
|
93
|
9 |
|
return $this->decorated->getPropertyVisibility(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getObject(): object |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->decorated->getObject(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
12 |
|
private function hasCacheItem(string $key): bool |
|
102
|
|
|
{ |
|
103
|
12 |
|
return isset(self::$cache[$this->cacheKey][$key]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
9 |
|
#[ArrayShape([ |
|
107
|
|
|
'rules' => 'iterable', |
|
108
|
|
|
'reflectionProperties' => 'array', |
|
109
|
|
|
'data' => 'array', |
|
110
|
|
|
])] |
|
111
|
|
|
private function getCacheItem(string $key): mixed |
|
112
|
|
|
{ |
|
113
|
9 |
|
return self::$cache[$this->cacheKey][$key]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
9 |
|
private function updateCacheItem(string $key, mixed $value): void |
|
117
|
|
|
{ |
|
118
|
9 |
|
self::$cache[$this->cacheKey][$key] = $value; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
5 |
|
private function deleteCacheItem(string $key): void |
|
122
|
|
|
{ |
|
123
|
5 |
|
unset(self::$cache[$this->cacheKey][$key]); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|