/**
  * return values as array
  * 
  * @return array
  */
 public function toArray()
 {
     $conditions = array();
     foreach ($this->_conditions as $condition) {
         $conditions[] = $condition->toArray();
     }
     $action = $this->_action->toArray();
     return array('conditions' => $conditions, 'action_type' => $action['type'], 'action_argument' => $action['argument'], 'enabled' => (int) $this->_enabled, 'id' => $this->_id);
 }
 /**
  * get sieve rule object
  * 
  * @return Expressomail_Sieve_Rule
  */
 public function getFSR()
 {
     $fsr = new Expressomail_Sieve_Rule();
     $fsr->setEnabled($this->enabled)->setId($this->id);
     $fsra = new Expressomail_Sieve_Rule_Action();
     $fsra->setType($this->action_type)->setArgument($this->action_argument);
     $fsr->setAction($fsra);
     foreach ($this->conditions as $condition) {
         $value = $condition['key'];
         if ($condition['test'] == 'size') {
             //Converts back from MB to KB
             $value = floor($value * 1024);
         }
         $fsrc = new Expressomail_Sieve_Rule_Condition();
         $fsrc->setTest($condition['test'])->setComperator($condition['comperator'])->setHeader($condition['header'])->setKey($value);
         $fsr->addCondition($fsrc);
     }
     return $fsr;
 }
 /**
  * parse rule generated by SmartSieve
  * 
  * @param   array   $parts
  */
 protected function _parseSmartSieveRule(array $parts)
 {
     $rule = new Expressomail_Sieve_Rule();
     $rule->setId($parts[1])->setEnabled($parts[2] == 'ENABLED' ? true : false);
     // conditions
     // from test
     if (!empty($parts[3])) {
         $condition = new Expressomail_Sieve_Rule_Condition();
         $condition->setTest(Expressomail_Sieve_Rule_Condition::TEST_ADDRESS)->setComperator(Expressomail_Sieve_Rule_Condition::COMPERATOR_CONTAINS)->setHeader('from')->setKey($this->_unescapeChars($parts[3]));
         $rule->addCondition($condition);
     }
     // to test
     if (!empty($parts[4])) {
         $condition = new Expressomail_Sieve_Rule_Condition();
         $condition->setTest(Expressomail_Sieve_Rule_Condition::TEST_ADDRESS)->setComperator(Expressomail_Sieve_Rule_Condition::COMPERATOR_CONTAINS)->setHeader(array('to', 'cc'))->setKey($this->_unescapeChars($parts[4]));
         $rule->addCondition($condition);
     }
     // subject test
     if (!empty($parts[5])) {
         $condition = new Expressomail_Sieve_Rule_Condition();
         $condition->setTest(Expressomail_Sieve_Rule_Condition::TEST_HEADER)->setComperator(Expressomail_Sieve_Rule_Condition::COMPERATOR_CONTAINS)->setHeader('subject')->setKey($this->_unescapeChars($parts[5]));
         $rule->addCondition($condition);
     }
     // header test
     if (!empty($parts[9])) {
         $condition = new Expressomail_Sieve_Rule_Condition();
         $condition->setTest(Expressomail_Sieve_Rule_Condition::TEST_HEADER)->setComperator(Expressomail_Sieve_Rule_Condition::COMPERATOR_CONTAINS)->setHeader($this->_unescapeChars($parts[9]))->setKey($this->_unescapeChars($parts[10]));
         $rule->addCondition($condition);
     }
     // size
     if (!empty($parts[11])) {
         $condition = new Expressomail_Sieve_Rule_Condition();
         $comperator = empty($parts[8]) ? Expressomail_Sieve_Rule_Condition::COMPERATOR_OVER : $parts[8] === 0 ? Expressomail_Sieve_Rule_Condition::COMPERATOR_UNDER : Expressomail_Sieve_Rule_Condition::COMPERATOR_OVER;
         $condition->setTest(Expressomail_Sieve_Rule_Condition::TEST_SIZE)->setComperator($comperator)->setKey($this->_unescapeChars($parts[11]));
         $rule->addCondition($condition);
     }
     // action
     $action = new Expressomail_Sieve_Rule_Action();
     switch ($this->_unescapeChars($parts[6])) {
         case 'folder':
             $action->setType(Expressomail_Sieve_Rule_Action::FILEINTO)->setArgument($this->_unescapeChars($parts[7]));
             break;
         case 'address':
             $action->setType(Expressomail_Sieve_Rule_Action::REDIRECT)->setArgument($this->_unescapeChars($parts[7]));
             break;
         case 'reject':
             $action->setType(Expressomail_Sieve_Rule_Action::REJECT)->setArgument($this->_unescapeChars($parts[7]));
             break;
         case 'discard':
             $action->setType(Expressomail_Sieve_Rule_Action::DISCARD);
             break;
     }
     $rule->setAction($action);
     //parts[8] == regex bit
     $this->addRule($rule);
 }
 /**
  * test enabled rule
  */
 public function testEnabledRule()
 {
     $script = new Expressomail_Sieve_Backend_Script();
     $rule = new Expressomail_Sieve_Rule();
     $condition = new Expressomail_Sieve_Rule_Condition();
     $action = new Expressomail_Sieve_Rule_Action();
     $condition->setComperator(Expressomail_Sieve_Rule_Condition::COMPERATOR_CONTAINS)->setTest(Expressomail_Sieve_Rule_Condition::TEST_ADDRESS)->setHeader('From')->setKey('*****@*****.**');
     $action->setType(Expressomail_Sieve_Rule_Action::FILEINTO)->setArgument('INBOX/UNITTEST');
     $rule->setEnabled(true)->setId(12)->setAction($action)->addCondition($condition);
     $script->addRule($rule);
     $sieveScript = $script->getSieve();
     #echo $sieveScript;
     $this->assertContains('if allof (address :contains "From" "*****@*****.**")', $sieveScript);
     $this->assertContains('fileinto "INBOX/UNITTEST";', $sieveScript);
     $this->assertContains('Expressomail_Sieve_Rule', $sieveScript);
 }