[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/spout/src/Spout/Reader/XLSX/Helper/SharedStringsCaching/ -> InMemoryStrategy.php (source)

   1  <?php
   2  
   3  namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching;
   4  
   5  use Box\Spout\Reader\Exception\SharedStringNotFoundException;
   6  
   7  /**
   8   * Class InMemoryStrategy
   9   *
  10   * This class implements the in-memory caching strategy for shared strings.
  11   * This strategy is used when the number of unique strings is low, compared to the memory available.
  12   *
  13   * @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching
  14   */
  15  class InMemoryStrategy implements CachingStrategyInterface
  16  {
  17      /** @var \SplFixedArray Array used to cache the shared strings */
  18      protected $inMemoryCache;
  19  
  20      /** @var bool Whether the cache has been closed */
  21      protected $isCacheClosed;
  22  
  23      /**
  24       * @param int $sharedStringsUniqueCount Number of unique shared strings
  25       */
  26      public function __construct($sharedStringsUniqueCount)
  27      {
  28          $this->inMemoryCache = new \SplFixedArray($sharedStringsUniqueCount);
  29          $this->isCacheClosed = false;
  30      }
  31  
  32      /**
  33       * Adds the given string to the cache.
  34       *
  35       * @param string $sharedString The string to be added to the cache
  36       * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
  37       * @return void
  38       */
  39      public function addStringForIndex($sharedString, $sharedStringIndex)
  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       * @return void
  51       */
  52      public function closeCache()
  53      {
  54          $this->isCacheClosed = true;
  55      }
  56  
  57      /**
  58       * Returns the string located at the given index from the cache.
  59       *
  60       * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
  61       * @return string The shared string at the given index
  62       * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index
  63       */
  64      public function getStringAtIndex($sharedStringIndex)
  65      {
  66          try {
  67              return $this->inMemoryCache->offsetGet($sharedStringIndex);
  68          } catch (\RuntimeException $e) {
  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       * @return void
  77       */
  78      public function clearCache()
  79      {
  80          unset($this->inMemoryCache);
  81          $this->isCacheClosed = false;
  82      }
  83  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1