【问题标题】:If Class exists && If Method exists PHP / Laravel如果类存在 && 如果方法存在 PHP / Laravel
【发布时间】:2019-05-20 22:08:35
【问题描述】:

PHP/Laravel

嘿,我正在进入 php 中的抽象,并试图根据已提交的任何内容来验证和存储值,我希望这些方法既不知道要验证什么和/或使用哪个类和方法这样做 -

我有什么作品,但我可以看到存在类/方法不存在的问题。这是我的问题。

如果我要调用以下格式的方法,哪种方式最好“检查”class_exists()method exists()

public function store(Request $request)
{
    $dataSet = $request->all();
    $inputs = $this->findTemplate();

    $errors = [];
    $inputValidators = [];
    foreach ($inputs as $input) {
        $attributes = json_decode($input->attributes);
        if (isset($attributes->validate)) {
            $inputValidators[$input->name] = $input->name;
        }
    }

    foreach ($dataSet as $dataKey => $data) {
        if (array_key_exists($dataKey, $inputValidators)) {
           $validate = "validate" . ucfirst($dataKey);
           $validated = $this->caseValidator::{$validate}($data);
            if ($validated == true) {
                $inputValidators[$dataKey] = $data;
            } else {
                $errors[$dataKey] = $data;
            }
        } else {
            $inputValidators[$dataKey] = $data;
        }
    }

    if (empty($errors)) {
        $this->mapCase($dataSet);
    } else {
        return redirect()->back()->with(['errors' => $errors]);
    }
}
public function mapCase($dataSet)
{
    foreach($dataSet as $dataKey => $data) {
        $model = 'case' . ucfirst($dataKey);
        $method = 'new' . ucfirst($dataKey);
        $attribute = $this->{$model}::{$method}($dataKey);

        if($attribute == false) {
            return redirect()->back()->with(['issue' => 'error msg here']);
        }
    }
    return redirect()->back->with(['success' => 'success msg here'])'
}

对于一些额外的上下文,输入表单将由一组输入组成,这可以随时更改。因此,我将所有值存储为 json 'payload'。

当用户首先提交所述表单时,会找到活动模板,该模板提供有关应验证的内容的详细信息$input->attributes,一旦定义了该模板,我就可以将caseValidator 模型中的函数调用为$this->caseValidator::{$validate}($data);

我认为这里不需要任何存在检查,因为验证参数是针对输入定义的,因此如果不存在,则使用 if (array_key_exists($dataKey, $inputValidators)) 跳过此检查

但是,我使用mapCase() 将一些数据分散到第二个代码块中的其他表中。这实际上是迭代所有数组键,无论是否存在它的方法,因此无法进行初始检查,如第一个块中所示。我尝试使用class_exists()method_exists,但从逻辑上讲它不适合,我不能指望它们按我的意愿工作,也许我在mapCase 中的方法不正确?我想如果我为每个键定义一个类,我应该使用一个类并在那里存在方法,这将消除检查现有类的需要。请指教

参考:

$attribute = $this->{$model}::{$method}($dataKey);

【问题讨论】:

  • 仅供参考,这段代码工作得很好,但我知道只要给mapCase() 一个我没有设置默认值的输入,它就会出错
  • 只是想确认...您正在接受用户的输入,并且用户提供了要使用的验证器列表?
  • store() 方法中,我选择了$dataSet,这是他们提交的内容的字面值和“名称”,然后$inputs 被赋予了活动表单模板findTemplate()。它是$inputs,现在所有输入条件都可用。验证存储在一个 json 有效负载字段中,如果用户提交的输入字段的“名称”匹配,则使用该字段
  • 管理员用户可以创建输入、定义验证等,然后他们可以定义哪些输入在哪个模板中以及哪个模板处于活动状态。所述模板将显示为非管理员用户的表单@Populus
  • 您能否将所有验证器存储在一个地图中,然后使用密钥访问它们?每个验证器都将实现一个 Validator 接口和一个返回 booleanvalidate 方法。

标签: php laravel validation abstract


【解决方案1】:

通过使用class_exists() 解决了潜在问题,考虑到我知道方法名称,因为它们与$dataKey 相同。

    public function mapCase($dataSet)
    {
        foreach($dataSet as $dataKey => $data) {
            $model = 'case' . ucfirst($dataKey);
            if (class_exists("App\Models\CaseRepository\\" . $model)) {
                $method = 'new' . ucfirst($dataKey);
                $attribute = $this->{$model}::{$method}($dataKey);
            }

            if($attribute == false) {
                return redirect()->back()->with(['issue' => 'error msg here']);
            }
        }
        return redirect()->back->with(['success' => 'success msg here'])'
    }

【讨论】:

    猜你喜欢
    • 2011-10-14
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2013-02-11
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多