Advertisement
linuxyamigos

Untitled

Dec 27th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. <?php
  2.  
  3. class Model
  4. {
  5.     public $data  = [];
  6.     public $index = 5;
  7.     public $accesors = [];
  8.    
  9.     function __construct($db = NULL){
  10.        
  11.         $this->accesorRegister([
  12.             'password'  => function($pass){ return password_hash($pass, PASSWORD_DEFAULT); }
  13.         ]);
  14.  
  15.         //parent::__construct($db);
  16.     }
  17.    
  18.     function accesorRegister(array $arr){
  19.         $this->accesors = $arr;
  20.     }
  21.    
  22.     function apply($data){
  23.         $accesor_keys = array_keys($this->accesors);
  24.        
  25.         foreach ($data as $k => $dato){
  26.             if (in_array($k, $accesor_keys))
  27.                 $data[$k] = $this->accesors[$k]($dato);
  28.         }
  29.        
  30.         return $data;
  31.     }
  32.    
  33.     function create($data){
  34.         $this->data[++$this->index] = $this->apply($data); 
  35.         return $this->index;
  36.     }
  37.    
  38.     function get($index){
  39.         return $this->data[$index];
  40.     }
  41. }
  42.  
  43. $obj = new Model;
  44. $id  = $obj->create(['nombre' => 'Pablo', 'pais' => 'Argentina', 'password' => '123']);
  45. var_dump(['id' =>$id]);
  46.  
  47. var_dump($obj->get(6));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement