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