Skip to content

Commit

Permalink
Added new methods: match, matches & delMatches
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeghe committed Jan 26, 2025
1 parent 984a2cd commit 2881d3a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 71 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ composer require nabeghe/mem
// Checks if a key exists in a cache group.
Mem::has(mixed $key, mixed $group = 'default'): bool

// Returns the first key of an item that matches the regex.
Mem::match($regex, $group = 'default'): ?string

// Returns items whose keys match the regex.
Mem::matches($regex, $group = 'default'): ?array

// Checks if a group exists.
Mem::hasGroup(mixed $group): bool

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

// Returns all groups and their keys.
// Deletes items based on key matching with regex.
Mem::delMatches($regex, $group = 'default'): bool

// Returns all storages (groups) and their keys.
Mem::all(): array

// Returns all keys and values of a group.
Expand Down
106 changes: 39 additions & 67 deletions src/Mem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ class Mem implements MemInterface
];

/**
* Everything is stored here
* @var Storage[]
*/
protected static array $storage = [];
protected static array $storages = [];

/**
* @var array
*/
protected static array $configs = [];

/**
* @param string $group
* @param array|null|false $config
* @return mixed|void|null
*/
public static function config($group = 'default', $config = false)
{
if ($config === false) {
Expand All @@ -36,6 +27,8 @@ public static function config($group = 'default', $config = false)
} else {
static::$configs[$group] = $config;
}

return null;
}

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


/**
* @param string $key
* @param string $group
* @return bool
*/
public static function has($key, $group = 'default')
{
return static::hasGroup($group) && isset(static::$storage[$group][$key]);
return static::hasGroup($group) && isset(static::$storages[$group][$key]);
}

public static function match($regex, $group = 'default')
{
if (!static::hasGroup($group)) {
return null;
}

return static::$storages[$group]->match($regex);
}

public static function matches($regex, $group = 'default')
{
if (!static::hasGroup($group)) {
return null;
}

return static::$storages[$group]->matches($regex);
}

/**
* @param string $group
* @return bool
*/
public static function hasGroup($group = 'default')
{
return isset(static::$storage[$group]);
return isset(static::$storages[$group]);
}

/**
* @param string $key
* @param string $group
* @param mixed $default
* @return mixed|null
*/
public static function get($key, $group = 'default', $default = null)
{
return static::has($key, $group) ? static::$storage[$group][$key] : $default;
return static::has($key, $group) ? static::$storages[$group][$key] : $default;
}

