Untitled

mail@pastecode.io avatar
unknown
php
a year ago
6.9 kB
2
Indexable
Never
<?php

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

****************/

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

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

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

class ValidateQuantityField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = false;
        $this->sanitizedData = filter_var($_POST[$this->fieldName], FILTER_SANITIZE_NUMBER_INT);
        $this->setIsValid();
    }

    private function setIsValid(){
        if(trim($_POST[$this->fieldName]) === ""){
            $this->isValid = true;
        }
        if(is_numeric($this->sanitizedData)){
            $this->isValid = true;
        }
    }
}

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

    private function setIsValid(){
        if(trim($_POST[$this->fieldName]) !== ""){
            $this->isValid = true;
        }
    }
}

class ValidateProvinceField extends ValidateField {
    private array $provinceCodes = [
        'AB', // Alberta
        'BC', // British Columbia
        'MB', // Manitoba
        'NB', // New Brunswick
        'NL', // Newfoundland and Labrador
        'NS', // Nova Scotia
        'ON', // Ontario
        'PE', // Prince Edward Island
        'QC', // Quebec
        'SK', // Saskatchewan
        'NT', // Northwest Territories
        'NU', // Nunavut
        'YT'  // Yukon
    ];

    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->sanitizedData = filter_input(INPUT_POST, $this->fieldName, FILTER_SANITIZE_STRING);
        $this->setIsValid();
    }

    private function setIsValid(){
        if(in_array($_POST[$this->fieldName], $this->provinceCodes)){
            $this->isValid = true;
        }
    }
}

class ValidatePostalField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = true;
        $this->sanitizedData = filter_input(INPUT_POST, $this->fieldName, FILTER_SANITIZE_STRING);
        $this->setIsValid();
    }

    private function setIsValid(){
        if(filter_var($this->sanitizedData, FILTER_VALIDATE_REGEXP, array(
            "options" => array("regexp"=>"/^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/")))){
                $this->isValid = true;
            }
    }
}

class ValidateEmailField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = true;
        $this->sanitizedData = filter_input(INPUT_POST, $this->fieldName, FILTER_SANITIZE_STRING);
        $this->setIsValid();
    }

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

class ValidateCardTypeField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = true;
        $this->sanitizedData = filter_input(INPUT_POST, $this->fieldName, FILTER_SANITIZE_STRING);
        $this->setIsValid();
    }

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

class ValidateMonthField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = true;
        $this->sanitizedData = filter_var($_POST[$this->fieldName], FILTER_SANITIZE_NUMBER_INT);
        $this->setIsValid();
    }

    private function setIsValid(){
        if(filter_var($this->sanitizedData, FILTER_VALIDATE_INT, array("options" => array("min_range" => 1, "max_range" => 12)))){
            $this->isValid = true;
        }
    }
}

class ValidateYearField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = true;
        $this->sanitizedData = filter_var($_POST[$this->fieldName], FILTER_SANITIZE_STRING);
        $this->setIsValid();
    }

    private function setIsValid(){
        if($this->sanitizedData >= date("Y") && $this->sanitizedData < (date("Y") + 5)){
            $this->isValid = true;
        }
    }
}

class ValidateCardNumberField extends ValidateField {
    public function __construct($fieldName){
        parent::__construct($fieldName);
        $this->required = true;
        $this->sanitizedData = filter_var($_POST[$this->fieldName], FILTER_SANITIZE_NUMBER_INT);
        $this->setIsValid();
    }

    private function setIsValid(){
        if(filter_var($this->sanitizedData, FILTER_VALIDATE_INT) && 
          (strlen((string)$this->sanitizedData) == 10) && 
           is_numeric($this->sanitizedData)){
           $this->isValid = true;
        }
    }
}


$errors = [];

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

    // Shipping information
    'fullname' => new ValidateEmptyField('fullname'),
    'address' => new ValidateEmptyField('address'),
    'city' => new ValidateEmptyField('city'),
    'province' => new ValidateProvinceField('province'),
    'postal' => new ValidatePostalField('postal'),
    'email' => new ValidateEmailField('email'),

    // // Payment information
    'cardtype' => new ValidateCardTypeField('cardtype'),
    'cardname' => new ValidateEmptyField('cardname'),
    'month' => new ValidateMonthField('month'),
    'year' => new ValidateYearField('year'),
    'cardnumber' => new ValidateCardNumberField('cardnumber')
];

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

?>