1<?php 2/** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10namespace Zend\Filter\Compress; 11 12use Zend\Filter\Exception; 13 14/** 15 * Compression adapter for php snappy (http://code.google.com/p/php-snappy/) 16 */ 17class Snappy implements CompressionAlgorithmInterface 18{ 19 /** 20 * Class constructor 21 * 22 * @param null|array|\Traversable $options (Optional) Options to set 23 * @throws Exception\ExtensionNotLoadedException if snappy extension not loaded 24 */ 25 public function __construct($options = null) 26 { 27 if (!extension_loaded('snappy')) { 28 throw new Exception\ExtensionNotLoadedException('This filter needs the snappy extension'); 29 } 30 } 31 32 /** 33 * Compresses the given content 34 * 35 * @param string $content 36 * @return string 37 * @throws Exception\RuntimeException on memory, output length or data warning 38 */ 39 public function compress($content) 40 { 41 $compressed = snappy_compress($content); 42 43 if ($compressed === false) { 44 throw new Exception\RuntimeException('Error while compressing.'); 45 } 46 47 return $compressed; 48 } 49 50 /** 51 * Decompresses the given content 52 * 53 * @param string $content 54 * @return string 55 * @throws Exception\RuntimeException on memory, output length or data warning 56 */ 57 public function decompress($content) 58 { 59 $compressed = snappy_uncompress($content); 60 61 if ($compressed === false) { 62 throw new Exception\RuntimeException('Error while decompressing.'); 63 } 64 65 return $compressed; 66 } 67 68 /** 69 * Returns the adapter name 70 * 71 * @return string 72 */ 73 public function toString() 74 { 75 return 'Snappy'; 76 } 77} 78