Méthode filter($match) pour les recherches

This commit is contained in:
xdrm-brackets 2016-05-17 15:37:47 +02:00
parent 2cf98451e2
commit 083814ab16
2 changed files with 18 additions and 19 deletions

View File

@ -103,22 +103,8 @@
var_dump('INSERTING key0 to key99'); var_dump('INSERTING key0 to key99');
for( $i = 0 ; $i < 100 ; $i++ ) for( $i = 0 ; $i < 100 ; $i++ )
echo $db->insert('key'.$i, array('data0_'.$i, 'data1_'.$i)) ? 'o' : '_'; echo $db->insert('key'.$i, array('data0_'.$i, 'data1_'.$i)) ? '.' : '_';
var_dump( $db->filter(array( 0 => 'data0_1' )));
var_dump('FETCHING key0 : '); var_dump( $db->fetch('key0') );
var_dump('FETCHING key1 : '); var_dump( $db->fetch('key1') );
var_dump('FETCHING key2 : '); var_dump( $db->fetch('key2') );
var_dump('FETCHING key3 : '); var_dump( $db->fetch('key3') );
var_dump('DELETING key0 : '. $db->delete('key0') );
var_dump('DELETING key1 : '. $db->delete('key1') );
var_dump('DELETING key2 : '. $db->delete('key2') );
var_dump('DELETING key3 : '. $db->delete('key3') );
var_dump('FETCHING key0 : '); var_dump( $db->fetch('key0') );
var_dump('FETCHING key1 : '); var_dump( $db->fetch('key1') );
var_dump('FETCHING key2 : '); var_dump( $db->fetch('key2') );
var_dump('FETCHING key3 : '); var_dump( $db->fetch('key3') );
$db->close(); $db->close();

View File

@ -96,7 +96,7 @@
public function insert($key, $data){ public function insert($key, $data){
/* (1) On vérifie que la clé est unique */ /* (1) On vérifie que la clé est unique */
if( array_key_exists($key, $this->index) ) if( array_key_exists($key, $this->index) )
return false; return true;
/* (2) On ajoute les données aux fichier */ /* (2) On ajoute les données aux fichier */
$json_data = json_encode($data); $json_data = json_encode($data);
@ -219,18 +219,31 @@
/* (1) Si @data est un tableau associatif */ /* (1) Si @data est un tableau associatif */
if( is_array($data) ){ if( is_array($data) ){
$filtered = array();
foreach($this->index as $i=>$indexData){
$this->driver->seek( $indexData['line'] );
$dbData = json_decode( $this->driver->fgets(), true );
foreach($data as $key=>$value)
if( isset($dbData[$key]) && preg_match("#$value#", $dbData[$key]) ){
$filtered[$i] = $dbData;
break;
}
}
return $filtered;
/* (2) Sinon on compare @data en tant que valeur simple */ /* (2) Sinon on compare @data en tant que valeur simple */
}else{ }else{
$this->tmp = sha1( json_encode($data) ); $this->tmp = sha1( json_encode($data) );
return array_filter($this->index, array($this, 'callbackFilter')); return array_filter($this->index, array($this, 'simpleFilter'));
} }
} }
protected function callbackFilter($e){ return $e['hash'] == $this->tmp; } protected function simpleFilter($e){ return $e['hash'] == $this->tmp; }