gpt4 book ai didi

php - 如何重构这段代码?

转载 作者:行者123 更新时间:2023-12-04 06:58:24 24 4
gpt4 key购买 nike

分组参数:

<?php
class Form {
private $field;

public function getFieldRelated($field) {
return $this->fieldrelated[$field];
}

public function __construct() {
$this->fieldrelated['email']['name'] = 'email';
$this->fieldrelated['email']['value'] = $_POST['email'];
$this->fieldrelated['email']['pattern'] = REGEX_EMAIL;
$this->fieldrelated['email']['confirmation'] = 'emailconfirmation';
$this->fieldrelated['email']['names'] = 'emails';

$this->fieldrelated['emailconfirmation']['name'] = 'email confirmation';
$this->fieldrelated['emailconfirmation']['value'] = $_POST['emailconfirmation'];
$this->fieldrelated['emailconfirmation']['pattern'] = REGEX_EMAIL;

$this->fieldrelated['password']['name'] = 'password';
$this->fieldrelated['password']['value'] = $_POST['password'];
$this->fieldrelated['password']['pattern'] = REGEX_PASSWORD;
$this->fieldrelated['password']['confirmation'] = 'passwordconfirmation';
$this->fieldrelated['password']['names'] = 'passwords';

$this->fieldrelated['passwordconfirmation']['name'] = 'password confirmation';
$this->fieldrelated['passwordconfirmation']['value'] = $_POST['passwordconfirmation'];
$this->fieldrelated['passwordconfirmation']['pattern'] = REGEX_PASSWORD;
}
}
?>

Validate 类的一部分:
public function isEmpty($field) {
$fieldrelated = $this->form->getFieldRelated($field);
$name = $fieldrelated['name'];
$value = $fieldrelated['value'];

if(empty($value)) {
$this->setProperty($field, 'empty');
$this->addErrorMessage('The '.$name.' is empty!');
return true;
} else {
$this->setProperty($field, 'unempty');
return false;
}
}
public function isValid($field) {
$fieldrelated = $this->form->getFieldRelated($field);
$name = $fieldrelated['name'];
$value = $fieldrelated['value'];

$pattern = $fieldrelated['pattern'];

if(preg_match($pattern, $value)) {
$this->setProperty($field, 'valid');
return true;
} else {
$this->setProperty($field, 'invalid');
$this->addErrorMessage('The '.$name.' is invalid!');
return false;
}
}
public function isConfirmed($field) {
$fieldrelated = $this->form->getFieldRelated($field);
$value = $fieldrelated['value'];

$field2 = $fieldrelated['confirmation'];

$fieldrelated2 = $this->form->getFieldRelated($field2);
$value2 = $fieldrelated2['value'];

$names = $fieldrelated['names'];

if($value == $value2) {
$this->setProperty($field, 'confirmed');
$this->setProperty($field2, 'confirmed');
return true;
} else {
$this->setProperty($field, 'unconfirmed');
$this->setProperty($field2, 'unconfirmed');
$this->addErrorMessage('The '.$names.' are unconfirmed!');
return false;
}
}
public function isEmailOnlyIn($correct) {
$fieldrelated = $this->form->getFieldRelated('email');
$name = $fieldrelated['name'];
$value = $fieldrelated['value'];

$value = mysql_real_escape_string($value);
$result = "SELECT * FROM account WHERE email = '$value'";
$result = mysql_query($result);
$result = mysql_fetch_array($result);

if($result) {
$this->setProperty('email', 'email only in');
if($correct == 'not in') {
$this->addErrorMessage('The '.$name.' is in database!');
}
return true;
} else {
$this->setProperty('email', 'email only not in');
if($correct == 'in') {
$this->addErrorMessage('The '.$name.' is not in database.');
}
return false;
}
}
public function isPasswordAlsoIn($correct) {
$fieldrelated = $this->form->getFieldRelated('email');
$name = $fieldrelated['name'];
$value = $fieldrelated['value'];

$fieldrelated2 = $this->form->getFieldRelated('password');
$name2 = $fieldrelated2['name'];
$value2 = $fieldrelated2['value'];

$value = mysql_real_escape_string($value);

$value2 = md5($value2);
$value2 = mysql_real_escape_string($value2);

$result = "SELECT * FROM account WHERE email = '$value' AND password = '$value2'";
$result = mysql_query($result);
$result = mysql_fetch_array($result);

if($result) {
$this->setProperty('password', 'password also in');
if($correct == 'not in') {
$this->addErrorMessage('The '.$name2.' is in database!');
}
return true;
} else {
$this->setProperty('password', 'password also not in');
if($correct == 'in') {
$this->addErrorMessage('The '.$name2.' is not in database!');
}
return false;
}
}

用法:
    if(!$validate->isEmpty('email')) {
$validate->isValid('email');
}
if(!$validate->isEmpty('emailconfirmation')) {
$validate->isValid('emailconfirmation');
}
if($validate->isProperty('email', 'valid') && $validate->isProperty('emailconfirmation', 'valid')) {
$validate->isConfirmed('email');
}

if(!$validate->isEmpty('password')) {
$validate->isValid('password');
}
if(!$validate->isEmpty('passwordconfirmation')) {
$validate->isValid('passwordconfirmation');
}
if($validate->isProperty('password', 'valid') && $validate->isProperty('passwordconfirmation', 'valid')) {
$validate->isConfirmed('password');
}

if($validate->isProperty('email', 'confirmed') && $validate->isProperty('emailconfirmation', 'confirmed')) {
$validate->isEmailOnlyIn('not in');
}

最佳答案

尝试找出代码中组件之间的异同。例如,您需要一个 Form你已经知道了,但是表单由不同的字段组成,那么为什么不把它们提取成一堆Field -类(class)?赞 EmailField , PasswordField .

您可能已经注意到 Validate做太多事情。例如,如果表单仅包含电子邮件字段,则您不需要 Validate包括有关密码等的任何内容。当您开始为“用户名”或“原产国”或任何其他属性添加验证规则时,您不想将规则添加到一个大的、单一的 Validate -class,但对于每个 Field或者像 ValidateEmailField 这样的辅助类.

关于php - 如何重构这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2262388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com