RapidMod

RapidmodDataModel

Mar 12th, 2018
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author RapidMod.io
  4.  * @author 813.330.0522
  5.  */
  6.  
  7.  
  8. namespace Rapidmod\Data;
  9. use \stdClass;
  10.  
  11. class Model
  12. {
  13.     const VERSION = "0.0.3";
  14.  
  15.     private $_DATAEXTRACT = NULL;
  16.     private $_DATAFORMAT = NULL;
  17.     private $_DATAOBJECT = NULL;
  18.     private $_DATASANITIZE = NULL;
  19.     private $_DATAVALIDATE = NULL;
  20.  
  21.  
  22.     protected function _dataObject(){
  23.         if(is_null($this->_DATAOBJECT)){
  24.             $this->_DATAOBJECT = new \stdClass();
  25.         }
  26.         return $this->_DATAOBJECT;
  27.     }
  28.  
  29.     public function _get($key){
  30.         if(isset($this->_dataObject()->{$key})){
  31.             return $this->_dataObject()->{$key};
  32.         }
  33.         return false;
  34.     }
  35.  
  36.     public function __get ( $key ){
  37.         if(isset($this->_dataObject()->{$key})){
  38.             return $this->_dataObject()->{$key};
  39.         }
  40.         if (method_exists($this, "get_{$key}")){
  41.             $x = "get_{$key}";
  42.             $this->_dataObject()->{$key} = $this->{$x}();
  43.         }elseif (method_exists($this, $key)){
  44.             $this->_dataObject()->{$key} =  $this->{$key}();
  45.         }elseif (property_exists($this,$key)){
  46.             $this->_dataObject()->{$key} = $this->{$key};
  47.         }
  48.         if(!isset($this->_dataObject()->{$key})){
  49.             $this->_dataObject()->{$key} = false;
  50.         }
  51.         return $this->_get($key);
  52.  
  53.     }
  54.  
  55.     /**
  56.      *
  57.      * Name _set
  58.      * @param $key
  59.      * @param $value
  60.      * @return $this
  61.      *
  62.      * @author RapidMod.io
  63.      * @author 813.330.0522
  64.      * @WTF magic methods blow balls.......
  65.      */
  66.     public function _set($key,$value){
  67.         if (method_exists($this, "set_{$key}")){
  68.             $x = "set_{$key}";
  69.             $value =  $this->{$x}($value);
  70.         }elseif (method_exists($this, $key)){
  71.             $value =  $this->{$key}($value);
  72.         }
  73.         $this->_dataObject()->{$key} = $value;
  74.         return $this;
  75.     }
  76.  
  77.     public function  __set($key,$value=""){
  78.         return $this->_set($key,$value);
  79.     }
  80.  
  81.     public function appendArray($key,$value){
  82.         $data = $this->_get($key);
  83.         if(!is_array($data)){$data = array();}
  84.         $data[] = $value;
  85.         return $this->_set($key,$data);
  86.     }
  87.  
  88.     public function buildObject($dataArray){
  89.         $this->reset();
  90.         return $this->setData($dataArray);
  91.     }
  92.  
  93.     public function extract(){
  94.         if(is_null($this->_DATAEXTRACT)){
  95.             $this->_DATAEXTRACT = new \Rapidmod\Data\Extract();
  96.         }
  97.         return $this->_DATAEXTRACT;
  98.     }
  99.  
  100.     public function format(){
  101.         if(is_null($this->_DATAFORMAT)){
  102.             $this->_DATAFORMAT = new \Rapidmod\Data\Format();
  103.         }
  104.         return $this->_DATAFORMAT;
  105.     }
  106.  
  107.     public function getData($key=false){
  108.         if(is_array($key) && !empty($key)){
  109.             $data = array();
  110.             foreach($key as $k){
  111.                 $data[$k] = $this->_get($k);
  112.             }
  113.             return $data;
  114.         }elseif(!empty($key) && is_string($key)){
  115.             return $this->_get($key);
  116.         }else{
  117.             return $this->toArray();
  118.         }
  119.     }
  120.  
  121.     public function reset(){
  122.         $this->_DATAOBJECT = new \stdClass();
  123.         return $this;
  124.     }
  125.  
  126.     public function sanitize(){
  127.         if(is_null($this->_DATASANITIZE)){
  128.             $this->_DATASANITIZE = new \Rapidmod\Data\Sanitize();
  129.         }
  130.         return $this->_DATASANITIZE;
  131.     }
  132.  
  133.     public function setData($key,$value = ""){
  134.         if(!is_array($key) && is_string($key)) {
  135.             $this->_set($key,$value);
  136.             return $this;
  137.         }
  138.         if(!empty($key) && empty($value)){
  139.             foreach($key as $k=>$v){
  140.                 $this->_set($k,$v);
  141.             }
  142.         }
  143.         return $this;
  144.     }
  145.  
  146.     public function toArray($dataArray= array()){
  147.         if(!$dataArray){
  148.             $dataArray = $this->_dataObject();
  149.         }
  150.         if(empty($dataArray)){ return array();}
  151.  
  152.         return (array) $this->_dataObject();
  153.         return json_decode(json_encode($dataArray),1);
  154.     }
  155.  
  156.     public function validate(){
  157.         if(is_null($this->_DATAVALIDATE)){
  158.             $this->_DATAVALIDATE = new \Rapidmod\Data\Validate();
  159.         }
  160.         return $this->_DATAVALIDATE;
  161.     }
  162.  
  163.     public static function model($name = NULL,$vars=array()){
  164.         if(is_null($name)){ $name = get_called_class(); }
  165.         return new $name($vars);
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment