      1: <?php
      2:
      3: /**
      4:  * A customer sequence datatype, which is stored as a string and is made up of a string prefix
      5:  * and an integer sequence, which is stored in a database.
      6:  *
      7:  * @package alpha::model::types
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: Sequence.inc 1496 2012-02-12 20:32:21Z alphadev $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class Sequence extends AlphaDAO implements AlphaTypeInterface{
     49:     /**
     50:      * The string prefix (must be capital alphabet characters only)
     51:      *
     52:      * @var String
     53:      * @since 1.0
     54:      */
     55:     protected $prefix;
     56:
     57:     /**
     58:      * The integer sequence number incremented for each Sequence value with this prefix
     59:      *
     60:      * @var Integer
     61:      * @since 1.0
     62:      */
     63:     protected $sequence;
     64:
     65:     /**
     66:      * The name of the database table for the class
     67:      *
     68:      * @var string
     69:      * @since 1.0
     70:      */
     71:     const TABLE_NAME = 'Sequence';
     72:
     73:     /**
     74:      * An array of data display labels for the class properties
     75:      *
     76:      * @var array
     77:      * @since 1.0
     78:      */
     79:     protected $dataLabels = array("OID"=>"Sequence ID#","prefix"=>"Sequence prefix","sequence"=>"Sequence number");
     80:
     81:     /**
     82:      * The message to display to the user when validation fails
     83:      *
     84:      * @var string
     85:      * @since 1.0
     86:      */
     87:     protected $helper = 'Not a valid sequence value!';
     88:
     89:     /**
     90:      * The size of the value for the this Sequence
     91:      *
     92:      * @var integer
     93:      * @since 1.0
     94:      */
     95:     protected $size = 255;
     96:
     97:     /**
     98:      * The validation rule for the Sequence type
     99:      *
    100:      * @var string
    101:      * @since 1.0
    102:      */
    103:     protected $validationRule;
    104:
    105:     /**
    106:      * The absolute maximum size of the value for the this Sequence
    107:      *
    108:      * @var integer
    109:      * @since 1.0
    110:      */
    111:     const MAX_SIZE = 255;
    112:
    113:     /**
    114:      * The constructor
    115:      *
    116:      * @since 1.0
    117:      */
    118:     public function __construct() {
    119:         // ensure to call the parent constructor
    120:         parent::__construct();
    121:
    122:         $this->validationRule = AlphaValidator::ALLOW_ALL;
    123:
    124:         $this->sequence = new Integer();
    125:
    126:         $this->prefix = new String();
    127:         $this->prefix->setRule(AlphaValidator::REQUIRED_ALPHA_UPPER);
    128:         $this->prefix->setHelper('Sequence prefix must be uppercase string!');
    129:         $this->markUnique('prefix');
    130:
    131:         $this->markTransient('helper');
    132:         $this->markTransient('validationRule');
    133:         $this->markTransient('size');
    134:     }
    135:
    136:     /**
    137:      * Get the validation rule
    138:      *
    139:      * @return string
    140:      * @since 1.0
    141:      */
    142:     public function getRule() {
    143:         return $this->validationRule;
    144:     }
    145:
    146:     /**
    147:      * Sets the sequence number to be the maximum value matching the prefix in the database
    148:      * plus one.  Note that calling this method increments the maximum value in the database.
    149:      *
    150:      * @since 1.0
    151:      */
    152:     public function setSequenceToNext() {
    153:
    154:         try {
    155:             $this->loadByAttribute('prefix', $this->prefix->getValue());
    156:         }catch (BONotFoundException $e) {
    157:             $this->set('sequence', 0);
    158:         }
    159:
    160:         $this->set('sequence', $this->get('sequence')+1);
    161:         $this->save();
    162:     }
    163:
    164:     /**
    165:      * Getter for the validation helper string
    166:      *
    167:      * @return string
    168:      * @since 1.0
    169:      */
    170:     public function getHelper() {
    171:         return $this->helper;
    172:     }
    173:
    174:     /**
    175:      * Set the validation helper text
    176:      *
    177:      * @param string $helper
    178:      * @since 1.0
    179:      */
    180:     public function setHelper($helper) {
    181:         $this->helper = $helper;
    182:     }
    183:
    184:     /**
    185:      * Used to get the Sequence value as a string
    186:      *
    187:      * @return string
    188:      * @since 1.0
    189:      */
    190:     public function getValue() {
    191:         if($this->prefix->getValue() != '' && $this->sequence->getValue() != 0)
    192:             return $this->prefix->getValue().'-'.$this->sequence->getValue();
    193:         else
    194:             return '';
    195:     }
    196:
    197:     /**
    198:      * Accepts a string to set the Sequence prefix/sequence values to, in the
    199:      * format PREFIX-00000000000
    200:      *
    201:      * @param string $val
    202:      * @since 1.0
    203:      * @throws IllegalArguementException
    204:      */
    205:     public function setValue($val) {
    206:         if (strlen($val) <= $this->size) {
    207:             if(!empty($val)) {
    208:                 if(!AlphaValidator::isSequence($val))
    209:                     throw new IllegalArguementException($this->helper);
    210:
    211:                 $parts = explode('-', $val);
    212:                 $this->prefix->setValue($parts[0]);
    213:                 $this->sequence->setValue($parts[1]);
    214:             }
    215:         }else{
    216:             throw new IllegalArguementException($this->helper);
    217:         }
    218:     }
    219:
    220:     /**
    221:      * Get the allowable size of the Sequence in the database field
    222:      *
    223:      * @return integer
    224:      * @since 1.0
    225:      */
    226:     public function getSize(){
    227:         return $this->size;
    228:     }
    229:
    230:     /**
    231:      * Used to convert the object to a printable string
    232:      *
    233:      * @return string
    234:      * @since 1.0
    235:      */
    236:     public function __toString() {
    237:         return $this->prefix->getValue().'-'.$this->sequence->getValue();
    238:     }
    239: }
    240:
    241: ?>