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

Source for file Enum.inc

Documentation is available at Enum.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/exceptions/AlphaException.inc';
  4. require_once $config->get('sysRoot').'alpha/model/types/AlphaType.inc';
  5. require_once $config->get('sysRoot').'alpha/model/types/AlphaTypeInterface.inc';
  6.  
  7. /**
  8.  * The Enum complex data type
  9.  *
  10.  * @package alpha::model::types
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: Enum.inc 1453 2011-12-04 15:12:54Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class Enum extends AlphaType implements AlphaTypeInterface {
  52.     /**
  53.      * An array of valid enum options
  54.      * 
  55.      * @var array 
  56.      * @since 1.0
  57.      */
  58.     private $options;
  59.  
  60.     /**
  61.      * The currently selected enum option
  62.      *
  63.      * @var string 
  64.      * @since 1.0
  65.      */
  66.     private $value '';
  67.  
  68.     /**
  69.      * The message to display to the user when validation fails
  70.      *
  71.      * @var string 
  72.      * @since 1.0
  73.      */
  74.     protected $helper = 'Not a valid enum option!';
  75.  
  76.     /**
  77.      * Constructor that sets up the enum options
  78.      *
  79.      * @param array $opts 
  80.      * @since 1.0
  81.      * @throws IllegalArguementException
  82.      */
  83.     public function __construct($opts=array('')) {
  84.         if(is_array($opts))
  85.             $this->options $opts;
  86.         else
  87.             throw new IllegalArguementException('Not a valid enum option array!');
  88.     }
  89.  
  90.     /**
  91.      * Setter for the enum options
  92.      *
  93.      * @param array $opts 
  94.      * @since 1.0
  95.      * @throws IllegalArguementException
  96.      */
  97.     public function setOptions($opts{
  98.         if(is_array($opts))
  99.             $this->options $opts;
  100.         else
  101.             throw new IllegalArguementException('Not a valid enum option array!');
  102.     }
  103.  
  104.     /**
  105.      * Get the array of enum options
  106.      *
  107.      * @param boolean $alphaSort Set to true if you want the Enum options in alphabetical order (default false)
  108.      * @return array 
  109.      * @since 1.0
  110.      */
  111.     public function getOptions($alphaSort false{
  112.         if($alphaSort)
  113.             sort($this->optionsSORT_STRING);
  114.         return $this->options;
  115.     }    
  116.  
  117.     /**
  118.      * Used to get the current enum item
  119.      *
  120.      * @return string 
  121.      * @since 1.0
  122.      */
  123.     public function getValue({
  124.         return $this->value;
  125.     }
  126.  
  127.     /**
  128.      * Used to select the current enum item
  129.      *
  130.      * @param string $item The item to set as selected in the Enum
  131.      * @since 1.0
  132.      * @throws IllegalArguementException
  133.      */
  134.     public function setValue($item{
  135.         if (in_array($item$this->options)) {
  136.             $this->value $item;
  137.         }else{
  138.             throw new IllegalArguementException($this->getHelper());
  139.         }
  140.     }
  141. }
  142.  
  143. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:51 +0000 by phpDocumentor 1.4.3