Robin Dirksen
Robin Dirksen

Reputation: 3422

Laravel if statement with other variable value

I try to get data from a model, but the value of the column name from the data model is in another variable. Here's a little preview what I try to achieve:

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
}

Example of what should be displayed:

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

The reason behide this is a little bit complicated. The user's need to create triggers which will be executed.

Btw, all possible conditons are possible.


For example:

Hope someone has an answer if you can understand my problem...

Upvotes: 0

Views: 2426

Answers (1)

Jonathon
Jonathon

Reputation: 16333

I'm not sure how feasible it is to dynamically insert the operator into an expression like that. However, with a limited set of operators, you could do something like this:

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');
        }
    }
}

Or a nicer, class based way of doing it...

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

Then you can use it like this...

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

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

Upvotes: 1

Related Questions