Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Personne
- {
- private $nom;
- private $adresse;
- private $tel;
- private $sexe;
- public function __construct($nom = '', $adresse = '', $tel = '', $sex = '')
- {
- $this->nom = $nom;
- $this->adresse = $adresse;
- $this->tel = $tel;
- $this->sexe = $sex;
- }
- public function toHTML()
- {
- $html = sprintf('<strong>Nom :</strong><em>%s</em><br />',
- htmlspecialchars($this->nom))
- . sprintf('<strong>Adresse :</strong><em>%s</em><br />',
- htmlspecialchars($this->adresse))
- . sprintf('<strong>Tel :</strong><em>%s</em><br />',
- htmlspecialchars($this->tel))
- . sprintf('<strong>Sexe :</strong><em>%s</em><br />',
- htmlspecialchars($this->sexe));
- return $html;
- }
- public function formulaire()
- {
- $html = sprintf('<label>Nom:<input type="text" name="nom" value="%s"/></label><br />',
- htmlspecialchars($this->nom))
- . sprintf('<label>Adresse:<textarea name="adresse" rows="8" cols="45">%s</textarea></label><br />',
- htmlspecialchars($this->adresse))
- . sprintf('<label>Tel:<input type="text" name="tel" value="%s"/></label><br />',
- htmlspecialchars($this->tel))
- . sprintf('<input type="radio" name="sexe" value="M" id="masculin" %s/> <label for="masculin">M</label><br />',
- ($this->sexe === 'M' ? 'checked="checked"' : ''))
- . sprintf('<input type="radio" name="sexe" value="F" id="feminin" %s/> <label for="feminin">F</label><br />',
- ($this->sexe === 'F' ? 'checked="checked"' : ''));
- return $html;
- }
- public function extraire_form()
- {
- foreach($_REQUEST as $key => $value)
- {
- $this->{$key} = $value;
- }
- }
- public function valider()
- {
- $this->extraire_form();
- foreach(get_object_vars($this) as $key => $value)
- {
- switch($key)
- {
- case 'nom':
- case 'adresse':
- if(empty($value))
- {
- return false;
- }
- break;
- case 'tel':
- if(!preg_match('/\d{3}-\d{3}-\d{4}/', $value))
- {
- return false;
- }
- break;
- break;
- case 'sexe':
- if($value !== 'M' && $value !== 'F')
- {
- return false;
- }
- break;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment