Advertisement
Guest User

Zaawansowany PHP dzien 1

a guest
Dec 5th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.72 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class User
  4. {
  5.     protected $username;
  6.     protected $password;
  7.  
  8.     abstract function checkLogin($username, $password);
  9.  
  10.     abstract function setLogin($username);
  11.  
  12.     abstract function setPassword($password);
  13.  
  14.     final public function login($username, $password){
  15.         return $this->checkLogin($username,$password);
  16.     }
  17. }
  18.  
  19. class Client extends User {
  20.  
  21.     function checkLogin($username, $password)
  22.     {
  23.         if ($password != $this->password) return false;
  24.         if ($username != $this->username) return false;
  25.         return true;
  26.     }
  27.  
  28.     function setLogin($username)
  29.     {
  30.         $this->username = $username;
  31.     }
  32.  
  33.     function setPassword($password)
  34.     {
  35.         if ( strlen($password) < 8 ) return false;
  36.         $this->password = $password;
  37.     }
  38.  
  39.     function getPassword(){
  40.         return $this->password;
  41.     }
  42.  
  43.     public function getUsername()
  44.     {
  45.         return $this->username;
  46.     }
  47. }
  48.  
  49. class Admin extends User {
  50.  
  51.     private $ip;
  52.  
  53.     function checkLogin($username, $password)
  54.     {
  55.         $this->ip = "192.168.33.22";
  56.         if ($password != $this->password) return false;
  57.         if ($username != $this->username) return false;
  58.         return true;
  59.     }
  60.  
  61.     function setLogin($username)
  62.     {
  63.         $this->username = $username;
  64.     }
  65.  
  66.     function setPassword($password)
  67.     {
  68.         if ( strlen($password) < 9 ) return false;
  69.         $this->password = $password;
  70.     }
  71. }
  72.  
  73. class UserSet implements Iterator {
  74.     private $users = array();
  75.     private $index = 0;
  76.    
  77.     public function add($user){
  78.         $this->users[] = $user;
  79.     }
  80.  
  81.     public function current()
  82.     {
  83.         return $this->users[$this->index];
  84.     }
  85.  
  86.     public function next()
  87.     {
  88.         $this->index++;
  89.     }
  90.  
  91.     public function key()
  92.     {
  93.     }
  94.  
  95.     public function valid()
  96.     {
  97.         if ($this->index == count($this->users)) return false;
  98.         return true;
  99.     }
  100.  
  101.     public function rewind()
  102.     {
  103.         $this->index=0;
  104.     }
  105.  
  106.     public function checkLogin($password){
  107.         foreach ($this as $user){
  108.             if ($password == $user->getPassword()) echo $user->getUserName()."\n";
  109.         }
  110.     }
  111. }
  112.  
  113. $admin = new Admin();
  114. $admin->setLogin("admin");
  115. $admin->setPassword("testowiecsda");
  116. if ($admin->login("admin","testowiecs")){
  117.     echo "brawo";
  118. }
  119.  
  120. $userSet = new UserSet();
  121. $c1 = new Client();
  122. $c1->setLogin("marek");
  123. $c1->setPassword("zegarek");
  124.  
  125. $c2 = new Client();
  126. $c2->setLogin("arek");
  127. $c2->setPassword("figlarek");
  128.  
  129. $c3 = new Client();
  130. $c3->setLogin("test");
  131. $c3->setPassword("testowski");
  132.  
  133. $userSet->add($c1);
  134. $userSet->add($c2);
  135. $userSet->add($c3);
  136.  
  137. $userSet->checkLogin("figlarek");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement