Everything in this code is completely working, but I still feel that this code needs to be refactored. any suggestions?
<?php
class Db_CheckUsername{
protected $_conn;
protected $_username;
protected $_minimumChars = 8;
protected $_errors = array();
public function __construct($username, $conn){
$this->_username = $username;
$this->_conn= $conn;
}
public function check(){
$sql = "SELECT * FROM accounts WHERE username = '{$this->_username}'";
$result = $this->_conn->query($sql);
$numRows = $result->num_rows;
if(preg_match('/\s/',$this->_username)){
$this->_errors[] = "Spaces are not allowed";
}
if($numRows > 0){
$this->_errors[] = "Username Taken";
}
if(strlen($this->_username) < $this->_minimumChars){
$this->_errors[] = "Username Should Contain, atleast {$this->_minimumChars} chars";
}
return $this->_errors ? false : true;
}
public static function isUsernameCorrect($username,$conn){
}
public function getErrors(){
return $this->_errors;
}
}