web/Zend/Filter/Compress.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Filter
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Compress.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Filter_Interface
       
    24  */
       
    25 require_once 'Zend/Filter/Interface.php';
       
    26 
       
    27 /**
       
    28  * Compresses a given string
       
    29  *
       
    30  * @category   Zend
       
    31  * @package    Zend_Filter
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 class Zend_Filter_Compress implements Zend_Filter_Interface
       
    36 {
       
    37     /**
       
    38      * Compression adapter
       
    39      */
       
    40     protected $_adapter = 'Gz';
       
    41 
       
    42     /**
       
    43      * Compression adapter constructor options
       
    44      */
       
    45     protected $_adapterOptions = array();
       
    46 
       
    47     /**
       
    48      * Class constructor
       
    49      *
       
    50      * @param string|array $options (Optional) Options to set
       
    51      */
       
    52     public function __construct($options = null)
       
    53     {
       
    54         if ($options instanceof Zend_Config) {
       
    55             $options = $options->toArray();
       
    56         }
       
    57         if (is_string($options)) {
       
    58             $this->setAdapter($options);
       
    59         } elseif ($options instanceof Zend_Filter_Compress_CompressInterface) {
       
    60             $this->setAdapter($options);
       
    61         } elseif (is_array($options)) {
       
    62             $this->setOptions($options);
       
    63         }
       
    64     }
       
    65 
       
    66     /**
       
    67      * Set filter setate
       
    68      *
       
    69      * @param  array $options
       
    70      * @return Zend_Filter_Compress
       
    71      */
       
    72     public function setOptions(array $options)
       
    73     {
       
    74         foreach ($options as $key => $value) {
       
    75             if ($key == 'options') {
       
    76                 $key = 'adapterOptions';
       
    77             }
       
    78             $method = 'set' . ucfirst($key);
       
    79             if (method_exists($this, $method)) {
       
    80                 $this->$method($value);
       
    81             }
       
    82         }
       
    83         return $this;
       
    84     }
       
    85 
       
    86     /**
       
    87      * Returns the current adapter, instantiating it if necessary
       
    88      *
       
    89      * @return string
       
    90      */
       
    91     public function getAdapter()
       
    92     {
       
    93         if ($this->_adapter instanceof Zend_Filter_Compress_CompressInterface) {
       
    94             return $this->_adapter;
       
    95         }
       
    96 
       
    97         $adapter = $this->_adapter;
       
    98         $options = $this->getAdapterOptions();
       
    99         if (!class_exists($adapter)) {
       
   100             require_once 'Zend/Loader.php';
       
   101             if (Zend_Loader::isReadable('Zend/Filter/Compress/' . ucfirst($adapter) . '.php')) {
       
   102                 $adapter = 'Zend_Filter_Compress_' . ucfirst($adapter);
       
   103             }
       
   104             Zend_Loader::loadClass($adapter);
       
   105         }
       
   106 
       
   107         $this->_adapter = new $adapter($options);
       
   108         if (!$this->_adapter instanceof Zend_Filter_Compress_CompressInterface) {
       
   109             require_once 'Zend/Filter/Exception.php';
       
   110             throw new Zend_Filter_Exception("Compression adapter '" . $adapter . "' does not implement Zend_Filter_Compress_CompressInterface");
       
   111         }
       
   112         return $this->_adapter;
       
   113     }
       
   114 
       
   115     /**
       
   116      * Retrieve adapter name
       
   117      *
       
   118      * @return string
       
   119      */
       
   120     public function getAdapterName()
       
   121     {
       
   122         return $this->getAdapter()->toString();
       
   123     }
       
   124 
       
   125     /**
       
   126      * Sets compression adapter
       
   127      *
       
   128      * @param  string|Zend_Filter_Compress_CompressInterface $adapter Adapter to use
       
   129      * @return Zend_Filter_Compress
       
   130      */
       
   131     public function setAdapter($adapter)
       
   132     {
       
   133         if ($adapter instanceof Zend_Filter_Compress_CompressInterface) {
       
   134             $this->_adapter = $adapter;
       
   135             return $this;
       
   136         }
       
   137         if (!is_string($adapter)) {
       
   138             require_once 'Zend/Filter/Exception.php';
       
   139             throw new Zend_Filter_Exception('Invalid adapter provided; must be string or instance of Zend_Filter_Compress_CompressInterface');
       
   140         }
       
   141         $this->_adapter = $adapter;
       
   142 
       
   143         return $this;
       
   144     }
       
   145 
       
   146     /**
       
   147      * Retrieve adapter options
       
   148      *
       
   149      * @return array
       
   150      */
       
   151     public function getAdapterOptions()
       
   152     {
       
   153         return $this->_adapterOptions;
       
   154     }
       
   155 
       
   156     /**
       
   157      * Set adapter options
       
   158      *
       
   159      * @param  array $options
       
   160      * @return void
       
   161      */
       
   162     public function setAdapterOptions(array $options)
       
   163     {
       
   164         $this->_adapterOptions = $options;
       
   165         return $this;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Calls adapter methods
       
   170      *
       
   171      * @param string       $method  Method to call
       
   172      * @param string|array $options Options for this method
       
   173      */
       
   174     public function __call($method, $options)
       
   175     {
       
   176         $adapter = $this->getAdapter();
       
   177         if (!method_exists($adapter, $method)) {
       
   178             require_once 'Zend/Filter/Exception.php';
       
   179             throw new Zend_Filter_Exception("Unknown method '{$method}'");
       
   180         }
       
   181 
       
   182         return call_user_func_array(array($adapter, $method), $options);
       
   183     }
       
   184 
       
   185     /**
       
   186      * Defined by Zend_Filter_Interface
       
   187      *
       
   188      * Compresses the content $value with the defined settings
       
   189      *
       
   190      * @param  string $value Content to compress
       
   191      * @return string The compressed content
       
   192      */
       
   193     public function filter($value)
       
   194     {
       
   195         return $this->getAdapter()->compress($value);
       
   196     }
       
   197 }