source: sandbox/2.2.0.2/library/Zend/Validate/PostCode.php @ 4456

Revision 4456, 5.9 KB checked in by airton, 13 years ago (diff)

Ticket #1991 - Parametrizacao das buscas LDAP no Expresso Mail - Adicionando arquivos e bibliotecas

  • Property svn:executable set to *
Line 
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_Validate
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: PostCode.php 22668 2010-07-25 14:50:46Z thomas $
20 */
21
22/**
23 * @see Zend_Validate_Abstract
24 */
25require_once 'Zend/Validate/Abstract.php';
26
27/**
28 * @see Zend_Locale_Format
29 */
30require_once 'Zend/Locale/Format.php';
31
32/**
33 * @category   Zend
34 * @package    Zend_Validate
35 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license    http://framework.zend.com/license/new-bsd     New BSD License
37 */
38class Zend_Validate_PostCode extends Zend_Validate_Abstract
39{
40    const INVALID  = 'postcodeInvalid';
41    const NO_MATCH = 'postcodeNoMatch';
42
43    /**
44     * @var array
45     */
46    protected $_messageTemplates = array(
47        self::INVALID  => "Invalid type given. String or integer expected",
48        self::NO_MATCH => "'%value%' does not appear to be a postal code",
49    );
50
51    /**
52     * Locale to use
53     *
54     * @var string
55     */
56    protected $_locale;
57
58    /**
59     * Manual postal code format
60     *
61     * @var unknown_type
62     */
63    protected $_format;
64
65    /**
66     * Constructor for the integer validator
67     *
68     * Accepts either a string locale, a Zend_Locale object, or an array or
69     * Zend_Config object containing the keys "locale" and/or "format".
70     *
71     * @param string|Zend_Locale|array|Zend_Config $options
72     * @throws Zend_Validate_Exception On empty format
73     */
74    public function __construct($options = null)
75    {
76        if ($options instanceof Zend_Config) {
77            $options = $options->toArray();
78        }
79
80        if (empty($options)) {
81            require_once 'Zend/Registry.php';
82            if (Zend_Registry::isRegistered('Zend_Locale')) {
83                $this->setLocale(Zend_Registry::get('Zend_Locale'));
84            }
85        } elseif (is_array($options)) {
86            // Received
87            if (array_key_exists('locale', $options)) {
88                $this->setLocale($options['locale']);
89            }
90
91            if (array_key_exists('format', $options)) {
92                $this->setFormat($options['format']);
93            }
94        } elseif ($options instanceof Zend_Locale || is_string($options)) {
95            // Received Locale object or string locale
96            $this->setLocale($options);
97        }
98
99        $format = $this->getFormat();
100        if (empty($format)) {
101            require_once 'Zend/Validate/Exception.php';
102            throw new Zend_Validate_Exception("A postcode-format string has to be given for validation");
103        }
104    }
105
106    /**
107     * Returns the set locale
108     *
109     * @return string|Zend_Locale The set locale
110     */
111    public function getLocale()
112    {
113        return $this->_locale;
114    }
115
116    /**
117     * Sets the locale to use
118     *
119     * @param string|Zend_Locale $locale
120     * @throws Zend_Validate_Exception On unrecognised region
121     * @throws Zend_Validate_Exception On not detected format
122     * @return Zend_Validate_PostCode  Provides fluid interface
123     */
124    public function setLocale($locale = null)
125    {
126        require_once 'Zend/Locale.php';
127        $this->_locale = Zend_Locale::findLocale($locale);
128        $locale        = new Zend_Locale($this->_locale);
129        $region        = $locale->getRegion();
130        if (empty($region)) {
131            require_once 'Zend/Validate/Exception.php';
132            throw new Zend_Validate_Exception("Unable to detect a region for the locale '$locale'");
133        }
134
135        $format = Zend_Locale::getTranslation(
136            $locale->getRegion(),
137            'postaltoterritory',
138            $this->_locale
139        );
140
141        if (empty($format)) {
142            require_once 'Zend/Validate/Exception.php';
143            throw new Zend_Validate_Exception("Unable to detect a postcode format for the region '{$locale->getRegion()}'");
144        }
145
146        $this->setFormat($format);
147        return $this;
148    }
149
150    /**
151     * Returns the set postal code format
152     *
153     * @return string
154     */
155    public function getFormat()
156    {
157        return $this->_format;
158    }
159
160    /**
161     * Sets a self defined postal format as regex
162     *
163     * @param string $format
164     * @throws Zend_Validate_Exception On empty format
165     * @return Zend_Validate_PostCode  Provides fluid interface
166     */
167    public function setFormat($format)
168    {
169        if (empty($format) || !is_string($format)) {
170            require_once 'Zend/Validate/Exception.php';
171            throw new Zend_Validate_Exception("A postcode-format string has to be given for validation");
172        }
173
174        if ($format[0] !== '/') {
175            $format = '/^' . $format;
176        }
177
178        if ($format[strlen($format) - 1] !== '/') {
179            $format .= '$/';
180        }
181
182        $this->_format = $format;
183        return $this;
184    }
185
186    /**
187     * Defined by Zend_Validate_Interface
188     *
189     * Returns true if and only if $value is a valid postalcode
190     *
191     * @param  string $value
192     * @return boolean
193     */
194    public function isValid($value)
195    {
196        $this->_setValue($value);
197        if (!is_string($value) && !is_int($value)) {
198            $this->_error(self::INVALID);
199            return false;
200        }
201
202        $format = $this->getFormat();
203        if (!preg_match($format, $value)) {
204            $this->_error(self::NO_MATCH);
205            return false;
206        }
207
208        return true;
209    }
210}
Note: See TracBrowser for help on using the repository browser.