source: trunk/library/Zend/Validate/Barcode.php @ 5146

Revision 5146, 6.2 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus. Library: adicionando arquivos.

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: Barcode.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_Loader
29 */
30require_once 'Zend/Loader.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_Barcode extends Zend_Validate_Abstract
39{
40    const INVALID        = 'barcodeInvalid';
41    const FAILED         = 'barcodeFailed';
42    const INVALID_CHARS  = 'barcodeInvalidChars';
43    const INVALID_LENGTH = 'barcodeInvalidLength';
44
45    protected $_messageTemplates = array(
46        self::FAILED         => "'%value%' failed checksum validation",
47        self::INVALID_CHARS  => "'%value%' contains invalid characters",
48        self::INVALID_LENGTH => "'%value%' should have a length of %length% characters",
49        self::INVALID        => "Invalid type given. String expected",
50    );
51
52    /**
53     * Additional variables available for validation failure messages
54     *
55     * @var array
56     */
57    protected $_messageVariables = array(
58        'length' => '_length'
59    );
60
61    /**
62     * Length for the set subtype
63     *
64     * @var integer
65     */
66    protected $_length;
67
68    /**
69     * Barcode adapter
70     *
71     * @var Zend_Validate_Barcode_BarcodeAdapter
72     */
73    protected $_adapter;
74
75    /**
76     * Generates the standard validator object
77     *
78     * @param  string|Zend_Config|
79     *         Zend_Validate_Barcode_BarcodeAdapter $adapter Barcode adapter to use
80     * @return void
81     * @throws Zend_Validate_Exception
82     */
83    public function __construct($adapter)
84    {
85        if ($adapter instanceof Zend_Config) {
86            $adapter = $adapter->toArray();
87        }
88
89        $options  = null;
90        $checksum = null;
91        if (is_array($adapter)) {
92            if (array_key_exists('options', $adapter)) {
93                $options = $adapter['options'];
94            }
95
96            if (array_key_exists('checksum', $adapter)) {
97                $checksum = $adapter['checksum'];
98            }
99
100            if (array_key_exists('adapter', $adapter)) {
101                $adapter = $adapter['adapter'];
102            } else {
103                require_once 'Zend/Validate/Exception.php';
104                throw new Zend_Validate_Exception("Missing option 'adapter'");
105            }
106        }
107
108        $this->setAdapter($adapter, $options);
109        if ($checksum !== null) {
110            $this->setChecksum($checksum);
111        }
112    }
113
114    /**
115     * Returns the set adapter
116     *
117     * @return Zend_Validate_Barcode_BarcodeAdapter
118     */
119    public function getAdapter()
120    {
121        return $this->_adapter;
122    }
123
124    /**
125     * Sets a new barcode adapter
126     *
127     * @param  string|Zend_Validate_Barcode $adapter Barcode adapter to use
128     * @param  array  $options Options for this adapter
129     * @return void
130     * @throws Zend_Validate_Exception
131     */
132    public function setAdapter($adapter, $options = null)
133    {
134        $adapter = ucfirst(strtolower($adapter));
135        require_once 'Zend/Loader.php';
136        if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) {
137            $adapter = 'Zend_Validate_Barcode_' . $adapter;
138        }
139
140        if (!class_exists($adapter)) {
141            Zend_Loader::loadClass($adapter);
142        }
143
144        $this->_adapter = new $adapter($options);
145        if (!$this->_adapter instanceof Zend_Validate_Barcode_AdapterInterface) {
146            require_once 'Zend/Validate/Exception.php';
147            throw new Zend_Validate_Exception(
148                "Adapter " . $adapter . " does not implement Zend_Validate_Barcode_AdapterInterface"
149            );
150        }
151
152        return $this;
153    }
154
155    /**
156     * Returns the checksum option
157     *
158     * @return boolean
159     */
160    public function getChecksum()
161    {
162        return $this->getAdapter()->getCheck();
163    }
164
165    /**
166     * Sets the checksum option
167     *
168     * @param  boolean $checksum
169     * @return Zend_Validate_Barcode
170     */
171    public function setChecksum($checksum)
172    {
173        $this->getAdapter()->setCheck($checksum);
174        return $this;
175    }
176
177    /**
178     * Defined by Zend_Validate_Interface
179     *
180     * Returns true if and only if $value contains a valid barcode
181     *
182     * @param  string $value
183     * @return boolean
184     */
185    public function isValid($value)
186    {
187        if (!is_string($value)) {
188            $this->_error(self::INVALID);
189            return false;
190        }
191
192        $this->_setValue($value);
193        $adapter       = $this->getAdapter();
194        $this->_length = $adapter->getLength();
195        $result        = $adapter->checkLength($value);
196        if (!$result) {
197            if (is_array($this->_length)) {
198                $temp = $this->_length;
199                $this->_length = "";
200                foreach($temp as $length) {
201                    $this->_length .= "/";
202                    $this->_length .= $length;
203                }
204
205                $this->_length = substr($this->_length, 1);
206            }
207
208            $this->_error(self::INVALID_LENGTH);
209            return false;
210        }
211
212        $result = $adapter->checkChars($value);
213        if (!$result) {
214            $this->_error(self::INVALID_CHARS);
215            return false;
216        }
217
218        if ($this->getChecksum()) {
219            $result = $adapter->checksum($value);
220            if (!$result) {
221                $this->_error(self::FAILED);
222                return false;
223            }
224        }
225
226        return true;
227    }
228}
Note: See TracBrowser for help on using the repository browser.