Laravel if语句带有其他变量值

时间:2016-09-01 09:42:30

标签: laravel laravel-5.1

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

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

不过,所有可能的条件都是可能的。

例如:

  • 检查表格column condition value
  • 检查服务器的状态== 1

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

1 个答案:

答案 0 :(得分:1)

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

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 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.
}