DeflateMarshaller.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Marshaller;
  11. use Symfony\Component\Cache\Exception\CacheException;
  12. /**
  13. * Compresses values using gzdeflate().
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class DeflateMarshaller implements MarshallerInterface
  18. {
  19. private $marshaller;
  20. public function __construct(MarshallerInterface $marshaller)
  21. {
  22. if (!\function_exists('gzdeflate')) {
  23. throw new CacheException('The "zlib" PHP extension is not loaded.');
  24. }
  25. $this->marshaller = $marshaller;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function marshall(array $values, ?array &$failed): array
  31. {
  32. return array_map('gzdeflate', $this->marshaller->marshall($values, $failed));
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function unmarshall(string $value)
  38. {
  39. if (false !== $inflatedValue = @gzinflate($value)) {
  40. $value = $inflatedValue;
  41. }
  42. return $this->marshaller->unmarshall($value);
  43. }
  44. }