Skip to content

Commit 984a2cd

Browse files
committed
Added The Storage class & configuration
1 parent f0f4b75 commit 984a2cd

5 files changed

Lines changed: 232 additions & 11 deletions

File tree

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Mem::del($key, $group = 'default'): bool
3838
Mem::all(): array
3939

4040
// Returns all keys and values of a group.
41-
Mem::group($group = 'default'): array
41+
Mem::group($group = 'default'): Storage
4242

4343
// Returns the number of existing groups.
4444
Mem::groupsCount(): int
@@ -53,6 +53,37 @@ Mem::reset(): bool
5353

5454
<hr>
5555

56+
### Configuration
57+
58+
The `config` method is used for configuration, with the first argument being the group name & the second argument as an array to config that group.
59+
If the second argument is `false` or not set, it returns the current config.
60+
61+
The `configProp` method retrieves a key's value from the config, with the key and group name as parameters.
62+
63+
The default config value is in `DEFAULT_CONFIG` const.
64+
65+
Currently, there's only one configuration, `length_limit`, with a default value of `-1`, which defines the group size.
66+
When the item count exceeds this, the first item is removed.
67+
`-1` means unlimited, also, `0` is currently not useful & should not be used.
68+
69+
```php
70+
Mem::config('default', ['length_limit' => 3]);
71+
72+
Mem::set('item_1', 'value 1');
73+
Mem::set('item_2', 'value 2');
74+
Mem::set('item_3', 'value 3');
75+
Mem::set('item_4', 'value 4');
76+
77+
/*
78+
* Items in the default group:
79+
* [
80+
* 'item_2' => 'value 2',
81+
* 'item_3' => 'value 3',
82+
* 'item_4' => 'value 4'
83+
* ]
84+
*/
85+
```
86+
5687
## 📖 License
5788

5889
Copyright (c) Hadi Akbarzadeh

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"library", "helper", "support"
77
],
88
"type": "library",
9-
"version": "1.0.0",
109
"homepage": "https://github.com/nabeghe/mem-php",
1110
"license": "MIT",
1211
"autoload": {

src/Mem.php

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,102 @@
55
*/
66
class Mem implements MemInterface
77
{
8+
public const DEFAULT_CONFIG = [
9+
'length_limit' => -1,
10+
];
11+
812
/**
913
* Everything is stored here
10-
*
11-
* @var array
14+
* @var Storage[]
1215
*/
1316
protected static array $storage = [];
1417

18+
/**
19+
* @var array
20+
*/
21+
protected static array $configs = [];
22+
23+
/**
24+
* @param string $group
25+
* @param array|null|false $config
26+
* @return mixed|void|null
27+
*/
28+
public static function config($group = 'default', $config = false)
29+
{
30+
if ($config === false) {
31+
return isset(static::$configs[$group]) ? static::$configs[$group] : null;
32+
}
33+
34+
if ($config === null) {
35+
unset(static::$configs[$group]);
36+
} else {
37+
static::$configs[$group] = $config;
38+
}
39+
}
40+
41+
public static function configProp($name, $group = 'default')
42+
{
43+
return isset(static::$configs[$group][$name])
44+
? static::$configs[$group][$name]
45+
: (isset(static::DEFAULT_CONFIG[$name]) ? static::DEFAULT_CONFIG[$name] : null);
46+
}
47+
48+
49+
/**
50+
* @param string $key
51+
* @param string $group
52+
* @return bool
53+
*/
1554
public static function has($key, $group = 'default')
1655
{
1756
return static::hasGroup($group) && isset(static::$storage[$group][$key]);
1857
}
1958

20-
public static function hasGroup($group)
59+
/**
60+
* @param string $group
61+
* @return bool
62+
*/
63+
public static function hasGroup($group = 'default')
2164
{
2265
return isset(static::$storage[$group]);
2366
}
2467

68+
/**
69+
* @param string $key
70+
* @param string $group
71+
* @param mixed $default
72+
* @return mixed|null
73+
*/
2574
public static function get($key, $group = 'default', $default = null)
2675
{
2776
return static::has($key, $group) ? static::$storage[$group][$key] : $default;
2877
}
2978

79+
/**
80+
* @param string $key
81+
* @param mixed $value
82+
* @param string $group
83+
* @return void
84+
*/
3085
public static function set($key, $value, $group = 'default')
3186
{
3287
if (!static::hasGroup($group)) {
33-
static::$storage[$group] = [];
88+
static::$storage[$group] = new Storage();
3489
}
90+
3591
static::$storage[$group][$key] = $value;
92+
93+
$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());
96+
}
3697
}
3798

