Erbureth

Untitled

May 10th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. /**
  2.  * @brief Base class for loading every module
  3.  *
  4.  * Handles automatic loading of properties, provides interface to
  5.  * verify values
  6.  * NEVER instantiate this class directly
  7.  **/
  8. class Loadable {
  9.     protected $init = false;
  10.     protected $required = array();
  11.     protected $id;
  12.     protected $columns = array();
  13.     protected $columntypes = array();
  14.     protected $tablename;
  15.     protected $dataobject;
  16.     protected $db;
  17.  
  18.     public function __construct($firstparam = null, $connection = null) {
  19.         if ($connection === null) {
  20.             $connection = "default";
  21.         }
  22.         $this->db = \yadaal\GetConnection($connection);
  23.         if (is_null($firstparam)) {
  24.             return;
  25.         }
  26.         $this->init = true;
  27.         if (is_array($firstparam)) {
  28.             $this->insert($firstparam);
  29.             return;
  30.         }
  31.         else if(is_int($firstparam) || is_string($firstparam)) {
  32.             $this->init($firstparam);
  33.             return;
  34.         }
  35.     }
  36.  
  37.     public function insert(array $data) {
  38.         $table = $this->db->Table($this->tablename);
  39.         $cont = true;
  40.         foreach($required as $colname) {
  41.             if (!isset($data[$colname])) {
  42.                 $cont = false;
  43.             }
  44.         }
  45.         $id = $table->insert($data);
  46.         $this->init($id);
  47.     }
  48.  
  49.     public function init($id) {
  50.         $table = $this->db->Table($this->tablename);
  51.         $idcol = $this->id;
  52.         $table->$idcol = $id;
  53.         if ($table->numRows == 0) {
  54.             $this->init = false;
  55.             return;
  56.         }
  57.         $this->dataobject = $table->result();
  58.     }
  59.  
  60.     public function __get($name) {
  61.         return $dataobject->$name;
  62.     }
  63.  
  64.     public function __set($name, $value) {
  65.         if ($this->validate($name, $value)) {
  66.             $this->dataobject->$name = $this->transform($name, $value);
  67.         }
  68.     }
  69.  
  70.     public function validate($name, $value) {
  71.         return true;
  72.     }
  73.  
  74.     public function transform($name, $value) {
  75.         return $value;
  76.     }
  77.  
  78.     public function isEqual($name, $value) {
  79.         return $this->__get($name) == $value;
  80.     }
  81.  
  82.     public function isStrictlyEqual($name, $value) {
  83.         return $this->__get($name) === $value;
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment