Skip to content

Commit 2881d3a

Browse files
committed
Added new methods: match, matches & delMatches
1 parent 984a2cd commit 2881d3a

File tree

5 files changed

+154
-71
lines changed

5 files changed

+154
-71
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ composer require nabeghe/mem
2222
// Checks if a key exists in a cache group.
2323
Mem::has(mixed $key, mixed $group = 'default'): bool
2424

25+
// Returns the first key of an item that matches the regex.
26+
Mem::match($regex, $group = 'default'): ?string
27+
28+
// Returns items whose keys match the regex.
29+
Mem::matches($regex, $group = 'default'): ?array
30+
2531
// Checks if a group exists.
2632
Mem::hasGroup(mixed $group): bool
2733

@@ -34,7 +40,10 @@ Mem::set(mixed $key, mixed $value, mixed $group = 'default'): bool
3440
// Deletes a key from a group.
3541
Mem::del($key, $group = 'default'): bool
3642

37-
// Returns all groups and their keys.
43+
// Deletes items based on key matching with regex.
44+
Mem::delMatches($regex, $group = 'default'): bool
45+
46+
// Returns all storages (groups) and their keys.
3847
Mem::all(): array
3948

4049
// Returns all keys and values of a group.

src/Mem.php

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,12 @@ class Mem implements MemInterface
1010
];
1111

1212
/**
13-
* Everything is stored here
1413
* @var Storage[]
1514
*/
16-
protected static array $storage = [];
15+
protected static array $storages = [];
1716

18-
/**
19-
* @var array
20-
*/
2117
protected static array $configs = [];
2218

23-
/**
24-
* @param string $group
25-
* @param array|null|false $config
26-
* @return mixed|void|null
27-
*/
2819
public static function config($group = 'default', $config = false)
2920
{
3021
if ($config === false) {
@@ -36,6 +27,8 @@ public static function config($group = 'default', $config = false)
3627
} else {
3728
static::$configs[$group] = $config;
3829
}
30+
31+
return null;
3932
}
4033

4134
public static function configProp($name, $group = 'default')
@@ -45,118 +38,97 @@ public static function configProp($name, $group = 'default')
4538
: (isset(static::DEFAULT_CONFIG[$name]) ? static::DEFAULT_CONFIG[$name] : null);
4639
}
4740

48-
49-
/**
50-
* @param string $key
51-
* @param string $group
52-
* @return bool
53-
*/
5441
public static function has($key, $group = 'default')
5542
{
56-
return static::hasGroup($group) && isset(static::$storage[$group][$key]);
43+
return static::hasGroup($group) && isset(static::$storages[$group][$key]);
44+
}
45+
46+
public static function match($regex, $group = 'default')
47+
{
48+
if (!static::hasGroup($group)) {
49+
return null;
50+
}
51+
52+
return static::$storages[$group]->match($regex);
53+
}
54+
55+
public static function matches($regex, $group = 'default')
56+
{
57+
if (!static::hasGroup($group)) {
58+
return null;
59+
}
60+
61+
return static::$storages[$group]->matches($regex);
5762
}
5863

59-
/**
60-
* @param string $group
61-
* @return bool
62-
*/
6364
public static function hasGroup($group = 'default')
6465
{
65-
return isset(static::$storage[$group]);
66+
return isset(static::$storages[$group]);
6667
}
6768

68-
/**
69-
* @param string $key
70-
* @param string $group
71-
* @param mixed $default
72-
* @return mixed|null
73-
*/
7469
public static function get($key, $group = 'default', $default = null)
7570
{
76-
return static::has($key, $group) ? static::$storage[$group][$key] : $default;
71+
return static::has($key, $group) ? static::$storages[$group][$key] : $default;
7772
}
7873

