|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wambo\Catalog\Cache; |
|
3
|
|
|
|
|
4
|
|
|
use Wambo\Core\Module\Exception\InvalidArgumentException; |
|
5
|
|
|
use Wambo\Core\ValueObject\ValueObjectInterface; |
|
6
|
|
|
use Wambo\Core\ValueObject\ValueObjectTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class CacheKey represents a single cache key used to identify elements in a cache storage. |
|
10
|
|
|
* |
|
11
|
|
|
* @package Wambo\Catalog\Cache |
|
12
|
|
|
*/ |
|
13
|
|
|
class CacheKey implements ValueObjectInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use ValueObjectTrait; |
|
16
|
|
|
private $value; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a new cache key instance for the given element name and tags. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $elementName A name of the element that you want to cache |
|
22
|
|
|
* @param \string[] $tags A list of tags that help to categorize the element you want to cache |
|
23
|
|
|
* |
|
24
|
|
|
* @return CacheKey |
|
|
|
|
|
|
25
|
|
|
*/ |
|
26
|
17 |
|
function __construct(string $elementName, string ...$tags) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
17 |
|
$key = sprintf("%s--%s", $this->getCacheKeyPrefix(...$tags), $this->getCleanedName($elementName)); |
|
29
|
12 |
|
$this->value = $key; |
|
30
|
|
|
|
|
31
|
12 |
|
$this->setConstructed(); |
|
32
|
12 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get the value of the cache key |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
6 |
|
public function getValue() |
|
40
|
|
|
{ |
|
41
|
6 |
|
return $this->value; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get a cache key prefix from the given tags (default: "none") |
|
46
|
|
|
* |
|
47
|
|
|
* @param \string[] ...$tags A list of tags that help to categorize the element you want to cache |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
17 |
|
private function getCacheKeyPrefix(string ...$tags): string |
|
52
|
|
|
{ |
|
53
|
17 |
|
if (count($tags) === 0) { |
|
54
|
11 |
|
return "none"; // default prefix if no tags were given |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// sort the tags alphabetically |
|
58
|
6 |
|
sort($tags); |
|
59
|
|
|
|
|
60
|
|
|
// assemble the prefix |
|
61
|
6 |
|
$prefix = ""; |
|
62
|
6 |
|
foreach ($tags as $tag) { |
|
63
|
|
|
|
|
64
|
6 |
|
$cleanedTag = $this->getCleanedName($tag); |
|
65
|
|
|
|
|
66
|
3 |
|
if (strlen($prefix) == 0) { |
|
67
|
3 |
|
$prefix = "$cleanedTag"; |
|
68
|
3 |
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
$prefix = "$prefix,$cleanedTag"; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return $prefix; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get a cleaned cache key name for the given element name |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $elementName |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
* |
|
84
|
|
|
* @throws InvalidArgumentException If the given element name is empty |
|
85
|
|
|
*/ |
|
86
|
17 |
|
private function getCleanedName(string $elementName): string |
|
87
|
|
|
{ |
|
88
|
17 |
|
$trimmedElementName = trim($elementName); |
|
89
|
17 |
|
if (strlen($trimmedElementName) == 0) { |
|
90
|
5 |
|
throw new InvalidArgumentException("The element name cannot be empty"); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
12 |
|
return strtolower($trimmedElementName); |
|
94
|
|
|
} |
|
95
|
|
|
} |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.