Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class PromotionCode
- {
- private $promoCode;
- private $idAccount;
- const INVALID_CODE = 'The code you entered is invalid.';
- /**
- * Construct object with idAccount and user entered promotional code.
- *
- * @idAccount (int)
- * @promoCode (string)
- */
- public function __construct($idAccount, $promoCode)
- {
- $this->promoCode = $promoCode;
- $this->idAccount = $idAccount;
- // Do some simple validation prior to running queries.
- if(!$this->simpleValidation()) {
- throw new Exception($this::INVALID_CODE);
- }
- }
- /**
- * Process redemption of promotional code under account.
- */
- public function redeem()
- {
- // ...
- }
- protected function simpleValidation()
- {
- // Fail if code is not 12 characters.
- if(strlen($this->promoCode) <> 12) return false;
- // Fail is code doesn't begin with a letter.
- if(ctype_digit(substr($this->promoCode, 0, 1))) return false;
- return true;
- }
- }
- try {
- $redeem = new PromotionCode(30, "MN45QZ8HT9PN");
- $redeem->redeem();
- } catch(Exception $e) {
- echo $e->getMessage();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement