-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrefixedUtilityTrait.php
More file actions
91 lines (77 loc) · 2.5 KB
/
Copy pathPrefixedUtilityTrait.php
File metadata and controls
91 lines (77 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* This file is part of php-cache organization.
*
* (c) 2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Cache\Prefixed;
use Cache\Prefixed\Exception\InvalidArgumentException;
/**
* Utility to reduce code suplication between the Prefixed* decoratrs.
*
* @author ndobromirov
*/
trait PrefixedUtilityTrait
{
private const ENCODING_MARKER = '_x';
private string $prefix;
private function encodePrefix(string $prefix): string
{
$encoded = '';
$length = \strlen($prefix);
for ($index = 0; $index < $length; ++$index) {
$byte = $prefix[$index];
$ordinal = \ord($byte);
$portable = ($ordinal >= 48 && $ordinal <= 57)
|| ($ordinal >= 65 && $ordinal <= 90)
|| ($ordinal >= 97 && $ordinal <= 122)
|| '_' === $byte
|| '.' === $byte;
$startsMarker = '_' === $byte && 'x' === ($prefix[$index + 1] ?? null);
$encoded .= $portable && !$startsMarker
? $byte
: self::ENCODING_MARKER.strtoupper(bin2hex($byte)).'_';
}
return $encoded;
}
/**
* Add namespace prefix on the key.
*
* @param string $key Reference to the key. It is mutated.
*/
private function prefixValue(string &$key): void
{
$this->validateKey($key);
$key = $this->prefix.$key;
}
private function validateKey(string $key): void
{
if ('' === $key) {
throw new InvalidArgumentException('Cache key cannot be an empty string');
}
if (preg_match('|[\{\}\(\)/\\\@\:]|', $key)) {
throw new InvalidArgumentException(\sprintf('Invalid key: "%s". The key contains one or more characters reserved for future extension: {}()/\@:', $key));
}
}
/**
* Adds a namespace prefix on a list of keys.
*
* @param array<array-key, mixed> $keys
*
* @return array<array-key, string>
*/
private function prefixValues(array $keys): array
{
foreach ($keys as $index => $key) {
if (!\is_string($key)) {
throw new InvalidArgumentException(\sprintf('Cache key must be string, "%s" given', get_debug_type($key)));
}
$this->prefixValue($key);
$keys[$index] = $key;
}
return $keys;
}
}