コード例 #1
0
 public function testGetMatch()
 {
     $condition = new PaletteConditionChain();
     $condition->setConjunction(PaletteConditionChain::AND_CONJUNCTION);
     $condition->addCondition(new PropertyValueCondition('prop1', '0'));
     $condition->addCondition(new PropertyValueCondition('prop2', '1'));
     $this->assertEquals(0, $condition->getMatchCount());
     $model = new DefaultModel();
     $model->setProperty('prop1', '0');
     $model->setProperty('prop2', '1');
     $this->assertEquals(2, $condition->getMatchCount($model));
     $model->setProperty('prop2', '0');
     $this->assertEquals(0, $condition->getMatchCount($model));
     $propertyValueBag = new PropertyValueBag();
     $propertyValueBag->setPropertyValue('prop1', '0');
     $propertyValueBag->setPropertyValue('prop2', '1');
     $this->assertEquals(2, $condition->getMatchCount(null, $propertyValueBag));
     $propertyValueBag->setPropertyValue('prop2', '3');
     $this->assertEquals(0, $condition->getMatchCount(null, $propertyValueBag));
 }
コード例 #2
0
 /**
  * Parse the palette selector and create the corresponding condition.
  *
  * @param string $paletteSelector    Create the condition for the selector.
  *
  * @param array  $selectorFieldNames The property names to be used as selectors.
  *
  * @return PaletteConditionInterface
  */
 public function createPaletteCondition($paletteSelector, array $selectorFieldNames)
 {
     if ($paletteSelector == 'default') {
         return new DefaultPaletteCondition();
     }
     // Legacy fallback, try to split on $selectors with optimistic suggestion of values.
     if (strpos($paletteSelector, '|') === false) {
         foreach ($selectorFieldNames as $selectorFieldName) {
             $paletteSelector = str_replace($selectorFieldName, '|' . $selectorFieldName . '|', $paletteSelector);
         }
     }
     // Extended mode, split selectors and values with "|".
     $paletteSelectorParts = explode('|', $paletteSelector);
     $paletteSelectorParts = array_map('trim', $paletteSelectorParts);
     $paletteSelectorParts = array_filter($paletteSelectorParts);
     $condition = new PaletteConditionChain();
     foreach ($paletteSelectorParts as $paletteSelectorPart) {
         // The part is a property name (checkbox like selector).
         if (in_array($paletteSelectorPart, $selectorFieldNames)) {
             $condition->addCondition(new PalettePropertyTrueCondition($paletteSelectorPart));
         } else {
             $orCondition = new PaletteConditionChain(array(), PaletteConditionChain::OR_CONJUNCTION);
             foreach ($selectorFieldNames as $selectorFieldName) {
                 $orCondition->addCondition(new PalettePropertyValueCondition($selectorFieldName, $paletteSelectorPart, true));
             }
             $condition->addCondition($orCondition);
         }
     }
     return $condition;
 }