我正在開發一個應用程序,它將查找電子郵件收件箱並將特定電子郵件保存爲其功能的一部分。發生的是一個imap連接,然後檢索所有設置爲UNSEEN的電子郵件。每個電子郵件都會根據預先定義的條件進行檢查,然後在符合時保存到數據庫中。這些條件可以由用戶設置,並且可以是下列:如何存儲/格式化這些無限數據過濾條件
主題
- 包含字符串
- 不包含字符串
身體
- 包含字符串
- 不包含字符串
從
- 具體地址
條件可以 「鏈接」,例如:
FILTER WHERE
Subject CONTAINS "Order Confirmation"
AND
Email FROM "[email protected]" OR "[email protected]"
我有如何格式化和存儲這些麻煩思維條件。我需要一個GUI,用戶可以創建這些條件。
我的問題是我應該如何存儲這些條件?即某種數據庫表結構,或者可能轉換爲字符串格式並存儲在單個表中。需要爲無限數量的用戶提供無限數量的條件,並且我需要知道有哪些運營商等。
希望這是足夠的信息!
*編輯邁克爾·
因此,我可以創造條件並保存。現在我正在檢索它們並嘗試匹配電子郵件。我用一個條件創建了一個過濾器:Subject包含'TEST'。只有一封電子郵件應該與此匹配,但不知何故,所有電子郵件都將被添加到匹配的數組中。
我的控制器是獲得電子郵件/條件:
public function check_email(){
$filters = $this->filters_model->get_filters($owner_id=1);
foreach($filters->result() as $filter){
$emails = $this->gmail->get_emails($mailbox_id = $filter->mailbox_id, $limit = 10);
$matched = array();
$conditions = unserialize($filter->conditions);
foreach($emails as $email){
if($conditions->isMet($email) == TRUE){
$matched[] = $email;
}
}
echo count($matched);
echo '<pre>'.$filter->title.'<br /.';
print_r($conditions);
echo '</pre><br />-----';
exit;
}
}
Keyvalueprerequisite.php
這似乎很好地工作作爲stripos($subject, $this->value) !== FALSE;
一個的var_dump僅顯示1個電子郵件TRUE。
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Keyvalueprerequisite {
private $key;
private $comparator;
private $value;
public function __construct($key, $comparator, $value){
$this->key = $key;
$this->comparator = $comparator;
$this->value = $value;
}
public function isMet(&$context)
{
switch ($this->key) {
case "subject":
$subject = $context["subject"];
if ($this->comparator === "in"){
return stripos($subject, $this->value) !== FALSE;
} else if ($this->comparator === "!in") {
return stripos($subject, $this->value) === FALSE;
}
return FALSE;
break;
case "body":
$body = $context["body"];
if ($this->comparator === "in") {
return stripos($body, $this->value) !== FALSE;
} else if ($this->comparator === "!in") {
return stripos($body, $this->value) === FALSE;
}
return FALSE;
break;
case "from_address":
$from = $context["from"];
if ($this->comparator === "=") {
return $this->value === $from;
} else if ($this->comparator === "!=") {
return $this->value !== $from;
} else{
return false;
}
break;
default:
}
return FALSE;
}
}
Prerequisistegroup.php
東西可能不是很寫到這裏。對於10封電子郵件中的每一封,var_dump的$result = $result && $is_met
都會返回true。
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Prerequisitegroup {
private $type;
private $prerequisites;
public function __construct($type = 'AND'){
$this->type = $type;
}
public function add(){
foreach(func_get_args() as $prerequisite){
$this->prerequisites[] = $prerequisite;
}
}
public function isMet(&$context)
{
if (empty($this->prerequisites) === FALSE) {
$result = TRUE;
foreach ($this->prerequisites as $prerequisite) {
$is_met = $prerequisite->isMet($context);
if ($this->type === 'AND') {
$result = $result && $is_met;
if ($result === FALSE) {
return FALSE;
}
} else {
$result = $result || $is_met;
if ($result === TRUE) {
return TRUE;
}
}
}
return $result;
}
return TRUE;
}
}
這是我正在檢查的條件:
string(3) "AND"
["prerequisites":"Prerequisitegroup":private]=>
array(1) {
[0]=>
object(Prerequisitegroup)#23 (2) {
["type":"Prerequisitegroup":private]=>
string(2) "OR"
["prerequisites":"Prerequisitegroup":private]=>
array(1) {
[0]=>
object(Keyvalueprerequisite)#24 (3) {
["key":"Keyvalueprerequisite":private]=>
string(7) "subject"
["comparator":"Keyvalueprerequisite":private]=>
string(2) "in"
["value":"Keyvalueprerequisite":private]=>
string(4) "TEST"
}
}
}
}
}
你能像'email'一樣擁有多個'contains'嗎?你能對'包含'有否定嗎? – Michael