~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Validate/Barcode/UpcA.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Zend Framework
 
5
 *
 
6
 * LICENSE
 
7
 *
 
8
 * This source file is subject to the new BSD license that is bundled
 
9
 * with this package in the file LICENSE.txt.
 
10
 * It is also available through the world-wide-web at this URL:
 
11
 * http://framework.zend.com/license/new-bsd
 
12
 * If you did not receive a copy of the license and are unable to
 
13
 * obtain it through the world-wide-web, please send an email
 
14
 * to license@zend.com so we can send you a copy immediately.
 
15
 *
 
16
 * @category   Zend
 
17
 * @package    Zend_Validate
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 * @version    $Id: UpcA.php 8210 2008-02-20 14:09:05Z andries $
 
21
 */
 
22
 
 
23
 
 
24
/**
 
25
 * @see Zend_Validate_Abstract
 
26
 */
 
27
require_once 'Zend/Validate/Abstract.php';
 
28
 
 
29
 
 
30
/**
 
31
 * @category   Zend
 
32
 * @package    Zend_Validate
 
33
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
34
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
35
 */
 
36
class Zend_Validate_Barcode_UpcA extends Zend_Validate_Abstract
 
37
{
 
38
    /**
 
39
     * Validation failure message key for when the value is
 
40
     * an invalid barcode
 
41
     */
 
42
    const INVALID = 'invalid';
 
43
 
 
44
    /**
 
45
     * Validation failure message key for when the value is
 
46
     * not 12 characters long
 
47
     */
 
48
    const INVALID_LENGTH = 'invalidLength';
 
49
 
 
50
    /**
 
51
     * Validation failure message template definitions
 
52
     *
 
53
     * @var array
 
54
     */
 
55
    protected $_messageTemplates = array(
 
56
        self::INVALID        => "'%value%' is an invalid UPC-A barcode",
 
57
        self::INVALID_LENGTH => "'%value%' should be 12 characters",
 
58
    );
 
59
 
 
60
    /**
 
61
     * Defined by Zend_Validate_Interface
 
62
     *
 
63
     * Returns true if and only if $value contains a valid barcode
 
64
     *
 
65
     * @param  string $value
 
66
     * @return boolean
 
67
     */
 
68
    public function isValid($value)
 
69
    {
 
70
        $valueString = (string) $value;
 
71
        $this->_setValue($valueString);
 
72
 
 
73
        if (strlen($valueString) !== 12) {
 
74
            $this->_error(self::INVALID_LENGTH);
 
75
            return false;
 
76
        }
 
77
 
 
78
        $barcode = substr($valueString, 0, -1);
 
79
        $oddSum  = 0;
 
80
        $evenSum = 0;
 
81
 
 
82
        for ($i = 0; $i < 11; $i++) {
 
83
            if ($i % 2 === 0) {
 
84
                $oddSum += $barcode[$i] * 3;
 
85
            } elseif ($i % 2 === 1) {
 
86
                $evenSum += $barcode[$i];
 
87
            }
 
88
        }
 
89
 
 
90
        $calculation = ($oddSum + $evenSum) % 10;
 
91
        $checksum    = ($calculation === 0) ? 0 : 10 - $calculation;
 
92
 
 
93
        if ($valueString[11] != $checksum) {
 
94
            $this->_error(self::INVALID);
 
95
            return false;
 
96
        }
 
97
 
 
98
        return true;
 
99
    }
 
100
}