File indexing completed on 2024-12-22 05:36:41

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  * @category   Zend
0029  * @package    Zend_Filter
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Filter_StringToUpper implements Zend_Filter_Interface
0034 {
0035     /**
0036      * Encoding for the input string
0037      *
0038      * @var string
0039      */
0040     protected $_encoding = null;
0041 
0042     /**
0043      * Constructor
0044      *
0045      * @param string|array $options OPTIONAL
0046      */
0047     public function __construct($options = null)
0048     {
0049         if ($options instanceof Zend_Config) {
0050             $options = $options->toArray();
0051         } else if (!is_array($options)) {
0052             $options = func_get_args();
0053             $temp    = array();
0054             if (!empty($options)) {
0055                 $temp['encoding'] = array_shift($options);
0056             }
0057             $options = $temp;
0058         }
0059 
0060         if (!array_key_exists('encoding', $options) && function_exists('mb_internal_encoding')) {
0061             $options['encoding'] = mb_internal_encoding();
0062         }
0063 
0064         if (array_key_exists('encoding', $options)) {
0065             $this->setEncoding($options['encoding']);
0066         }
0067     }
0068 
0069     /**
0070      * Returns the set encoding
0071      *
0072      * @return string
0073      */
0074     public function getEncoding()
0075     {
0076         return $this->_encoding;
0077     }
0078 
0079     /**
0080      * Set the input encoding for the given string
0081      *
0082      * @param  string $encoding
0083      * @return Zend_Filter_StringToUpper Provides a fluent interface
0084      * @throws Zend_Filter_Exception
0085      */
0086     public function setEncoding($encoding = null)
0087     {
0088         if ($encoding !== null) {
0089             if (!function_exists('mb_strtoupper')) {
0090                 // require_once 'Zend/Filter/Exception.php';
0091                 throw new Zend_Filter_Exception('mbstring is required for this feature');
0092             }
0093 
0094             $encoding = (string) $encoding;
0095             if (!in_array(strtolower($encoding), array_map('strtolower', mb_list_encodings()))) {
0096                 // require_once 'Zend/Filter/Exception.php';
0097                 throw new Zend_Filter_Exception("The given encoding '$encoding' is not supported by mbstring");
0098             }
0099         }
0100 
0101         $this->_encoding = $encoding;
0102         return $this;
0103     }
0104 
0105     /**
0106      * Defined by Zend_Filter_Interface
0107      *
0108      * Returns the string $value, converting characters to uppercase as necessary
0109      *
0110      * @param  string $value
0111      * @return string
0112      */
0113     public function filter($value)
0114     {
0115         if ($this->_encoding) {
0116             return mb_strtoupper((string) $value, $this->_encoding);
0117         }
0118 
0119         return strtoupper((string) $value);
0120     }
0121 }