PHP OOPでの私の検証クラスの問題

ranoxo9887:

私は、クラスとシングルトンを使用してPHP OOPを経由して登録してlogingシステムを構築しようとしているが、予想通り、私のバリデーションクラスが動作していません。これは、エラーの配列を返しますが、私は、ユーザー名を提出し、空白の他のすべてのフィールドを残している場合のみ1つの項目は、その配列で、これまででその返すか、私はこれがあることを意図しない方法で成功をプリントアウトします。缶誰も私のコード内のエラーを発見します。私は私のクラスが含まれるようにspl_auto_registerを使用していますが、これは問題の一部ではありません。

私Input.class.phpのコード

<?php
  class Input{
      public static function exists($type = 'post'){
          switch($type){
              case 'post':
                  return (!empty($_POST)) ? true : false;
                  break;
              case 'get':
                  return (!empty($_GET)) ? true : false;
                  break;
              default:
                  return false;
                  break;
          }
      }
      public static function get($item){
          if(isset($_POST['$item'])){
              return $_POST['$item'];
          }else if(isset($_GET['$item'])){
              return $_GET['$item'];
              }
              return '';
      }
  }

?>

Validate.class.phpのコード

<?php
                            // ::>> Notes: in every file Change errors -> errs, change error to err
  class Validate{
      private $_passed = false,
              $_errors = array(),
              $_db     = null;
      // public $error;              
      public function __construct(){
          $this->_db = DB::getInstance();
      }
      public function check($source, $items = array()){
          foreach($items as $item => $rules){
              foreach($rules as $rule => $rule_value){
                 // echo "{$item} {$rule} must be {$rule_value}<br>";

                 $value = $source[$item];
                 // ::>> echo $value;
                 if($rule === 'required' && empty($value)){
                     $this->addError("{$item} is required");
                 }else{

                 }
              }
              if(empty($this->_errors)){
                  $this->_passed = true;
              }
              return $this;
          }
      }
      private function addError($error){
          $this->_errors[] = $error;            // I added an Underscore to $this->errors[] = $error as it was not returning anything.
      }
      public function errors(){
          return $this->_errors;
      }
      public function passed(){
          return $this->_passed;
      }
  }
?>

私もここでサニタイズ機能を持っています

<?php

function escape($string){
    return htmlentities($string, ENT_QUOTES, 'UTF-8');
}

必ず私は完全に物事が働いているかを把握OOPを学ぼう日間このいじりが、その急な学習曲線とされていません。

誰もが任意の明らかなエラーを見ることはできますか?

背の高いマジディは略:

あなたがPHP OPPを行う場合のアイデアは、すべてがオブジェクトで可能な限り行う分割することです。理想的には、クラスは非常に基本的でなければなりませんが、それでも私は少し船外に行く傾向にあります。

ここに私のフォーム検証クラスであり、私はこれ以上、それを使用しないでください、プラス私はこれ以上パスワード要件好きではありません。私は簡単なセットパスワードに必要な長さだろうが、それは個人的な好みです。

<?php

namespace Library\FormValidation;

use Library\Database\Database as DB;

class FormValidation {

    private $query;
    private $stmt;
    public $row;
    public $result;
    public $userAvailability = \NULL;
    public $content = \NULL;
    public $validEmail = \NULL;
    public $verifyEmail = \NULL;
    public $validPassword = \NULL;
    public $verifyPassword = \NULL;
    public $data = [];
    public $valid = [];
    public $resultArray = [];
    public $message = '';

    public function __construct(array $data) {
        $this->data = $data;
        if (is_array($this->data) && !empty($this->data)) {

            $this->valid['content'] = $this->contentCheck();
            $this->valid['userAvailability'] = $this->usernameCheck();
            $this->valid['validPassword'] = $this->passwordCheck();
            $this->valid['verifyPassword'] = $this->verifyPassword();
            $this->valid['validEmail'] = $this->emailCheck();
            $this->valid['verifyEmail'] = $this->verifyEmail();

        }

        $this->result = $this->validate();

    }

    public function check(array $data) {
        self::__construct($data);
    }

    protected function contentCheck() {

        /* Make sure user just didn't type spaces in an attempt to make valid */
        foreach ($this->data as $key => $value) {
            $this->data[$key] = isset($value) ? trim($value) : '';
        }
        /* if nothing is in the data field then there is not data */
        if (in_array("", $this->data, true)) {
            return \FALSE; // No Data:
        } else {
            return \TRUE; // All Fields contain Data:
        }
    }

    protected function usernameCheck() {
        $db = DB::getInstance();
        $pdo = $db->getConnection();

        $this->query = "SELECT 1 FROM users WHERE username = :username";
        $this->stmt = $pdo->prepare($this->query);
        $this->stmt->bindParam(':username', $this->data['username']);
        $this->stmt->execute();
        $this->row = $this->stmt->fetch();
        if ($this->row) {
            /* If there is at least one match then username is already taken */
            return \FALSE;
        } else {
            return \TRUE;
        }
    }

    protected function passwordCheck() {
        /*
         * 
         * Explaining !preg_match_all('$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$', $password)
         * $ = beginning of string
         * \S* = any set of characters
         * (?=\S{8,}) = of at least length 8
         * (?=\S*[a-z]) = containing at least one lowercase letter
         * (?=\S*[A-Z]) = and at least one one uppercase letter
         * (?=\S*[\d]) = and at least one number
         * (?=\S*[\W]) = and at least a special character (non-word character)
         * $ = end of the string:
         * 
         */
        if (preg_match_all('$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$', $this->data['password'])) {
            return \TRUE; // Valid Password:
        } else {
            return \FALSE; // Invalid Password:
        }
    }

    protected function verifyPassword() {
        if ($this->data['password'] === $this->data['verify_password']) {
            return \TRUE;
        } else {
            return \FALSE;
        }
    }

    protected function emailCheck() {
        if (filter_var($this->data['email'], FILTER_VALIDATE_EMAIL)) {
            return \TRUE; // Valid
        } else {
            return \FALSE; // Invalid
        }
    }

    protected function verifyEmail() {
        if ($this->data['email'] === $this->data['verify_email']) {
            return \TRUE;
        } else {
            return \FALSE;
        }
    }

    protected function validate() {
        foreach ($this->valid as $status) {
            if (!$status) {
                return \FALSE;
            }
        }
        return \TRUE;
    }

}

私が言ったように私はこれはもう使用していないし、私がやった場合、私は間違いなくそれを修正します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=21969&siteId=1