Untitled

mail@pastecode.io avatar
unknown
php
7 months ago
5.9 kB
4
Indexable
Never
<?php

/*******w******** 
    
    Assignment 2
    Name: Ian Chatelain	
    Date: Sept. 15, 2023
    Description: Input Validation

****************/
enum ProvinceCodes: string {
    case AB = 'AB'; // Alberta
    case BC = 'BC'; // British Columbia
    case MB = 'MB'; // Manitoba
    case NB = 'NB'; // New Brunswick
    case NL = 'NL'; // Newfoundland and Labrador
    case NS = 'NS'; // Nova Scotia
    case ON = 'ON'; // Ontario
    case PE = 'PE'; // Prince Edward Island
    case QC = 'QC'; // Quebec
    case SK = 'SK'; // Saskatchewan
    case NT = 'NT'; // Northwest Territories
    case NU = 'NU'; // Nunavut
    case YT = 'YT'; // Yukon
}

class ValidateField {
    protected $sanitizedData;
    protected bool $isValid;
    protected string $fieldName;
    protected bool $required;

    public function __construct(string $fieldName){
        $this->fieldName = $fieldName;
    }

    public function getSanitizedData(){
        return $this->sanitizedData;
    }
    
    public function getIsValid(){
        return $this->isValid;
    }
}

class ValidateQuantityField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = false;
        $this->isValid = false;
        $this->setData();
        $this->setIsValid();
    }

    public function getIsValid(){
        return $this->isValid;
    }

    private function setIsValid(){
        if(is_int($this->sanitizedData)){
            $this->isValid = true;
        }
    }

    private function setData(){
        $this->sanitizedData = filter_input(INPUT_POST, $this->fieldName, FILTER_SANITIZE_NUMBER_INT);
    }
}

// class ValidateEmptyField extends ValidateField {
//     public function __construct($name){
//         parent::__construct($name);
//         $this->required = true;
//         $this->sanitizedData = filter_input(INPUT_POST, $name, FILTER_SANITIZE_NUMBER_INT);
//     }
// }

$errors = [];

$fieldObjects = [
    // Product quantities
    'qty1' => new ValidateQuantityField($_POST['qty1']),
    'qty2' => new ValidateQuantityField($_POST['qty2']),
    'qty3' => new ValidateQuantityField($_POST['qty3']),
    'qty4' => new ValidateQuantityField($_POST['qty4']),
    'qty5' => new ValidateQuantityField($_POST['qty5']),

    // // Shipping information
    // 'fullname',
    // 'address',
    // 'city',
    // 'province',
    // 'postal',
    // 'email',

    // // Payment information
    // 'cardtype',
    // 'cardname',
    // 'month',
    // 'year',
    // 'cardnumber'
];

function validateFields(){
    global $fieldObjects;
    foreach($fieldObjects as $fields => $object){
        if(!$object->getIsValid()){
            $errors[$fields] = $object;
        }
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="thankyou.css">
    <title>Thanks for your order!</title>
</head>
<body>
    <?php if(!validateFields()): ?>
        <table>
            <tbody>
                <tr>
                    <td colspan="4">
                        <h3>Address Information</h3>
                    </td>
                </tr>
                <tr>
                    <td class="alignright">
                        <span class="bold">Address:</span>
                    </td>
                    <td></td>
                    <td class="alignright">
                        <span class="bold">City:</span>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td class="alignright">
                        <span class="bold">Province:</span>
                    </td>
                    <td></td>
                    <td class="alignright">
                        <span class="bold">Postal Code:</span>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td colspan="2" class="alignright">
                        <span class="bold">Email:</span>
                    </td>
                    <td colspan="2"></td>
                </tr>
            </tbody>
        </table>

        <table>
            <tbody>
                <tr>
                    <td colspan="3">
                        <h3>Order Information</h3>
                    </td>
                </tr>
                <tr>
                    <td><span class="bold">Quantity</span></td>
                    <td><span class="bold">Description</span></td>
                    <td><span class="bold">Cost</span></td>
                </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td class="alignright"></td>
                    </tr>
                <tr>
                    <td colspan="2" class="alignright"><span class="bold">Totals</span></td>
                    <td class="alignright">
                        <span class="bold">$ 446497.65</span>
                    </td>
                </tr>
            </tbody>
        </table>
    <?php else: ?>
        <table>
            <tbody>
                <tr>
                    <td>
                        <h1>The following fields were invalid:</h1>
                    </td>
                </tr>
                <tr>
                    <td>
                        <ul>
                        <?php print_r($fieldObjects) ?>
                        </ul>
                    </td>
                </tr>
            </tbody>
        </table>
    <?php endif ?>
</body>
</html>