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  * @see Zend_Loader
0029  */
0030 // require_once 'Zend/Locale/Format.php';
0031 
0032 /**
0033  * Normalizes given localized input
0034  *
0035  * @category   Zend
0036  * @package    Zend_Filter
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_Filter_LocalizedToNormalized implements Zend_Filter_Interface
0041 {
0042     /**
0043      * Set options
0044      * @var array
0045      */
0046     protected $_options = array(
0047         'locale'      => null,
0048         'date_format' => null,
0049         'precision'   => null
0050     );
0051 
0052     /**
0053      * Class constructor
0054      *
0055      * @param string|Zend_Locale $locale (Optional) Locale to set
0056      */
0057     public function __construct($options = null)
0058     {
0059         if ($options instanceof Zend_Config) {
0060             $options = $options->toArray();
0061         }
0062 
0063         if (null !== $options) {
0064             $this->setOptions($options);
0065         }
0066     }
0067 
0068     /**
0069      * Returns the set options
0070      *
0071      * @return array
0072      */
0073     public function getOptions()
0074     {
0075         return $this->_options;
0076     }
0077 
0078     /**
0079      * Sets options to use
0080      *
0081      * @param  array $options (Optional) Options to use
0082      * @return Zend_Filter_LocalizedToNormalized
0083      */
0084     public function setOptions(array $options = null)
0085     {
0086         $this->_options = $options + $this->_options;
0087         return $this;
0088     }
0089 
0090     /**
0091      * Defined by Zend_Filter_Interface
0092      *
0093      * Normalizes the given input
0094      *
0095      * @param  string $value Value to normalized
0096      * @return string|array The normalized value
0097      */
0098     public function filter($value)
0099     {
0100         if (Zend_Locale_Format::isNumber($value, $this->_options)) {
0101             return Zend_Locale_Format::getNumber($value, $this->_options);
0102         } else if (($this->_options['date_format'] === null) && (strpos($value, ':') !== false)) {
0103             // Special case, no date format specified, detect time input
0104             return Zend_Locale_Format::getTime($value, $this->_options);
0105         } else if (Zend_Locale_Format::checkDateFormat($value, $this->_options)) {
0106             // Detect date or time input
0107             return Zend_Locale_Format::getDate($value, $this->_options);
0108         }
0109 
0110         return $value;
0111     }
0112 }