Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/Barcode.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026 
00030 require_once 'Zend/Loader.php';
00031 
00038 class Zend_Validate_Barcode extends Zend_Validate_Abstract
00039 {
00040     const INVALID        = 'barcodeInvalid';
00041     const FAILED         = 'barcodeFailed';
00042     const INVALID_CHARS  = 'barcodeInvalidChars';
00043     const INVALID_LENGTH = 'barcodeInvalidLength';
00044 
00045     protected $_messageTemplates = array(
00046         self::FAILED         => "'%value%' failed checksum validation",
00047         self::INVALID_CHARS  => "'%value%' contains invalid characters",
00048         self::INVALID_LENGTH => "'%value%' should have a length of %length% characters",
00049         self::INVALID        => "Invalid type given, value should be string",
00050     );
00051 
00057     protected $_messageVariables = array(
00058         'length' => '_length'
00059     );
00060 
00066     protected $_length;
00067 
00073     protected $_adapter;
00074 
00083     public function __construct($adapter)
00084     {
00085         if ($adapter instanceof Zend_Config) {
00086             $adapter = $adapter->toArray();
00087         }
00088 
00089         $options  = null;
00090         $checksum = null;
00091         if (is_array($adapter)) {
00092             if (array_key_exists('options', $adapter)) {
00093                 $options = $adapter['options'];
00094             }
00095 
00096             if (array_key_exists('checksum', $adapter)) {
00097                 $checksum = $adapter['checksum'];
00098             }
00099 
00100             if (array_key_exists('adapter', $adapter)) {
00101                 $adapter = $adapter['adapter'];
00102             } else {
00103                 require_once 'Zend/Validate/Exception.php';
00104                 throw new Zend_Validate_Exception("Missing option 'adapter'");
00105             }
00106         }
00107 
00108         $this->setAdapter($adapter, $options);
00109         if ($checksum !== null) {
00110             $this->setChecksum($checksum);
00111         }
00112     }
00113 
00119     public function getAdapter()
00120     {
00121         return $this->_adapter;
00122     }
00123 
00132     public function setAdapter($adapter, $options = null)
00133     {
00134         $adapter = ucfirst(strtolower($adapter));
00135         require_once 'Zend/Loader.php';
00136         if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) {
00137             $adapter = 'Zend_Validate_Barcode_' . $adapter;
00138         }
00139 
00140         if (!class_exists($adapter)) {
00141             Zend_Loader::loadClass($adapter);
00142         }
00143 
00144         $this->_adapter = new $adapter($options);
00145         if (!$this->_adapter instanceof Zend_Validate_Barcode_AdapterInterface) {
00146             require_once 'Zend/Validate/Exception.php';
00147             throw new Zend_Validate_Exception(
00148                 "Adapter " . $adapter . " does not implement Zend_Validate_Barcode_AdapterInterface"
00149             );
00150         }
00151 
00152         return $this;
00153     }
00154 
00160     public function getChecksum()
00161     {
00162         return $this->getAdapter()->getCheck();
00163     }
00164 
00171     public function setChecksum($checksum)
00172     {
00173         $this->getAdapter()->setCheck($checksum);
00174         return $this;
00175     }
00176 
00185     public function isValid($value)
00186     {
00187         if (!is_string($value)) {
00188             $this->_error(self::INVALID);
00189             return false;
00190         }
00191 
00192         $this->_setValue($value);
00193         $adapter       = $this->getAdapter();
00194         $this->_length = $adapter->getLength();
00195         $result        = $adapter->checkLength($value);
00196         if (!$result) {
00197             $this->_error(self::INVALID_LENGTH);
00198             return false;
00199         }
00200 
00201         $result = $adapter->checkChars($value);
00202         if (!$result) {
00203             $this->_error(self::INVALID_CHARS);
00204             return false;
00205         }
00206 
00207         if ($this->getChecksum()) {
00208             $result = $adapter->checksum($value);
00209             if (!$result) {
00210                 $this->_error(self::FAILED);
00211                 return false;
00212             }
00213         }
00214 
00215         return true;
00216     }
00217 }
 All Data Structures Namespaces Files Functions Variables Enumerations