Guest User

Untitled

a guest
Mar 4th, 2011
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. class Personne
  2. {
  3.     private $nom;
  4.     private $adresse;
  5.     private $tel;
  6.     private $sexe;
  7.  
  8.     public function __construct($nom = '', $adresse = '', $tel = '', $sex = '')
  9.     {
  10.         $this->nom     = $nom;
  11.         $this->adresse = $adresse;
  12.         $this->tel     = $tel;
  13.         $this->sexe    = $sex;
  14.     }
  15.  
  16.      public function toHTML()
  17.      {
  18.          $html = sprintf('<strong>Nom :</strong><em>%s</em><br />',
  19.                  htmlspecialchars($this->nom))
  20.               . sprintf('<strong>Adresse :</strong><em>%s</em><br />',
  21.                   htmlspecialchars($this->adresse))
  22.               . sprintf('<strong>Tel :</strong><em>%s</em><br />',
  23.                   htmlspecialchars($this->tel))
  24.               . sprintf('<strong>Sexe :</strong><em>%s</em><br />',
  25.                   htmlspecialchars($this->sexe));
  26.  
  27.          return $html;
  28.       }
  29.  
  30.      public function formulaire()
  31.      {
  32.          $html = sprintf('<label>Nom:<input type="text" name="nom" value="%s"/></label><br />',
  33.                  htmlspecialchars($this->nom))
  34.                . sprintf('<label>Adresse:<textarea name="adresse" rows="8" cols="45">%s</textarea></label><br />',
  35.                    htmlspecialchars($this->adresse))
  36.                . sprintf('<label>Tel:<input type="text" name="tel" value="%s"/></label><br />',
  37.                    htmlspecialchars($this->tel))
  38.                . sprintf('<input type="radio" name="sexe" value="M" id="masculin" %s/> <label for="masculin">M</label><br />',
  39.                    ($this->sexe === 'M' ? 'checked="checked"' : ''))
  40.               . sprintf('<input type="radio" name="sexe" value="F" id="feminin" %s/> <label for="feminin">F</label><br />',
  41.                   ($this->sexe === 'F' ? 'checked="checked"' : ''));
  42.              
  43.         return $html;
  44.      }
  45.  
  46.      public function extraire_form()
  47.      {        
  48.          foreach($_REQUEST as $key => $value)
  49.          {
  50.             $this->{$key} = $value;
  51.          }
  52.      }
  53.      
  54.      public function valider()
  55.      {
  56.          $this->extraire_form();
  57.          
  58.          foreach(get_object_vars($this) as $key => $value)
  59.          {
  60.              switch($key)
  61.              {
  62.                  case 'nom':
  63.                  case 'adresse':
  64.                      if(empty($value))
  65.                      {
  66.                          return false;
  67.                      }
  68.                      break;
  69.                  case 'tel':
  70.                      if(!preg_match('/\d{3}-\d{3}-\d{4}/', $value))
  71.                      {
  72.                          return false;
  73.                      }
  74.                      break;
  75.                  break;
  76.                  case 'sexe':
  77.                      if($value !== 'M' && $value !== 'F')
  78.                      {
  79.                          return false;
  80.                      }
  81.                      break;
  82.              }
  83.          }
  84.          
  85.          return true;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment