0

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:

  • check table where column condition value
  • check servers where status == 1

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

4
  • Do you have many operators/conditions, such as ==, if it's a limited number, then this might be possible. It's the operator that presents the biggest issue. Commented Sep 1, 2016 at 9:50
  • @Jonathon all possible operators/conditions you can imagen are possible. Commented Sep 1, 2016 at 9:52
  • Oh right, well that makes it more difficult then. I don't know if there's a way to dynamically program in an operator into an expression. However, take a look at my answer below. If you are able to implement all the operators you can think to use, then you could do what I have done below. This would also give you the flexibility to define your own operators too. Commented Sep 1, 2016 at 10:06
  • Sounds better suited to events and listeners. Commented Sep 1, 2016 at 10:41

1 Answer 1

1

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.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.