99+
/**
100+
* @param string $key
101+
* @param string $group
102+
* @return bool
103+
*/
38104
public static function del($key, $group = 'default')
39105
{
40106
if (static::has($key, $group)) {
@@ -45,21 +111,35 @@ public static function del($key, $group = 'default')
45111
return false;
46112
}
47113

114+
/**
115+
* @return Storage[]
116+
*/
48117
public static function all()
49118
{
50119
return static::$storage;
51120
}
52121

122+
/**
123+
* @param string $group
124+
* @return Storage|null
125+
*/
53126
public static function group($group = 'default')
54127
{
55128
return static::hasGroup($group) ? static::$storage[$group] : null;
56129
}
57130

131+
/**
132+
* @return int
133+
*/
58134
public static function groupsCount()
59135
{
60136
return count(array_keys(static::$storage));
61137
}
62138

139+
/**
140+
* @param $group
141+
* @return bool
142+
*/
63143
public static function drop($group = 'default')
64144
{
65145
if (static::hasGroup($group)) {
@@ -70,6 +150,9 @@ public static function drop($group = 'default')
70150
return false;
71151
}
72152

153+
/**
154+
* @return bool
155+
*/
73156
public static function reset()
74157
{
75158
if (static::groupsCount()) {

src/Storage.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php namespace Nabeghe\Mem;
2+
3+
class Storage implements \ArrayAccess
4+
{
5+
protected $count = 0;
6+
7+
public function __construct(protected array $data = [])
8+
{
9+
}
10+
11+
public function firstKey()
12+
{
13+
//reset($this->data);
14+
return key($this->data);
15+
}
16+
17+
public function has($key)
18+
{
19+
return isset($this->data[$key]);
20+
}
21+
22+
public function add($key, $value)
23+
{
24+
$this->data[$key] = $value;
25+
$this->count++;
26+
}
27+
28+
public function del($key)
29+
{
30+
if (isset($this->data[$key])) {
31+
unset($this->data[$key]);
32+
}
33+
}
34+
35+
public function delValue($value)
36+
{
37+
if (($key = array_search($value, $this->data)) !== false) {
38+
unset($this->data[$key]);
39+
$this->count--;
40+
}
41+
}
42+
43+
public function count()
44+
{
45+
return $this->count;
46+
}
47+
48+
public function length()
49+
{
50+
return $this->count;
51+
}
52+
53+
public function getData()
54+
{
55+
return $this->data;
56+
}
57+
58+
public function setData($data)
59+
{
60+
$this->data = $data;
61+
$this->count = count($data);
62+
}
63+
64+
#[\ReturnTypeWillChange]
65+
public function offsetExists($offset)
66+
{
67+
return isset($this->data[$offset]);
68+
}
69+
70+
#[\ReturnTypeWillChange]
71+
public function offsetGet($offset)
72+
{
73+
return $this->data[$offset];
74+
}
75+
76+
#[\ReturnTypeWillChange]
77+
public function offsetSet($offset, $value)
78+
{
79+
$this->data[$offset] = $value;
80+
$this->count++;
81+
}
82+
83+
#[\ReturnTypeWillChange]
84+
public function offsetUnset($offset)
85+
{
86+
unset($this->data[$offset]);
87+
$this->count--;
88+
}
89+
}

tests/MemTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?php declare(strict_types=1);
22

33
use Nabeghe\Mem\Mem;
4+
use Nabeghe\Mem\Storage;
45

56
class MemTest extends \PHPUnit\Framework\TestCase
67
{
78
public function test()
89
{
10+
Mem::reset();
11+
912
// has (default group)
1013
$this->assertFalse(Mem::has('name'));
1114
$this->assertEquals(0, Mem::groupsCount());
@@ -35,12 +38,12 @@ public function test()
3538
$this->assertSame('my_value', Mem::get('my_key', 'my_group'));
3639

3740
$this->assertEquals([
38-
'default' => ['name' => 'nabeghe/mem'],
39-
'my_group' => ['my_key' => 'my_value'],
40-
], Mem::all());
41+
'default',
42+
'my_group',
43+
], array_keys(Mem::all()));
4144

42-
$this->assertEquals(['name' => 'nabeghe/mem'], Mem::group());
43-
$this->assertEquals(['my_key' => 'my_value'], Mem::group('my_group'));
45+
$this->assertEquals((new Storage(['name' => 'nabeghe/mem']))->getData(), Mem::group()->getData());
46+
$this->assertEquals((new Storage(['my_key' => 'my_value']))->getData(), Mem::group('my_group')->getData());
4447

4548
$this->assertEquals(2, Mem::groupsCount());
4649

@@ -68,4 +71,20 @@ public function test()
6871
Mem::reset();
6972
$this->assertEquals([], Mem::all());
7073
}
74+
75+
public function testLengthLimit()
76+
{
77+
Mem::reset();
78+
79+
Mem::config('default', ['length_limit' => 3]);
80+
81+
Mem::set('item_1', 'value 1');
82+
Mem::set('item_2', 'value 2');
83+
Mem::set('item_3', 'value 3');
84+
Mem::set('item_4', 'value 4');
85+
86+
$expected = ['item_2' => 'value 2', 'item_3' => 'value 3', 'item_4' => 'value 4'];
87+
88+
$this->assertEquals((new Storage($expected))->getData(), Mem::group()->getData());
89+
}
7190
}

0 commit comments

Comments
 (0)