File indexing completed on 2024-05-12 06:02:33

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Interface
0024  */
0025 // require_once 'Zend/Filter/Interface.php';
0026 
0027 /**
0028  * Compresses a given string
0029  *
0030  * @category   Zend
0031  * @package    Zend_Filter
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Filter_Compress implements Zend_Filter_Interface
0036 {
0037     /**
0038      * Compression adapter
0039      */
0040     protected $_adapter = 'Gz';
0041 
0042     /**
0043      * Compression adapter constructor options
0044      */
0045     protected $_adapterOptions = array();
0046 
0047     /**
0048      * Class constructor
0049      *
0050      * @param string|array $options (Optional) Options to set
0051      */
0052     public function __construct($options = null)
0053     {
0054         if ($options instanceof Zend_Config) {
0055             $options = $options->toArray();
0056         }
0057         if (is_string($options)) {
0058             $this->setAdapter($options);
0059         } elseif ($options instanceof Zend_Filter_Compress_CompressInterface) {
0060             $this->setAdapter($options);
0061         } elseif (is_array($options)) {
0062             $this->setOptions($options);
0063         }
0064     }
0065 
0066     /**
0067      * Set filter setate
0068      *
0069      * @param  array $options
0070      * @return Zend_Filter_Compress
0071      */
0072     public function setOptions(array $options)
0073     {
0074         foreach ($options as $key => $value) {
0075             if ($key == 'options') {
0076                 $key = 'adapterOptions';
0077             }
0078             $method = 'set' . ucfirst($key);
0079             if (method_exists($this, $method)) {
0080                 $this->$method($value);
0081             }
0082         }
0083         return $this;
0084     }
0085 
0086     /**
0087      * Returns the current adapter, instantiating it if necessary
0088      *
0089      * @return string
0090      */
0091     public function getAdapter()
0092     {
0093         if ($this->_adapter instanceof Zend_Filter_Compress_CompressInterface) {
0094             return $this->_adapter;
0095         }
0096 
0097         $adapter = $this->_adapter;
0098         $options = $this->getAdapterOptions();
0099         if (!class_exists($adapter)) {
0100             // require_once 'Zend/Loader.php';
0101             if (Zend_Loader::isReadable('Zend/Filter/Compress/' . ucfirst($adapter) . '.php')) {
0102                 $adapter = 'Zend_Filter_Compress_' . ucfirst($adapter);
0103             }
0104             Zend_Loader::loadClass($adapter);
0105         }
0106 
0107         $this->_adapter = new $adapter($options);
0108         if (!$this->_adapter instanceof Zend_Filter_Compress_CompressInterface) {
0109             // require_once 'Zend/Filter/Exception.php';
0110             throw new Zend_Filter_Exception("Compression adapter '" . $adapter . "' does not implement Zend_Filter_Compress_CompressInterface");
0111         }
0112         return $this->_adapter;
0113     }
0114 
0115     /**
0116      * Retrieve adapter name
0117      *
0118      * @return string
0119      */
0120     public function getAdapterName()
0121     {
0122         return $this->getAdapter()->toString();
0123     }
0124 
0125     /**
0126      * Sets compression adapter
0127      *
0128      * @param  string|Zend_Filter_Compress_CompressInterface $adapter Adapter to use
0129      * @return Zend_Filter_Compress
0130      */
0131     public function setAdapter($adapter)
0132     {
0133         if ($adapter instanceof Zend_Filter_Compress_CompressInterface) {
0134             $this->_adapter = $adapter;
0135             return $this;
0136         }
0137         if (!is_string($adapter)) {
0138             // require_once 'Zend/Filter/Exception.php';
0139             throw new Zend_Filter_Exception('Invalid adapter provided; must be string or instance of Zend_Filter_Compress_CompressInterface');
0140         }
0141         $this->_adapter = $adapter;
0142 
0143         return $this;
0144     }
0145 
0146     /**
0147      * Retrieve adapter options
0148      *
0149      * @return array
0150      */
0151     public function getAdapterOptions()
0152     {
0153         return $this->_adapterOptions;
0154     }
0155 
0156     /**
0157      * Set adapter options
0158      *
0159      * @param  array $options
0160      * @return void
0161      */
0162     public function setAdapterOptions(array $options)
0163     {
0164         $this->_adapterOptions = $options;
0165         return $this;
0166     }
0167 
0168     /**
0169      * Calls adapter methods
0170      *
0171      * @param string       $method  Method to call
0172      * @param string|array $options Options for this method
0173      */
0174     public function __call($method, $options)
0175     {
0176         $adapter = $this->getAdapter();
0177         if (!method_exists($adapter, $method)) {
0178             // require_once 'Zend/Filter/Exception.php';
0179             throw new Zend_Filter_Exception("Unknown method '{$method}'");
0180         }
0181 
0182         return call_user_func_array(array($adapter, $method), $options);
0183     }
0184 
0185     /**
0186      * Defined by Zend_Filter_Interface
0187      *
0188      * Compresses the content $value with the defined settings
0189      *
0190      * @param  string $value Content to compress
0191      * @return string The compressed content
0192      */
0193     public function filter($value)
0194     {
0195         return $this->getAdapter()->compress($value);
0196     }
0197 }