doujin4031 2011-12-17 08:14
浏览 16
已采纳

使用存储在数组中的比较运算符

I have the following example array:

$comparisons = array(
    0 => array(
        'method'    => 'productTotalQuantity',
        'operator'  => '>=',
        'value'     => '2'
    ),
    1 => array(
        'method'    => 'productTotalWeight',
        'operator'  => '<=',
        'value'     => '10'
    )
);

I have invented this array structure, so it can be altered if required. I am trying to somehow evaluate the operator key, so that I can achieve something along the lines of:

foreach ($comparisons as $comparison) {
    $value = $this->$comparison['method']($product);
    // E.g. $value = $this->productTotalQuantity($product)
    // $value could = 4

    if ($value $comparison['operator'] $comparison['value']) {
        // Comparison successful
        $matches[] = TRUE;
    }
}

if (count($matches) == count($comparisons)) {
    // All comparisons were successful. Apply the discount.
}

If you have time, a full code example of how to parse this array would be extremely helpful. I've been working on it for about 20 hours straight and think I'm about to literally bash my head against a brick wall. If you are familiar with Magento, I am trying to somewhat mimic the functionality of the "Shopping Cart Price Rule" Conditions in Promotions.

  • 写回答

3条回答 默认 最新

  • doujiao6507 2011-12-17 08:33
    关注

    "translating" operations from strings to real operations is mostly dirty and or evil :-) you need a lot of conditional code (if-else) or eval (but everybody knows: eval is evil ^^)

    I would use an object-oriented approach, since PHP does support object orientation (I don't know the exact php syntax, it has been a while ;-)) and I give you pseudo code to enforce the ideas:

    class AbstractComparator {
       boolean static compare($operand1, $operand2);   
    }
    
    class EqualsComparator extends AbstractComparator {
       @Override
       boolean static compare($operand1, $operand2) {
          return ($operand1 == $operand2);
       }
    }
    
    // Now use one of the subclasses in your datastructure
    $comparisons = array(
    0 => array(
        'method'    => 'productTotalQuantity',
        'operator'  => EqualsComparator,
        'value'     => '2'
    ),
    1 => array(
        'method'    => 'productTotalWeight',
        'operator'  => SmallerThanComparator,
        'value'     => '10'
    )
    );
    
    $allComparisonsSuccessful = true;
    foreach ($comparisons as $comparison) {
       $value = $comparison['operator'].equals($product);
        // Use the AND operator... full predicate is only true if all elements are true
       $allComparisonsSuccessful = $allComparisonsSuccessful && $value;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路