Back to index

d-push  2.0
stateobject.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   stateobject.php
00004 * Project   :   Z-Push
00005 * Descr     :   simple data object with some php magic
00006 *
00007 * Created   :   02.01.2012
00008 *
00009 * Copyright 2007 - 2012 Zarafa Deutschland GmbH
00010 *
00011 * This program is free software: you can redistribute it and/or modify
00012 * it under the terms of the GNU Affero General Public License, version 3,
00013 * as published by the Free Software Foundation with the following additional
00014 * term according to sec. 7:
00015 *
00016 * According to sec. 7 of the GNU Affero General Public License, version 3,
00017 * the terms of the AGPL are supplemented with the following terms:
00018 *
00019 * "Zarafa" is a registered trademark of Zarafa B.V.
00020 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00021 * The licensing of the Program under the AGPL does not imply a trademark license.
00022 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00023 *
00024 * However, if you propagate an unmodified version of the Program you are
00025 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00026 * Furthermore you may use our trademarks where it is necessary to indicate
00027 * the intended purpose of a product or service provided you use it in accordance
00028 * with honest practices in industrial or commercial matters.
00029 * If you want to propagate modified versions of the Program under the name "Z-Push",
00030 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00031 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00032 *
00033 * This program is distributed in the hope that it will be useful,
00034 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00036 * GNU Affero General Public License for more details.
00037 *
00038 * You should have received a copy of the GNU Affero General Public License
00039 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00040 *
00041 * Consult LICENSE file for details
00042 ************************************************/
00043 
00044 class StateObject implements Serializable {
00045     private $SO_internalid;
00046     protected $data = array();
00047     protected $unsetdata = array();
00048     protected $changed = false;
00049 
00056     public function GetID() {
00057         if (!isset($this->SO_internalid))
00058             $this->SO_internalid = sprintf('%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
00059 
00060         return $this->SO_internalid;
00061     }
00062 
00069     public function GetDataArray() {
00070         return $this->data;
00071     }
00072 
00082     public function SetDataArray($data, $markAsChanged = false) {
00083         $this->data = $data;
00084         $this->changed = $markAsChanged;
00085     }
00086 
00093     public function IsDataChanged() {
00094         return $this->changed;
00095     }
00096 
00103     public function __set($name, $value) {
00104         $lname = strtolower($name);
00105         if (isset($this->data[$lname]) && is_scalar($value) && !is_array($value) && $this->data[$lname] === $value)
00106             return false;
00107 
00108         $this->data[$lname] = $value;
00109         $this->changed = true;
00110     }
00111 
00120     public function __get($name) {
00121         $lname = strtolower($name);
00122 
00123         if (array_key_exists($lname, $this->data))
00124             return $this->data[$lname];
00125 
00126         if (isset($this->unsetdata) && is_array($this->unsetdata) && array_key_exists($lname, $this->unsetdata))
00127             return $this->unsetdata[$lname];
00128 
00129         return null;
00130     }
00131 
00138     public function __isset($name) {
00139         return isset($this->data[strtolower($name)]);
00140     }
00141 
00148     public function __unset($name) {
00149         if (isset($this->$name)) {
00150             unset($this->data[strtolower($name)]);
00151             $this->changed = true;
00152         }
00153     }
00154 
00163     public function __call($name, $arguments) {
00164         $name = strtolower($name);
00165         $operator = substr($name, 0,3);
00166         $var = substr($name,3);
00167 
00168         if ($operator == "set" && count($arguments) == 1){
00169             $this->$var = $arguments[0];
00170             return true;
00171         }
00172 
00173         if ($operator == "set" && count($arguments) == 2 && $arguments[1] === false){
00174             $this->data[$var] = $arguments[0];
00175             return true;
00176         }
00177 
00178         // getter without argument = return variable, null if not set
00179         if ($operator == "get" && count($arguments) == 0) {
00180             return $this->$var;
00181         }
00182 
00183         // getter with one argument = return variable if set, else the argument
00184         else if ($operator == "get" && count($arguments) == 1) {
00185             if (isset($this->$var)) {
00186                 return $this->$var;
00187             }
00188             else
00189                 return $arguments[0];
00190         }
00191 
00192         if ($operator == "has" && count($arguments) == 0)
00193             return isset($this->$var);
00194 
00195         if ($operator == "del" && count($arguments) == 0) {
00196             unset($this->$var);
00197             return true;
00198         }
00199 
00200         throw new FatalNotImplementedException(sprintf("StateObject->__call('%s'): not implemented. op: {$operator} args:". count($arguments), $name));
00201     }
00202 
00209     public function serialize() {
00210         // perform tasks just before serialization
00211         $this->preSerialize();
00212 
00213         return serialize(array($this->SO_internalid,$this->data));
00214     }
00215 
00223     public function unserialize($data) {
00224         // throw a StateInvalidException if unserialize fails
00225         ini_set('unserialize_callback_func', 'StateObject::ThrowStateInvalidException');
00226 
00227         list($this->SO_internalid, $this->data) = unserialize($data);
00228 
00229         // perform tasks just after unserialization
00230         $this->postUnserialize();
00231         return true;
00232     }
00233 
00240     protected function preSerialize() {
00241         // make sure the object has an id before serialization
00242         $this->GetID();
00243 
00244         return true;
00245     }
00246 
00253     protected function postUnserialize() {
00254         return true;
00255     }
00256 
00263     public static function ThrowStateInvalidException() {
00264         throw new StateInvalidException("Unserialization failed as class was not found or not compatible");
00265     }
00266 }
00267 
00268 ?>