Advertisement
Guest User

Redo2

a guest
Apr 29th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2.  
  3. class PromotionCode
  4. {
  5.     private $promoCode;
  6.     private $idAccount;
  7.        
  8.     const INVALID_CODE = 'The code you entered is invalid.';
  9.        
  10.     /**
  11.      * Construct object with idAccount and user entered promotional code.
  12.      *
  13.      * @idAccount (int)
  14.      * @promoCode (string)
  15.     */
  16.     public function __construct($idAccount, $promoCode)
  17.     {
  18.         $this->promoCode = $promoCode;
  19.         $this->idAccount = $idAccount;
  20.        
  21.         // Do some simple validation prior to running queries. 
  22.         if(!$this->simpleValidation()) {
  23.             throw new Exception($this::INVALID_CODE);
  24.         }
  25.     }
  26.    
  27.     /**
  28.      * Process redemption of promotional code under account.
  29.     */
  30.     public function redeem()
  31.     {
  32.         // ...
  33.     }
  34.    
  35.     protected function simpleValidation()
  36.     {
  37.         // Fail if code is not 12 characters.
  38.         if(strlen($this->promoCode) <> 12) return false;
  39.        
  40.         // Fail is code doesn't begin with a letter.
  41.         if(ctype_digit(substr($this->promoCode, 0, 1))) return false;
  42.        
  43.         return true;
  44.     }
  45. }
  46.  
  47. try {
  48.     $redeem = new PromotionCode(30, "MN45QZ8HT9PN");
  49.     $redeem->redeem();
  50. } catch(Exception $e) {
  51.     echo $e->getMessage();
  52. }
  53.  
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement