# Ternary.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2004-2007 by Konstantin V. Arkhipov                     *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00018     final class Ternary implements Stringable
    00019     {
    00020         private $trinity = null;    // ;-)
    00021
    00022         public function __construct($boolean = null)
    00023         {
    00024             return $this->setValue($boolean);
    00025         }
    00026
    00030         public static function create($boolean = null)
    00031         {
    00032             return new self($boolean);
    00033         }
    00034
    00038         public static function spawn($value, $true, $false, $null = null)
    00039         {
    00040             if ($value === $true)
    00041                 return new Ternary(true);
    00042             elseif ($value === $false)
    00043                 return new Ternary(false);
    00044             elseif (($value === $null) || ($null === null))
    00045                 return new Ternary(null);
    00046             else /* if ($value !== $null && $null !== null) or anything else */
    00047                 throw new WrongArgumentException(
    00048                     "failed to spawn Ternary from '{$value}' switching on ".
    00049                     "'{$true}', '{$false}' and '{$null}'"
    00050                 );
    00051         }
    00052
    00053         public function isNull()
    00054         {
    00055             return (null === $this->trinity);
    00056         }
    00057
    00058         public function isTrue()
    00059         {
    00060             return (true === $this->trinity);
    00061         }
    00062
    00063         public function isFalse()
    00064         {
    00065             return (false === $this->trinity);
    00066         }
    00067
    00071         public function setNull()
    00072         {
    00073             $this->trinity = null;
    00074
    00075             return $this;
    00076         }
    00077
    00081         public function setTrue()
    00082         {
    00083             $this->trinity = true;
    00084
    00085             return $this;
    00086         }
    00087
    00091         public function setFalse()
    00092         {
    00093             $this->trinity = false;
    00094
    00095             return $this;
    00096         }
    00097
    00098         public function getValue()
    00099         {
    00100             return $this->trinity;
    00101         }
    00102
    00106         public function setValue($boolean = null)
    00107         {
    00108             Assert::isTernaryBase($boolean);
    00109
    00110             $this->trinity = $boolean;
    00111
    00112             return $this;
    00113         }
    00114
    00115         public function decide($true, $false, $null = null)
    00116         {
    00117             if ($this->trinity === true)
    00118                 return $true;
    00119             elseif ($this->trinity === false)
    00120                 return $false;
    00121             elseif ($this->trinity === null)
    00122                 return $null;
    00123
    00124             throw new WrongStateException(
    00125                 'mama, weer all crazee now!' // (c) Slade
    00126             );
    00127         }
    00128
    00129         public function toString()
    00130         {
    00131             return $this->decide('true', 'false', 'null');
    00132         }
    00133     }
    00134 ?>