@@ -32,23 +32,23 @@ public function getAllAndClear(): array
3232 return $ data ;
3333 }
3434
35- public function get ($ key )
35+ public function get (string $ key, $ default = null )
3636 {
3737 $ keys = explode ('. ' , $ key );
3838
3939 $ resource = $ this ->getAll ();
4040
4141 foreach ($ keys as $ key ) {
4242 if (!isset ($ resource [$ key ])) {
43- return null ;
43+ return $ default ;
4444 }
4545 $ resource = &$ resource [$ key ];
4646 }
4747
4848 return $ resource ;
4949 }
5050
51- public function getAndRemove ($ key )
51+ public function getAndRemove (string $ key )
5252 {
5353 $ data = $ this ->get ($ key );
5454 $ this ->remove ($ key );
@@ -100,13 +100,6 @@ public function setAll(array $values): void
100100
101101 public function remove (string $ key ): void
102102 {
103- if (!$ this ->has ($ key )) {
104- throw new Exception (sprintf (
105- "Cannot remove non-existing value by key '%s' " ,
106- $ key
107- ));
108- }
109-
110103 $ keys = explode ('. ' , $ key );
111104
112105 $ data = $ this ->getAll ();
@@ -115,7 +108,10 @@ public function remove(string $key): void
115108
116109 foreach ($ keys as $ index => $ key ) {
117110 if (!isset ($ resource [$ key ])) {
118- return ;
111+ throw new Exception (sprintf (
112+ "Cannot remove non-existing value by key '%s' " ,
113+ $ key
114+ ));
119115 }
120116 if ($ index < (count ($ keys ) - 1 )) {
121117 $ resource = &$ resource [$ key ];
@@ -148,8 +144,14 @@ public function has(string $key): bool
148144 return true ;
149145 }
150146
151- public function count (): int
147+ public function count (string $ key = null ): int
152148 {
153- return count ($ this ->getAll ());
149+ if ($ key === null ) {
150+ $ data = $ this ->getAll ();
151+ } else {
152+ $ data = $ this ->get ($ key , []);
153+ }
154+
155+ return count ($ data );
154156 }
155157}
0 commit comments