1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace OpenSpout\Reader\XLSX\Manager\SharedStringsCaching;
|
|
|
6 |
|
|
|
7 |
use OpenSpout\Reader\Exception\SharedStringNotFoundException;
|
|
|
8 |
use RuntimeException;
|
|
|
9 |
use SplFixedArray;
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* This class implements the in-memory caching strategy for shared strings.
|
|
|
13 |
* This strategy is used when the number of unique strings is low, compared to the memory available.
|
|
|
14 |
*
|
|
|
15 |
* @internal
|
|
|
16 |
*/
|
|
|
17 |
final class InMemoryStrategy implements CachingStrategyInterface
|
|
|
18 |
{
|
|
|
19 |
/** @var SplFixedArray<string> Array used to cache the shared strings */
|
|
|
20 |
private SplFixedArray $inMemoryCache;
|
|
|
21 |
|
|
|
22 |
/** @var bool Whether the cache has been closed */
|
|
|
23 |
private bool $isCacheClosed = false;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* @param int $sharedStringsUniqueCount Number of unique shared strings
|
|
|
27 |
*/
|
|
|
28 |
public function __construct(int $sharedStringsUniqueCount)
|
|
|
29 |
{
|
|
|
30 |
$this->inMemoryCache = new SplFixedArray($sharedStringsUniqueCount);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Adds the given string to the cache.
|
|
|
35 |
*
|
|
|
36 |
* @param string $sharedString The string to be added to the cache
|
|
|
37 |
* @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
|
|
|
38 |
*/
|
|
|
39 |
public function addStringForIndex(string $sharedString, int $sharedStringIndex): void
|
|
|
40 |
{
|
|
|
41 |
if (!$this->isCacheClosed) {
|
|
|
42 |
$this->inMemoryCache->offsetSet($sharedStringIndex, $sharedString);
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Closes the cache after the last shared string was added.
|
|
|
48 |
* This prevents any additional string from being added to the cache.
|
|
|
49 |
*/
|
|
|
50 |
public function closeCache(): void
|
|
|
51 |
{
|
|
|
52 |
$this->isCacheClosed = true;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Returns the string located at the given index from the cache.
|
|
|
57 |
*
|
|
|
58 |
* @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
|
|
|
59 |
*
|
|
|
60 |
* @return string The shared string at the given index
|
|
|
61 |
*
|
|
|
62 |
* @throws SharedStringNotFoundException If no shared string found for the given index
|
|
|
63 |
*/
|
|
|
64 |
public function getStringAtIndex(int $sharedStringIndex): string
|
|
|
65 |
{
|
|
|
66 |
try {
|
|
|
67 |
return $this->inMemoryCache->offsetGet($sharedStringIndex);
|
|
|
68 |
} catch (RuntimeException) {
|
|
|
69 |
throw new SharedStringNotFoundException("Shared string not found for index: {$sharedStringIndex}");
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Destroys the cache, freeing memory and removing any created artifacts.
|
|
|
75 |
*/
|
|
|
76 |
public function clearCache(): void
|
|
|
77 |
{
|
|
|
78 |
$this->inMemoryCache = new SplFixedArray(0);
|
|
|
79 |
$this->isCacheClosed = false;
|
|
|
80 |
}
|
|
|
81 |
}
|