1+ <?php declare (strict_types=1 );
2+
3+ namespace ihipop \PsrNullCache \SimpleCache ;
4+
5+ use ihipop \PsrNullCache \Exception \InvalidArgumentException ;
6+
7+ trait NullCacheReactTrait
8+ {
9+ public static function typeString (mixed $ object ): string
10+ {
11+ return \is_object ($ object ) ? \get_class ($ object ) : \gettype ($ object );
12+ }
13+
14+ public static function validateKey (mixed $ key ): string
15+ {
16+ if (!\is_string ($ key )) {
17+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , self ::typeString ($ key )));
18+ }
19+ if ('' === $ key ) {
20+ throw new InvalidArgumentException ('Cache key length must be greater than zero ' );
21+ }
22+ if (false !== strpbrk ($ key , '{}()/\@: ' )) {
23+ throw new InvalidArgumentException (sprintf ('Cache key "%s" contains reserved characters {}()/\@: ' , $ key ));
24+ }
25+
26+ return $ key ;
27+ }
28+
29+ public static function traversToArray (mixed $ travers ): array
30+ {
31+ if ($ travers instanceof \Traversable) {
32+ $ travers = iterator_to_array ($ travers , false );
33+ }
34+ if (!\is_array ($ travers )) {
35+ throw new InvalidArgumentException (sprintf ('Cache keys must be array or Traversable, "%s" given ' ,
36+ self ::typeString ($ travers )));
37+ }
38+
39+ return $ travers ;
40+ }
41+
42+ /** @inheritdoc */
43+ public function get ($ key , $ default = null )
44+ {
45+ self ::validateKey ($ key );
46+
47+ return $ default ;
48+ }
49+
50+ /** @inheritdoc */
51+ public function set ($ key , $ value , $ ttl = null )
52+ {
53+ self ::validateKey ($ key );
54+
55+ return $ this ->defaultBool ;
56+ }
57+
58+ /** @inheritdoc */
59+ public function delete ($ key )
60+ {
61+ self ::validateKey ($ key );
62+
63+ return $ this ->defaultBool ;
64+ }
65+
66+ /** @inheritdoc */
67+ public function clear ()
68+ {
69+ return $ this ->defaultBool ;
70+ }
71+
72+ /** @inheritdoc */
73+ public function getMultiple (array $ keys , $ default = null )
74+ {
75+ $ keys = self ::traversToArray ($ keys );
76+ foreach ($ keys as $ value ) {
77+ self ::validateKey ($ value );
78+ }
79+ foreach ($ keys as $ key ) {
80+ yield $ key => $ default ;
81+ }
82+ }
83+
84+ /** @inheritdoc */
85+ public function setMultiple (array $ values , $ ttl = null )
86+ {
87+ $ KeyValues = self ::traversToArray ($ values );
88+
89+ foreach ($ KeyValues as $ key => $ __ ) {
90+ self ::validateKey ($ key );
91+ }
92+
93+ return $ this ->defaultBool ;
94+ }
95+
96+ /** @inheritdoc */
97+ public function deleteMultiple (array $ keys )
98+ {
99+ $ keys = self ::traversToArray ($ keys );
100+ foreach ($ keys as $ value ) {
101+ self ::validateKey ($ value );
102+ }
103+
104+ return $ this ->defaultBool ;
105+ }
106+
107+ /** @inheritdoc */
108+ public function has ($ key )
109+ {
110+ self ::validateKey ($ key );
111+
112+ return $ this ->defaultBool ;
113+ }
114+ }
0 commit comments