79-
/**
80-
* @param string $key
81-
* @param mixed $value
82-
* @param string $group
83-
* @return void
84-
*/
8574
public static function set($key, $value, $group = 'default')
8675
{
8776
if (!static::hasGroup($group)) {
88-
static::$storage[$group] = new Storage();
77+
static::$storages[$group] = new Storage();
8978
}
9079

91-
static::$storage[$group][$key] = $value;
80+
static::$storages[$group][$key] = $value;
9281

9382
$length_limit = static::configProp('length_limit', $group);
94-
if ($length_limit > 0 && static::$storage[$group]->count() > $length_limit) {
95-
static::$storage[$group]->del(static::$storage[$group]->firstKey());
83+
if ($length_limit > 0 && static::$storages[$group]->count() > $length_limit) {
84+
static::$storages[$group]->del(static::$storages[$group]->firstKey());
9685
}
9786
}
9887

99-
/**
100-
* @param string $key
101-
* @param string $group
102-
* @return bool
103-
*/
10488
public static function del($key, $group = 'default')
10589
{
10690
if (static::has($key, $group)) {
107-
unset(static::$storage[$group][$key]);
91+
unset(static::$storages[$group][$key]);
10892
return true;
10993
}
11094

11195
return false;
11296
}
11397

114-
/**
115-
* @return Storage[]
116-
*/
98+
public static function delMatches($regex, $group = 'default')
99+
{
100+
return static::hasGroup($group) && static::$storages[$group]->delMatches($regex);
101+
}
102+
117103
public static function all()
118104
{
119-
return static::$storage;
105+
return static::$storages;
120106
}
121107

122-
/**
123-
* @param string $group
124-
* @return Storage|null
125-
*/
126108
public static function group($group = 'default')
127109
{
128-
return static::hasGroup($group) ? static::$storage[$group] : null;
110+
return static::hasGroup($group) ? static::$storages[$group] : null;
129111
}
130112

131-
/**
132-
* @return int
133-
*/
134113
public static function groupsCount()
135114
{
136-
return count(array_keys(static::$storage));
115+
return count(array_keys(static::$storages));
137116
}
138117

139-
/**
140-
* @param $group
141-
* @return bool
142-
*/
143118
public static function drop($group = 'default')
144119
{
145120
if (static::hasGroup($group)) {
146-
unset(static::$storage[$group]);
121+
unset(static::$storages[$group]);
147122
return true;
148123
}
149124

150125
return false;
151126
}
152127

153-
/**
154-
* @return bool
155-
*/
156128
public static function reset()
157129
{
158130
if (static::groupsCount()) {
159-
static::$storage = [];
131+
static::$storages = [];
160132
return true;
161133
}
162134

src/MemInterface.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ interface MemInterface
1111
*/
1212
public static function has($key, $group = 'default');
1313

14+
/**
15+
* Returns the first key of an item that matches the regex.
16+
*
17+
* @param string $regex
18+
* @param string $group
19+
* @return ?string
20+
*/
21+
public static function match($regex, $group = 'default');
22+
23+
/**
24+
* Returns items whose keys match the regex.
25+
*
26+
* @param string $regex
27+
* @param string $group
28+
* @return ?array
29+
*/
30+
public static function matches($regex, $group = 'default');
31+
1432
/**
1533
* Checks if a group exists.
1634
*
@@ -49,9 +67,18 @@ public static function set($key, $value, $group = 'default');
4967
public static function del($key, $group = 'default');
5068

5169
/**
52-
* Returns all groups and their keys.
70+
* Deletes items based on key matching with regex.
71+
*
72+
* @param string $regex
73+
* @param string $group
74+
* @return bool
75+
*/
76+
public static function delMatches($regex, $group = 'default');
77+
78+
/**
79+
* Returns all storages (groups) and their keys.
5380
*
54-
* @return array
81+
* @return Storage[]
5582
*/
5683
public static function all();
5784

src/Storage.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,54 @@ public function has($key)
1919
return isset($this->data[$key]);
2020
}
2121

22+
public function match($regex)
23+
{
24+
foreach ($this->data as $key => $value) {
25+
if (preg_match($regex, $key)) {
26+
return $key;
27+
}
28+
}
29+
30+
return null;
31+
}
32+
33+
public function matches($regex)
34+
{
35+
$matches = array_filter($this->data, function ($key) use ($regex) {
36+
return preg_match($regex, $key);
37+
}, ARRAY_FILTER_USE_KEY);
38+
39+
return $matches ?: null;
40+
}
41+
2242
public function add($key, $value)
2343
{
2444
$this->data[$key] = $value;
2545
$this->count++;
2646
}
2747

28-
public function del($key)
48+
public function del($key, $isRegex = false)
2949
{
3050
if (isset($this->data[$key])) {
3151
unset($this->data[$key]);
52+
return true;
53+
}
54+
55+
return false;
56+
}
57+
58+
public function delMatches($regex)
59+
{
60+
$success = false;
61+
62+
foreach ($this->data as $key => $value) {
63+
if (preg_match($regex, $key)) {
64+
unset($this->data[$key]);
65+
$success = true;
66+
}
3267
}
68+
69+
return $success;
3370
}
3471

3572
public function delValue($value)

tests/MemTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class MemTest extends \PHPUnit\Framework\TestCase
88
public function test()
99
{
1010
Mem::reset();
11+
Mem::config('default', ['length_limit' => -1]);
1112

1213
// has (default group)
1314
$this->assertFalse(Mem::has('name'));
@@ -87,4 +88,41 @@ public function testLengthLimit()
8788

8889
$this->assertEquals((new Storage($expected))->getData(), Mem::group()->getData());
8990
}
91+
92+
public function testMatches()
93+
{
94+
Mem::reset();
95+
Mem::config('default', ['length_limit' => -1]);
96+
97+
Mem::set('nabeghe_1', 'nabeghe value 1');
98+
Mem::set('nabeghe_2', 'nabeghe value 2');
99+
Mem::set('mem_1', 'mem value 1');
100+
Mem::set('mem_2', 'mem value 2');
101+
102+
$this->assertSame('nabeghe_1', Mem::match('/^nabeghe_.*/'));
103+
104+
$this->assertSame([
105+
'nabeghe_1' => 'nabeghe value 1',
106+
'nabeghe_2' => 'nabeghe value 2',
107+
], Mem::matches('/^nabeghe_.*/'));
108+
109+
$this->assertTrue(Mem::delMatches('/^nabeghe_.*/'));
110+
111+
$this->assertNull(Mem::match('/^nabeghe_.*/'));
112+
113+
$this->assertNull(Mem::matches('/^nabeghe_.*/'));
114+
115+
Mem::set('nabeghe_1', 'nabeghe value 1');
116+
Mem::set('nabeghe_2', 'nabeghe value 2');
117+
118+
$this->assertSame([
119+
'nabeghe_1' => 'nabeghe value 1',
120+
'nabeghe_2' => 'nabeghe value 2',
121+
], Mem::matches('/^nabeghe_.*/'));
122+
123+
$this->assertSame([
124+
'mem_1' => 'mem value 1',
125+
'mem_2' => 'mem value 2',
126+
], Mem::matches('/^mem_.*/'));
127+
}
90128
}

0 commit comments

Comments
 (0)