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