|
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
|
|
|
'propertyVisibility' => 'int', |
|
21
|
|
|
], |
|
22
|
|
|
])] |
|
23
|
|
|
private static array $cache = []; |
|
24
|
|
|
|
|
25
|
5 |
|
public function __construct(private ObjectDataSetInterface|RulesProviderInterface $decorated) |
|
26
|
|
|
{ |
|
27
|
5 |
|
$this->cacheKey = get_class($this->decorated->getObject()); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
11 |
|
public function getAttributeValue(string $attribute): mixed |
|
31
|
|
|
{ |
|
32
|
11 |
|
return $this->decorated->getAttributeValue($attribute); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
12 |
|
public function getData(): mixed |
|
36
|
|
|
{ |
|
37
|
12 |
|
return $this->decorated->getData(); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function hasAttribute(string $attribute): bool |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->decorated->hasAttribute($attribute); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
12 |
|
public function getRules(): iterable |
|
46
|
|
|
{ |
|
47
|
|
|
if ( |
|
48
|
12 |
|
$this->hasCacheItem('rules') && |
|
49
|
12 |
|
$this->hasCacheItem('propertyVisibility') && |
|
50
|
12 |
|
$this->decorated->getPropertyVisibility() === $this->getCacheItem('propertyVisibility') |
|
|
|
|
|
|
51
|
|
|
) { |
|
52
|
9 |
|
return $this->getCacheItem('rules'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
9 |
|
$rules = $this->decorated->getRules(); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
9 |
|
$this->updateCacheItem('rules', $rules); |
|
58
|
9 |
|
$this->updateCacheItem('propertyVisibility', $this->getPropertyVisibility()); |
|
59
|
|
|
|
|
60
|
9 |
|
return $rules; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
9 |
|
public function getPropertyVisibility(): int |
|
64
|
|
|
{ |
|
65
|
9 |
|
return $this->decorated->getPropertyVisibility(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getObject(): object |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->decorated->getObject(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
12 |
|
private function hasCacheItem(string $key): bool |
|
74
|
|
|
{ |
|
75
|
12 |
|
return isset(self::$cache[$this->cacheKey][$key]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
9 |
|
#[ArrayShape([ |
|
79
|
|
|
'rules' => 'iterable', |
|
80
|
|
|
'propertyVisibility' => 'int', |
|
81
|
|
|
])] |
|
82
|
|
|
private function getCacheItem(string $key): mixed |
|
83
|
|
|
{ |
|
84
|
9 |
|
return self::$cache[$this->cacheKey][$key]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
9 |
|
private function updateCacheItem(string $key, mixed $value): void |
|
88
|
|
|
{ |
|
89
|
9 |
|
self::$cache[$this->cacheKey][$key] = $value; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|