Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • Boolean
  • Date
  • DEnum
  • DEnumItem
  • Double
  • Enum
  • Integer
  • Relation
  • RelationLookup
  • Sequence
  • String
  • Text
  • Timestamp
  • Type

Interfaces

  • TypeInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Alpha\Model\Type;
  4: 
  5: use Alpha\Exception\IllegalArguementException;
  6: 
  7: /**
  8:  * The Enum complex data type.
  9:  *
 10:  * @since 1.0
 11:  *
 12:  * @author John Collins <dev@alphaframework.org>
 13:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 14:  * @copyright Copyright (c) 2015, 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: class Enum extends Type implements TypeInterface
 50: {
 51:     /**
 52:      * An array of valid enum options.
 53:      *
 54:      * @var array
 55:      *
 56:      * @since 1.0
 57:      */
 58:     private $options;
 59: 
 60:     /**
 61:      * The currently selected enum option.
 62:      *
 63:      * @var string
 64:      *
 65:      * @since 1.0
 66:      */
 67:     private $value = '';
 68: 
 69:     /**
 70:      * The message to display to the user when validation fails.
 71:      *
 72:      * @var string
 73:      *
 74:      * @since 1.0
 75:      */
 76:     protected $helper = 'Not a valid enum option!';
 77: 
 78:     /**
 79:      * Constructor that sets up the enum options.
 80:      *
 81:      * @param array $opts
 82:      *
 83:      * @since 1.0
 84:      *
 85:      * @throws Alpha\Exception\IllegalArguementException
 86:      */
 87:     public function __construct($opts = array(''))
 88:     {
 89:         if (is_array($opts)) {
 90:             $this->options = $opts;
 91:         } else {
 92:             throw new IllegalArguementException('Not a valid enum option array!');
 93:         }
 94:     }
 95: 
 96:     /**
 97:      * Setter for the enum options.
 98:      *
 99:      * @param array $opts
100:      *
101:      * @since 1.0
102:      *
103:      * @throws Alpha\Exception\IllegalArguementException
104:      */
105:     public function setOptions($opts)
106:     {
107:         if (is_array($opts)) {
108:             $this->options = $opts;
109:         } else {
110:             throw new IllegalArguementException('Not a valid enum option array!');
111:         }
112:     }
113: 
114:     /**
115:      * Get the array of enum options.
116:      *
117:      * @param bool $alphaSort Set to true if you want the Enum options in alphabetical order (default false)
118:      *
119:      * @return array
120:      *
121:      * @since 1.0
122:      */
123:     public function getOptions($alphaSort = false)
124:     {
125:         if ($alphaSort) {
126:             sort($this->options, SORT_STRING);
127:         }
128: 
129:         return $this->options;
130:     }
131: 
132:     /**
133:      * Used to get the current enum item.
134:      *
135:      * @return string
136:      *
137:      * @since 1.0
138:      */
139:     public function getValue()
140:     {
141:         return $this->value;
142:     }
143: 
144:     /**
145:      * Used to select the current enum item.
146:      *
147:      * @param string $item The item to set as selected in the Enum
148:      *
149:      * @since 1.0
150:      *
151:      * @throws Alpha\Exception\IllegalArguementException
152:      */
153:     public function setValue($item)
154:     {
155:         if (in_array($item, $this->options)) {
156:             $this->value = $item;
157:         } else {
158:             throw new IllegalArguementException($this->getHelper());
159:         }
160:     }
161: }
162: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0