Laravel 带有其他变量值的 if 语句

Laravel if statement with other variable value

我尝试从模型中获取数据,但数据模型中列名的值在另一个变量中。这是我尝试实现的一些预览:

switch($device->target_objectclass_id) {
    case 10:
        $handler = Servers::findOrFail($device->target_object_id);
    default:
        break;
}

if($handler->($condition->column) ($condition->condition) ($condition->value)) {
    //process the other data it it's true
}

应显示的示例:

if($handler->status == 1) { 
    //handle data 
}

这背后的原因有点复杂。用户需要创建 triggers 将被执行。

顺便说一句,所有可能的条件都是可能的。


例如:

如果你能理解我的问题,希望有人能回答...

我不确定将运算符动态插入到这样的表达式中是否可行。但是,使用一组有限的运算符,您可以执行以下操作:

class YourClass
{
    public function yourMethod()
    {
        // Your model instance to test, I've just used a stdClass as an example
        $instance = new stdClass;
        $instance->status = 1;

        // Your condition instance
        $condition = new stdClass;
        $condition->column = 'status';
        $condition->condition = '==';
        $condition->value = '1';

        if ($this->compare($instance, $condition)) {
            // This code will execute then $instance->status == '1'
        }
    }

    public function compare($instance, $condition)
    {
        $operator = $condition->condition;
        $condition = $condition->column;
        $value = $condition->value;

        switch ($operator) {
            case '==':
                return $instance->$column == $value;
            case '!=':
                return $instance->$column != $value;
            case '>':
                return $instance->$column > $value;
            case '<':
                return $instance->$column < $value;
            case '>=':
                return $instance->$column >= $value;
            case '<=':
                return $instance->$column <= $value;
            default:
                throw new \Exception('Unsupported operator');
        }
    }
}

或者更好的、基于 class 的方法...

class Conditional
{
    protected $operator;
    protected $column;
    protected $value;
    protected $supportedOperators = [
        '==' => 'equals',
        '!=' => 'notEquals'
    ];

    public function __construct($column, $operator, $value)
    {
        $this->column = $column;
        $this->operator = $operator;
        $this->value = $value;
    }

    public function check($instance)
    {
        $method = $this->getMethod();

        $instanceValue = $instance->{$this->column};

        return $this->$method($instanceValue, $this->value);
    }

    private function getMethod()
    {
        if (isset($this->supportedOperators[$this->operator]) && method_exists($this, $this->supportedOperators[$this->operator])) {
            return $this->supportedOperators[$this->operator];
        }

        throw new \Exception('Unsupported operator');
    }

    protected function equals($one, $two)
    {
        return $one == $two;
    }

    protected function notEquals($one, $two)
    {
        return $one != $two;
    }
}

那你就可以这样用了...

$instance = new stdClass;
$instance->status = 1;

$conditional = new Conditional('status', '==', 1);
if ($conditional->check($instance)) {
    // Execute if $instance->status == 1.
}