Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
10 / 10 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
Hash | |
100.00% |
10 / 10 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
close | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
exists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
firstkey | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
nextkey | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | */ |
18 | |
19 | namespace Cdb\Reader; |
20 | |
21 | use Cdb\Reader; |
22 | |
23 | /** |
24 | * Hash implements the CdbReader interface based on an associative |
25 | * PHP array (a.k.a "hash"). |
26 | */ |
27 | class Hash extends Reader { |
28 | /** @var string[] */ |
29 | private $data; |
30 | |
31 | /** |
32 | * A queue of keys to return from nextkey(), initialized by firstkey(); |
33 | * |
34 | * @var string[]|null |
35 | */ |
36 | private $keys = null; |
37 | |
38 | /** |
39 | * Create the object and open the file |
40 | * |
41 | * @param string[] $data An associative array |
42 | */ |
43 | public function __construct( array $data ) { |
44 | $this->data = $data; |
45 | } |
46 | |
47 | /** |
48 | * Close the file. Optional, you can just let the variable go out of scope. |
49 | */ |
50 | public function close(): void { |
51 | $this->data = []; |
52 | $this->keys = null; |
53 | } |
54 | |
55 | /** |
56 | * Get a value with a given key. Only string values are supported. |
57 | * |
58 | * @param string|int $key |
59 | * @return string|false The value associated with $key, or false if $key is not known. |
60 | */ |
61 | public function get( $key ) { |
62 | return $this->data[ $key ] ?? false; |
63 | } |
64 | |
65 | /** |
66 | * Check whether key exists |
67 | * |
68 | * @param string|int $key |
69 | * @return bool |
70 | */ |
71 | public function exists( $key ): bool { |
72 | return isset( $this->data[ $key ] ); |
73 | } |
74 | |
75 | /** |
76 | * Fetch first key |
77 | * |
78 | * @return string|false |
79 | */ |
80 | public function firstkey() { |
81 | $this->keys = array_keys( $this->data ); |
82 | return $this->nextkey(); |
83 | } |
84 | |
85 | /** |
86 | * Fetch next key |
87 | * |
88 | * @return string|false |
89 | */ |
90 | public function nextkey() { |
91 | if ( $this->keys === null ) { |
92 | return $this->firstkey(); |
93 | } |
94 | |
95 | return $this->keys ? array_shift( $this->keys ) : false; |
96 | } |
97 | |
98 | } |