Skip to content

Commit 5f90f5a

Browse files
committed
Merge branch 'release/1.1.0'
2 parents 325226e + 8bd15aa commit 5f90f5a

File tree

7 files changed

+148
-7
lines changed

7 files changed

+148
-7
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ $generator = new Generator(
9191
$generator->generate($object); // Generates "some-key_some-value_another-key_another-value"
9292
```
9393

94+
### PSR-6 compatible key generation
95+
```php
96+
use EcomDev\CacheKey\Normalizer\Psr6Normalizer;
97+
use EcomDev\CacheKey\Generator;
98+
99+
$generator = new Generator(Psr6Normalizer::create());
100+
$generator->generate('give_me-psr-6-compatible_#^.;:/\\-key'); // Generates "give_me-psr-6-compatible_235e.3b3a2f5c-key"
101+
```
102+
103+
### Prefixed Key Generation
104+
```php
105+
use EcomDev\CacheKey\Normalizer\Psr6Normalizer;
106+
use EcomDev\CacheKey\Generator;
107+
108+
$generator = new Generator(Psr6Normalizer::create(), null, 'prefix-');
109+
$generator->generate('some-non-prefixed-key'); // Generates "prefix-some-non-prefixed-key"
110+
```
94111

95112
## Contribution
96113
Make a pull request based on develop branch

RELEASE_NOTES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Release 1.1.0
2+
New functionality:
3+
4+
* Added normalizer for PSR-6 compatible key. `Psr6Normalizer` uses `EncodeNormalier` and `LengthNormalizer` behind the scenes.
5+
6+
# Release 1.0.0
7+
Initial features:
8+
9+
* **Encode Normalizer** encodes all characters except alphanum into hex representation
10+
* **Length Normalizer** strips characters after some length is reached
11+
* **Scalar Converter** converts any non string scalar to string representation
12+
* **Hash Converter** converts any array into md5 hash of json encoded value
13+
* **Key Value Converter** converts simple key value pair array into string representation like `key1_value1_key2_value2`

spec/Normalizer/EncodeNormalizerSpec.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace spec\EcomDev\CacheKey\Normalizer;
44

5+
use EcomDev\CacheKey\NormalizerInterface;
56
use PhpSpec\ObjectBehavior;
67
use Prophecy\Argument;
78

89
class EncodeNormalizerSpec extends ObjectBehavior
910
{
1011
function it_implements_normalizer_interface()
1112
{
12-
$this->shouldHaveType('EcomDev\CacheKey\NormalizerInterface');
13+
$this->shouldHaveType(NormalizerInterface::class);
1314
}
1415

1516
function it_encodes_everything_except_alphanums_hypen_and_underscores()
1617
{
17-
$this->normalize('this:is../not&a#valid91code')->shouldReturn('this3ais2e2e2fnot26a23valid91code');
18+
$this->normalize('this:is../not&a#valid91code')->shouldReturn('this3ais..2fnot26a23valid91code');
1819

1920
}
2021

@@ -23,9 +24,10 @@ function it_lowers_uppercase_characters()
2324
$this->normalize('ThisIsUpperCASE')->shouldReturn('thisisuppercase');
2425
}
2526

26-
function it_allows_underscore_and_hypen()
27+
function it_allows_underscore_and_hypen_and_dot()
2728
{
2829
$this->normalize('this-is-a-valid-key')->shouldReturn('this-is-a-valid-key');
2930
$this->normalize('this_is_a_valid_key')->shouldReturn('this_is_a_valid_key');
31+
$this->normalize('this.is.a.valid.key')->shouldReturn('this.is.a.valid.key');
3032
}
3133
}

spec/Normalizer/LengthNormalizerSpec.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace spec\EcomDev\CacheKey\Normalizer;
44

5+
use EcomDev\CacheKey\NormalizerInterface;
56
use PhpSpec\ObjectBehavior;
67
use Prophecy\Argument;
78

89
class LengthNormalizerSpec extends ObjectBehavior
910
{
1011
function it_implements_normalizer_interface()
1112
{
12-
$this->shouldHaveType('EcomDev\CacheKey\NormalizerInterface');
13+
$this->shouldHaveType(NormalizerInterface::class);
1314
}
1415

1516
function it_is_possible_to_specify_custom_max_length()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace spec\EcomDev\CacheKey\Normalizer;
4+
5+
use EcomDev\CacheKey\Normalizer\EncodeNormalizer;
6+
use EcomDev\CacheKey\Normalizer\LengthNormalizer;
7+
use EcomDev\CacheKey\NormalizerInterface;
8+
use PhpSpec\ObjectBehavior;
9+
use Prophecy\Argument;
10+
11+
class Psr6NormalizerSpec extends ObjectBehavior
12+
{
13+
14+
function it_implements_normalizer_interface(
15+
EncodeNormalizer $encodeNormalizer, LengthNormalizer $lengthNormalizer
16+
)
17+
{
18+
$this->beConstructedWith($encodeNormalizer, $lengthNormalizer);
19+
$this->shouldHaveType(NormalizerInterface::class);
20+
}
21+
22+
function it_uses_encode_and_length_normalizer_as_dependency(
23+
EncodeNormalizer $encodeNormalizer, LengthNormalizer $lengthNormalizer
24+
)
25+
{
26+
$this->beConstructedWith($encodeNormalizer, $lengthNormalizer);
27+
$encodeNormalizer->normalize('value1')->willReturn('value2')->shouldBeCalled();
28+
$lengthNormalizer->normalize('value2')->willReturn('value_normalized')->shouldBeCalled();
29+
$this->normalize('value1')->shouldReturn('value_normalized');
30+
}
31+
32+
function it_has_factory_to_create_it_with_needed_normalizers()
33+
{
34+
$this->beConstructedThrough('create');
35+
$this->normalize('some-in&valid-012931231value')->shouldReturn('some-in26valid-012931231value');
36+
$this->normalize('some-very-long-value-that-is-longer-than-sixty-four-characters-can-you-belive-it')
37+
->shouldReturn('some-very-long-value-that-is-longer-than-sixty-four-char485b813b');
38+
}
39+
}

src/Normalizer/EncodeNormalizer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
/**
88
* Encode Normalizer
99
*
10-
* Encodes all characters that are not alphanum, dash or underscore
10+
* Encodes all characters that are not alphanum, dash, underscore or dots
1111
* Also it makes all characters lower cased
12+
*
13+
* Fully PSR-6 compliant cache key generator
1214
*/
1315
class EncodeNormalizer implements NormalizerInterface
1416
{
@@ -31,8 +33,7 @@ public function normalize($key)
3133
FILTER_FLAG_ENCODE_HIGH | FILTER_FLAG_ENCODE_LOW | FILTER_FLAG_ENCODE_AMP
3234
),
3335
[
34-
'%' => '',
35-
'.' => '2e'
36+
'%' => ''
3637
]
3738
));
3839
}

src/Normalizer/Psr6Normalizer.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace EcomDev\CacheKey\Normalizer;
4+
5+
use EcomDev\CacheKey\NormalizerInterface;
6+
7+
/**
8+
* PSR-6 Normalizer
9+
*
10+
* By default follows these rules:
11+
* Implementing libraries MUST support keys consisting of the characters A-Z, a-z, 0-9, _,
12+
* and . in any order in UTF-8 encoding and a length of up to 64 characters.
13+
*/
14+
class Psr6Normalizer implements NormalizerInterface
15+
{
16+
/**
17+
* Encode normalizer
18+
*
19+
* @var EncodeNormalizer
20+
*/
21+
private $encodeNormalizer;
22+
23+
/**
24+
* Length normalizer
25+
*
26+
* @var LengthNormalizer
27+
*/
28+
private $lengthNormalizer;
29+
30+
/**
31+
* Configured dependencies
32+
*
33+
* @param EncodeNormalizer $encodeNormalizer
34+
* @param LengthNormalizer $lengthNormalizer
35+
*/
36+
public function __construct(
37+
EncodeNormalizer $encodeNormalizer,
38+
LengthNormalizer $lengthNormalizer
39+
) {
40+
41+
$this->encodeNormalizer = $encodeNormalizer;
42+
$this->lengthNormalizer = $lengthNormalizer;
43+
}
44+
45+
/**
46+
* Creates a new instance of PSR-6 compatible normalizer
47+
*
48+
* @return static
49+
*/
50+
public static function create()
51+
{
52+
return new static(new EncodeNormalizer(), new LengthNormalizer(64));
53+
}
54+
55+
/**
56+
* Normalizes key according to PSR-6 specification
57+
*
58+
* @param string $key
59+
*
60+
* @return string
61+
*/
62+
public function normalize($key)
63+
{
64+
return $this->lengthNormalizer->normalize(
65+
$this->encodeNormalizer->normalize($key)
66+
);
67+
}
68+
}

0 commit comments

Comments
 (0)