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

Source for file ViewState.inc

Documentation is available at ViewState.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/exceptions/IllegalArguementException.inc';
  4.  
  5. /**
  6.  * 
  7.  * A singleton class that maintains the view state in the session
  8.  * 
  9.  * @package alpha::view
  10.  * @since 1.0
  11.  * @author John Collins <dev@alphaframework.org>
  12.  * @version $Id: ViewState.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 ViewState {
  51.     /**
  52.      * The name of the last selected tab by the user
  53.      * 
  54.      * @var string 
  55.      * @since 1.0
  56.      */
  57.     protected $selectedTab;
  58.     
  59.     /**
  60.      * The amount of rows to expand the Markdown edit TextBox by
  61.      * 
  62.      * @var string 
  63.      * @since 1.0
  64.      */
  65.     protected $markdownTextBoxRows;
  66.     
  67.     /**
  68.      * The view state object singleton
  69.      *
  70.      * @var ViewState 
  71.      * @since 1.0
  72.      */
  73.     protected static $instance;
  74.  
  75.     /**
  76.      * Private constructor means the class cannot be instantiated from elsewhere
  77.      * 
  78.      * @since 1.0
  79.      */
  80.     private function __construct ({}
  81.     
  82.     /**
  83.      * Get the ViewState instance.  Loads from $_SESSION if its not already in memory, otherwise
  84.      * a new instance will be returned with empty properties.
  85.      * 
  86.      * @var ViewState 
  87.      * @since 1.0
  88.      */
  89.     public static function getInstance({
  90.         // if we don't already have the object in memory...
  91.         if (!isset(self::$instance)) {
  92.             // load from the session, otherwise return a new object
  93.             if(isset($_SESSION['ViewState'])) {
  94.                 return unserialize($_SESSION['ViewState']);
  95.             }else{
  96.                 self::$instance new ViewState();
  97.                 return self::$instance;
  98.             }
  99.         }else{
  100.             return self::$instance;
  101.         }
  102.     }
  103.     
  104.     /**
  105.      * Get the attribute value indicated by the key
  106.      * 
  107.      * @param string $key 
  108.      * @throws IllegalArguementException
  109.      * @return string 
  110.      * @since 1.0
  111.      */
  112.     public function get($key{
  113.         $attribute new ReflectionProperty(get_class($this)$key);
  114.         
  115.         if($attribute != null)
  116.             return $this->$key;
  117.         else
  118.             throw new IllegalArguementException('The property ['.$key.'] does not exist on the ['.get_class($this).'] class');
  119.     }
  120.     
  121.     /**
  122.      * Sets the attribute value indicated by the key.  The ViewState instance will be serialized and saved back to the $_SESSION.
  123.      * 
  124.      * @param string $key 
  125.      * @param string $value 
  126.      * @throws IllegalArguementException
  127.      * @since 1.0
  128.      */
  129.     public function set($key$value{
  130.         $attribute new ReflectionProperty(get_class($this)$key);
  131.         
  132.         if($attribute != null{
  133.             $this->$key $value;
  134.             $_SESSION[get_class($this)serialize($this);
  135.         }else{
  136.             throw new IllegalArguementException('The property ['.$key.'] does not exist on the ['.get_class($this).'] class');
  137.         }
  138.     }
  139. }
  140.  
  141. ?>

Documentation generated on Thu, 17 Mar 2011 16:45:13 +0000 by phpDocumentor 1.4.3