Advertisement
Guest User

Untitled

a guest
Oct 16th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1.  
  2. abstract class FileARModel
  3. {
  4.     private $dataFile;
  5.    
  6.     protected function __construct()
  7.     {
  8.         $this->dataFile = get_class($this) . '.txt';
  9.     }
  10.    
  11.     protected function save()
  12.     {
  13.         file_put_contents($this->dataFile, serialize($this));
  14.     }
  15.    
  16.     public static function loadById($id)
  17.     {
  18.         $allData = file($this->dataFile);
  19.         foreach($allData as $line)
  20.         {
  21.             $user = unserialize($line);
  22.             if($user->getId() == $id)
  23.                 return $user;
  24.         }
  25.        
  26.         return null;
  27.     }
  28. }
  29.  
  30. class User extends FileARModel
  31. {
  32.     private $id = null;
  33.     private $name;
  34.    
  35.     public function getId()
  36.     {
  37.         return $this->id;
  38.     }
  39.    
  40.     public function getName()
  41.     {
  42.         return $this->id;
  43.     }
  44.    
  45.     public function getName()
  46.     {
  47.         return $this->name;
  48.     }
  49.    
  50.     public function setName($newName)
  51.     {
  52.         if($this->validateName($newName))
  53.             $this->name = $newName;
  54.         else
  55.             throw new \Exception('Invalid name');
  56.     }
  57.    
  58.     private function validateName($name)
  59.     {
  60.         return preg_match('#[a-z]*#iu', $name);
  61.     }
  62.    
  63.     public function setId($id)
  64.     {
  65.         $id = intval($id);
  66.        
  67.         if($id <= 999999 || >= 999999999)
  68.             throw new \Exception('invalid id');
  69.            
  70.         $this->id = $id;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement