Skip to content

Commit a16f5d5

Browse files
author
Andrey Helldar
authored
Merge pull request #18 from TheDragonCode/2.x
Added the ability to pass an array object to `keys` and `tags` methods
2 parents 4639e1d + b1cdcf1 commit a16f5d5

File tree

10 files changed

+192
-36
lines changed

10 files changed

+192
-36
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,30 @@ Or manually update `require` block of `composer.json` and run `composer update`.
2727

2828
## Using
2929

30-
### Keys Handling
30+
### Keys And Tags
31+
32+
In addition to passing an explicit value, you can also pass objects and arrays to the `keys` and `tags` methods.
33+
34+
For example:
35+
36+
```php
37+
use DragonCode\Cache\Services\Cache;
38+
use DragonCode\SimpleDataTransferObject\DataTransferObject;
39+
40+
$arr1 = ['foo', 'bar']
41+
$arr2 = new ArrayObject(['foo', 'bar'])
42+
$arr3 = DataTransferObject::make(['foo', 'bar'])
43+
44+
Cache::make()->key($arr1)->tags($arr1);
45+
Cache::make()->key($arr2)->tags($arr3);
46+
Cache::make()->key($arr2)->tags($arr3);
47+
48+
Cache::make()->key([$arr1, $arr2, $arr3, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, 'foo', 'bar']);
49+
Cache::make()->key([$arr1, $arr2, $arr3, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, 'foo', 'bar']);
50+
Cache::make()->key([$arr1, $arr2, $arr3, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, 'foo', 'bar']);
51+
```
52+
53+
#### Keys Handling
3154

3255
Since the main problem of working with the cache's key compilation, this package solves it.
3356

@@ -36,7 +59,9 @@ By passing values to the `keys` method, we get a ready-made key at the output.
3659
For example:
3760

3861
```php
39-
$cache = Cache::make()->key('foo', 'bar', ['baz', 'baq']);
62+
use DragonCode\Cache\Services\Cache;
63+
64+
$cache = Cache::make()->key('foo', 'bar', [null, 'baz', 'baq']);
4065

4166
// Key is `acbd18db4cc2f85cedef654fccc4a4d8:37b51d194a7513e45b56f6524f2d51f2:73feffa4b7f6bb68e44cf984c85f6e88:b47951d522316fdd8811b23fc9c2f583`
4267
```
@@ -46,6 +71,8 @@ This means that when writing to the cache, the tree view will be used.
4671
For example:
4772

4873
```php
74+
use DragonCode\Cache\Services\Cache;
75+
4976
Cache::make()->key('foo', 'foo')->put('foo');
5077
Cache::make()->key('foo', 'bar')->put('bar');
5178
Cache::make()->key('baz')->put('baz');

src/Concerns/Arrayable.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,60 @@
44

55
namespace DragonCode\Cache\Concerns;
66

7+
use ArrayObject;
8+
use Closure;
9+
use DragonCode\Contracts\Support\Arrayable as DragonCodeArrayable;
710
use DragonCode\Support\Facades\Helpers\Ables\Arrayable as Helper;
11+
use DragonCode\Support\Facades\Helpers\Arr;
12+
use DragonCode\Support\Facades\Helpers\Instance;
13+
use DragonCode\Support\Helpers\Ables\Arrayable as ArrayableHelper;
14+
use Illuminate\Contracts\Support\Arrayable as IlluminateArrayable;
815

916
trait Arrayable
1017
{
1118
protected function arrayMap(array $values, callable $callback): array
1219
{
1320
return Helper::of($values)
21+
->map(function ($value) {
22+
if ($this->isArrayable($value)) {
23+
return Arr::toArray($value);
24+
}
25+
26+
return $value;
27+
})
1428
->flatten()
29+
->filter()
1530
->map($callback)
31+
->values()
1632
->get();
1733
}
34+
35+
protected function toArray($value): array
36+
{
37+
$value = $this->resolveArray($value);
38+
39+
return Arr::wrap($value);
40+
}
41+
42+
protected function resolveArray($value): array
43+
{
44+
if ($this->isArrayable($value)) {
45+
return Arr::toArray($value);
46+
}
47+
48+
return $value;
49+
}
50+
51+
protected function isArrayable($value): bool
52+
{
53+
if (Instance::of($value, [DragonCodeArrayable::class, IlluminateArrayable::class, ArrayableHelper::class, ArrayObject::class])) {
54+
return true;
55+
}
56+
57+
if (Instance::of($value, Closure::class) && method_exists($value, 'toArray')) {
58+
return true;
59+
}
60+
61+
return false;
62+
}
1863
}

src/Concerns/Call.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44

55
namespace DragonCode\Cache\Concerns;
66

7-
use ArrayObject;
8-
use Closure;
9-
use DragonCode\Contracts\Support\Arrayable as DragonCodeArrayable;
10-
use DragonCode\Support\Facades\Helpers\Arr;
11-
use DragonCode\Support\Facades\Helpers\Instance;
12-
use DragonCode\Support\Helpers\Ables\Arrayable as ArrayableHelper;
13-
use Illuminate\Contracts\Support\Arrayable as IlluminateArrayable;
14-
157
trait Call
168
{
9+
use Arrayable;
10+
1711
/**
1812
* @param mixed $callback
1913
*
@@ -42,30 +36,8 @@ protected function resolve($value)
4236
return $this->isArrayable($value) ? $this->resolveArray($value) : $value;
4337
}
4438

45-
protected function resolveArray($value): array
46-
{
47-
if ($this->isArrayable($value)) {
48-
return Arr::toArray($value);
49-
}
50-
51-
return $value;
52-
}
53-
5439
protected function isCallable($value): bool
5540
{
5641
return is_callable($value);
5742
}
58-
59-
protected function isArrayable($value): bool
60-
{
61-
if (Instance::of($value, [DragonCodeArrayable::class, IlluminateArrayable::class, ArrayableHelper::class, ArrayObject::class])) {
62-
return true;
63-
}
64-
65-
if (Instance::of($value, Closure::class) && method_exists($value, 'toArray')) {
66-
return true;
67-
}
68-
69-
return false;
70-
}
7143
}

src/Facades/Support/Key.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace DragonCode\Cache\Facades\Support;
66

77
use DragonCode\Cache\Support\Key as Support;
8+
use DragonCode\Contracts\DataTransferObject\DataTransferObject;
9+
use DragonCode\Contracts\Support\Arrayable;
810
use Illuminate\Support\Facades\Facade;
911

1012
/**
11-
* @method static string get(string $separator, array $values)
13+
* @method static string get(string $separator, array|Arrayable|\Illuminate\Contracts\Support\Arrayable|\ArrayObject|DataTransferObject $values)
1214
*/
1315
class Key extends Facade
1416
{

src/Facades/Support/Tag.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace DragonCode\Cache\Facades\Support;
66

77
use DragonCode\Cache\Support\Tag as Support;
8+
use DragonCode\Contracts\DataTransferObject\DataTransferObject;
9+
use DragonCode\Contracts\Support\Arrayable;
810
use Illuminate\Support\Facades\Facade;
911

1012
/**
11-
* @method static array get(array $tags)
13+
* @method static array get(array|Arrayable|\Illuminate\Contracts\Support\Arrayable|\ArrayObject|DataTransferObject $tags)
1214
*/
1315
class Tag extends Facade
1416
{

src/Support/Key.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
namespace DragonCode\Cache\Support;
66

77
use DragonCode\Cache\Concerns\Arrayable;
8+
use DragonCode\Contracts\DataTransferObject\DataTransferObject;
89

910
class Key
1011
{
1112
use Arrayable;
1213

13-
public function get(string $separator, array $values): string
14+
/**
15+
* @param string $separator
16+
* @param array|\DragonCode\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Arrayable|\ArrayObject|DataTransferObject $values
17+
*
18+
* @return string
19+
*/
20+
public function get(string $separator, $values): string
1421
{
22+
$values = $this->toArray($values);
23+
1524
$hashed = $this->hash($values);
1625

1726
return $this->compile($hashed, $separator);

src/Support/Tag.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@
55
namespace DragonCode\Cache\Support;
66

77
use DragonCode\Cache\Concerns\Arrayable;
8+
use DragonCode\Contracts\DataTransferObject\DataTransferObject;
89
use DragonCode\Support\Facades\Helpers\Ables\Stringable;
910

1011
class Tag
1112
{
1213
use Arrayable;
1314

14-
public function get(array $tags): array
15+
/**
16+
* @param array|\DragonCode\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Arrayable|\ArrayObject|DataTransferObject $tags
17+
*
18+
* @return array
19+
*/
20+
public function get($tags): array
21+
{
22+
$tags = $this->toArray($tags);
23+
24+
return $this->map($tags);
25+
}
26+
27+
protected function map(array $tags): array
1528
{
1629
return $this->arrayMap($tags, function (string $tag) {
1730
return $this->slug($tag);

tests/Fixtures/Dto/CustomDto.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures\Dto;
6+
7+
use DragonCode\SimpleDataTransferObject\DataTransferObject;
8+
9+
class CustomDto extends DataTransferObject
10+
{
11+
public $wasd = null;
12+
}

tests/Support/KeyTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
namespace Tests\Support;
66

77
use DragonCode\Cache\Facades\Support\Key;
8+
use Tests\Fixtures\Concerns\Dtoable;
9+
use Tests\Fixtures\Dto\CustomDto;
10+
use Tests\Fixtures\Dto\DtoObject;
811
use Tests\TestCase;
912

1013
class KeyTest extends TestCase
1114
{
15+
use Dtoable;
16+
17+
protected $value = [
18+
'foo' => 'Foo',
19+
'bar' => 'Bar',
20+
];
21+
1222
public function testString()
1323
{
1424
$key = Key::get(':', ['Foo', 'Bar', 'Baz']);
@@ -48,4 +58,34 @@ public function testCombine()
4858

4959
$this->assertSame($expected, $key);
5060
}
61+
62+
public function testArrayable()
63+
{
64+
$key = Key::get(':', $this->dto());
65+
66+
$expected = '1356c67d7ad1638d816bfb822dd2c25d:ddc35f88fa71b6ef142ae61f35364653';
67+
68+
$this->assertSame($expected, $key);
69+
}
70+
71+
public function testMultiObjectArrays()
72+
{
73+
$key = Key::get(':', [
74+
'qwe',
75+
'rty',
76+
DtoObject::make(['foo' => 'Foo']),
77+
DtoObject::make(['bar' => 'Bar']),
78+
CustomDto::make(['wasd' => 'WASD']),
79+
]);
80+
81+
// Before hashing, the keys look like this:
82+
// qwe:rty:Foo:Bar:WASD
83+
84+
$expected =
85+
'76d80224611fc919a5d54f0ff9fba446:24113791d2218cb84c9f0462e91596ef:' .
86+
'1356c67d7ad1638d816bfb822dd2c25d:ddc35f88fa71b6ef142ae61f35364653:' .
87+
'91412421a30e87ce15a4f10ea39f6682';
88+
89+
$this->assertSame($expected, $key);
90+
}
5191
}

tests/Support/TagTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
namespace Tests\Support;
66

77
use DragonCode\Cache\Facades\Support\Tag;
8+
use Tests\Fixtures\Concerns\Dtoable;
9+
use Tests\Fixtures\Dto\CustomDto;
10+
use Tests\Fixtures\Dto\DtoObject;
811
use Tests\TestCase;
912

1013
class TagTest extends TestCase
1114
{
15+
use Dtoable;
16+
17+
protected $value = [
18+
'foo' => 'Foo',
19+
'bar' => 'Bar',
20+
];
21+
1222
public function testString()
1323
{
1424
$tags = Tag::get(['FoO', 'Bar', 'a-aZ', ' Qwe Rt_y']);
@@ -39,4 +49,28 @@ public function testArray()
3949

4050
$this->assertSame($expected, $tags);
4151
}
52+
53+
public function testArrayable()
54+
{
55+
$key = Tag::get($this->dto());
56+
57+
$expected = ['foo', 'bar'];
58+
59+
$this->assertSame($expected, $key);
60+
}
61+
62+
public function testMultiObjectArrays()
63+
{
64+
$key = Tag::get([
65+
'qwe',
66+
'rty',
67+
DtoObject::make(['foo' => 'Foo']),
68+
DtoObject::make(['bar' => 'Bar']),
69+
CustomDto::make(['wasd' => 'WASD']),
70+
]);
71+
72+
$expected = ['qwe', 'rty', 'foo', 'bar', 'wasd'];
73+
74+
$this->assertSame($expected, $key);
75+
}
4276
}

0 commit comments

Comments
 (0)