/**
* @param string $key
* @param mixed $value
* @param string $group
* @return void
*/
public static function set($key, $value, $group = 'default')
{
if (!static::hasGroup($group)) {
static::$storage[$group] = new Storage();
static::$storages[$group] = new Storage();
}

static::$storage[$group][$key] = $value;
static::$storages[$group][$key] = $value;

$length_limit = static::configProp('length_limit', $group);
if ($length_limit > 0 && static::$storage[$group]->count() > $length_limit) {
static::$storage[$group]->del(static::$storage[$group]->firstKey());
if ($length_limit > 0 && static::$storages[$group]->count() > $length_limit) {
static::$storages[$group]->del(static::$storages[$group]->firstKey());
}
}

/**
* @param string $key
* @param string $group
* @return bool
*/
public static function del($key, $group = 'default')
{
if (static::has($key, $group)) {
unset(static::$storage[$group][$key]);
unset(static::$storages[$group][$key]);
return true;
}

return false;
}

/**
* @return Storage[]
*/
public static function delMatches($regex, $group = 'default')
{
return static::hasGroup($group) && static::$storages[$group]->delMatches($regex);
}

public static function all()
{
return static::$storage;
return static::$storages;
}

/**
* @param string $group
* @return Storage|null
*/
public static function group($group = 'default')
{
return static::hasGroup($group) ? static::$storage[$group] : null;
return static::hasGroup($group) ? static::$storages[$group] : null;
}

/**
* @return int
*/
public static function groupsCount()
{
return count(array_keys(static::$storage));
return count(array_keys(static::$storages));
}

/**
* @param $group
* @return bool
*/
public static function drop($group = 'default')
{
if (static::hasGroup($group)) {
unset(static::$storage[$group]);
unset(static::$storages[$group]);
return true;
}

return false;
}

/**
* @return bool
*/
public static function reset()
{
if (static::groupsCount()) {
static::$storage = [];
static::$storages = [];
return true;
}

Expand Down
31 changes: 29 additions & 2 deletions src/MemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ interface MemInterface
*/
public static function has($key, $group = 'default');

/**
* Returns the first key of an item that matches the regex.
*
* @param string $regex
* @param string $group
* @return ?string
*/
public static function match($regex, $group = 'default');

/**
* Returns items whose keys match the regex.
*
* @param string $regex
* @param string $group
* @return ?array
*/
public static function matches($regex, $group = 'default');

/**
* Checks if a group exists.
*
Expand Down Expand Up @@ -49,9 +67,18 @@ public static function set($key, $value, $group = 'default');
public static function del($key, $group = 'default');

/**
* Returns all groups and their keys.
* Deletes items based on key matching with regex.
*
* @param string $regex
* @param string $group
* @return bool
*/
public static function delMatches($regex, $group = 'default');

/**
* Returns all storages (groups) and their keys.
*
* @return array
* @return Storage[]
*/
public static function all();

Expand Down
39 changes: 38 additions & 1 deletion src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,54 @@ public function has($key)
return isset($this->data[$key]);
}

public function match($regex)
{
foreach ($this->data as $key => $value) {
if (preg_match($regex, $key)) {
return $key;
}
}

return null;
}

public function matches($regex)
{
$matches = array_filter($this->data, function ($key) use ($regex) {
return preg_match($regex, $key);
}, ARRAY_FILTER_USE_KEY);

return $matches ?: null;
}

public function add($key, $value)
{
$this->data[$key] = $value;
$this->count++;
}

public function del($key)
public function del($key, $isRegex = false)
{
if (isset($this->data[$key])) {
unset($this->data[$key]);
return true;
}

return false;
}

public function delMatches($regex)
{
$success = false;

foreach ($this->data as $key => $value) {
if (preg_match($regex, $key)) {
unset($this->data[$key]);
$success = true;
}
}

return $success;
}

public function delValue($value)
Expand Down
38 changes: 38 additions & 0 deletions tests/MemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class MemTest extends \PHPUnit\Framework\TestCase
public function test()
{
Mem::reset();
Mem::config('default', ['length_limit' => -1]);

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

$this->assertEquals((new Storage($expected))->getData(), Mem::group()->getData());
}

public function testMatches()
{
Mem::reset();
Mem::config('default', ['length_limit' => -1]);

Mem::set('nabeghe_1', 'nabeghe value 1');
Mem::set('nabeghe_2', 'nabeghe value 2');
Mem::set('mem_1', 'mem value 1');
Mem::set('mem_2', 'mem value 2');

$this->assertSame('nabeghe_1', Mem::match('/^nabeghe_.*/'));

$this->assertSame([
'nabeghe_1' => 'nabeghe value 1',
'nabeghe_2' => 'nabeghe value 2',
], Mem::matches('/^nabeghe_.*/'));

$this->assertTrue(Mem::delMatches('/^nabeghe_.*/'));

$this->assertNull(Mem::match('/^nabeghe_.*/'));

$this->assertNull(Mem::matches('/^nabeghe_.*/'));

Mem::set('nabeghe_1', 'nabeghe value 1');
Mem::set('nabeghe_2', 'nabeghe value 2');

$this->assertSame([
'nabeghe_1' => 'nabeghe value 1',
'nabeghe_2' => 'nabeghe value 2',
], Mem::matches('/^nabeghe_.*/'));

$this->assertSame([
'mem_1' => 'mem value 1',
'mem_2' => 'mem value 2',
], Mem::matches('/^mem_.*/'));
}
}

0 comments on commit 2881d3a

Please sign in to comment.