Alpha Framework alpha--model--types
[ class tree: alpha--model--types ] [ index: alpha--model--types ] [ all elements ]

Source for file Sequence.inc

Documentation is available at Sequence.inc

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

Documentation generated on Thu, 17 Mar 2011 16:44:49 +0000 by phpDocumentor 1.4.3