web/Zend/Filter/StringToLower.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: StringToLower.php 22790 2010-08-03 19:16:33Z thomas $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Filter_Interface
       
    24  */
       
    25 require_once 'Zend/Filter/Interface.php';
       
    26 
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Filter
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Filter_StringToLower implements Zend_Filter_Interface
       
    34 {
       
    35     /**
       
    36      * Encoding for the input string
       
    37      *
       
    38      * @var string
       
    39      */
       
    40     protected $_encoding = null;
       
    41 
       
    42     /**
       
    43      * Constructor
       
    44      *
       
    45      * @param string|array|Zend_Config $options OPTIONAL
       
    46      */
       
    47     public function __construct($options = null)
       
    48     {
       
    49         if ($options instanceof Zend_Config) {
       
    50             $options = $options->toArray();
       
    51         } else if (!is_array($options)) {
       
    52             $options = func_get_args();
       
    53             $temp    = array();
       
    54             if (!empty($options)) {
       
    55                 $temp['encoding'] = array_shift($options);
       
    56             }
       
    57             $options = $temp;
       
    58         }
       
    59 
       
    60         if (!array_key_exists('encoding', $options) && function_exists('mb_internal_encoding')) {
       
    61             $options['encoding'] = mb_internal_encoding();
       
    62         }
       
    63 
       
    64         if (array_key_exists('encoding', $options)) {
       
    65             $this->setEncoding($options['encoding']);
       
    66         }
       
    67     }
       
    68 
       
    69     /**
       
    70      * Returns the set encoding
       
    71      *
       
    72      * @return string
       
    73      */
       
    74     public function getEncoding()
       
    75     {
       
    76         return $this->_encoding;
       
    77     }
       
    78 
       
    79     /**
       
    80      * Set the input encoding for the given string
       
    81      *
       
    82      * @param  string $encoding
       
    83      * @return Zend_Filter_StringToLower Provides a fluent interface
       
    84      * @throws Zend_Filter_Exception
       
    85      */
       
    86     public function setEncoding($encoding = null)
       
    87     {
       
    88         if ($encoding !== null) {
       
    89             if (!function_exists('mb_strtolower')) {
       
    90                 require_once 'Zend/Filter/Exception.php';
       
    91                 throw new Zend_Filter_Exception('mbstring is required for this feature');
       
    92             }
       
    93 
       
    94             $encoding = (string) $encoding;
       
    95             if (!in_array(strtolower($encoding), array_map('strtolower', mb_list_encodings()))) {
       
    96                 require_once 'Zend/Filter/Exception.php';
       
    97                 throw new Zend_Filter_Exception("The given encoding '$encoding' is not supported by mbstring");
       
    98             }
       
    99         }
       
   100 
       
   101         $this->_encoding = $encoding;
       
   102         return $this;
       
   103     }
       
   104 
       
   105     /**
       
   106      * Defined by Zend_Filter_Interface
       
   107      *
       
   108      * Returns the string $value, converting characters to lowercase as necessary
       
   109      *
       
   110      * @param  string $value
       
   111      * @return string
       
   112      */
       
   113     public function filter($value)
       
   114     {
       
   115         if ($this->_encoding !== null) {
       
   116             return mb_strtolower((string) $value, $this->_encoding);
       
   117         }
       
   118 
       
   119         return strtolower((string) $value);
       
   120     }
       
   121 }