This commit is contained in:
2024-06-26 11:10:17 +03:00
parent 987effd67f
commit 2714dad7ce
7 changed files with 78 additions and 142 deletions

View File

@ -11,11 +11,9 @@ class FileDatabaseDriver
public $file;
public array $headKeys = [];
public array $headValues = [];
private array $resArr = [];
private array $query = [];
private int $limitInt = 10;
public array $serviceInfo = [];
public function connect(string $filePath): bool
@ -26,7 +24,7 @@ class FileDatabaseDriver
if ($this->file) {
$this->getServiceInfo();
$this->setHeadKeys();
$this->setHeadValues();
// $this->setHeadValues();
return true;
}
@ -62,31 +60,26 @@ class FileDatabaseDriver
return $this->headKeys;
}
public function setHeadValues(): void
{
$headArr = explode(';', fgets($this->file));
foreach ($headArr as $item){
$item = str_replace("\n", "", $item);
$item = str_replace("\r", "", $item);
$this->headValues[] = $item;
}
// $this->head = explode(';', fgets($this->file));
}
public function getHeadValues(): array
{
return $this->headValues;
}
// public function setHeadValues(): void
// {
// $headArr = explode(';', fgets($this->file));
// foreach ($headArr as $item){
// $item = str_replace("\n", "", $item);
// $item = str_replace("\r", "", $item);
// $this->headValues[] = $item;
// }
//// $this->head = explode(';', fgets($this->file));
// }
//
// public function getHeadValues(): array
// {
// return $this->headValues;
// }
public function find(array $query): self
{
$j = 0;
foreach ($query as $key => $value) {
if(strlen($value) > 0) {
$this->query['column'][$j] = $key;
$this->query['value'][$j] = $value;
$j++;
}
$this->query[$key] = $value;
}
return $this;
}
@ -103,33 +96,35 @@ class FileDatabaseDriver
$resArr = [];
$i = 1;
while (!feof($this->file)) {
if ($i > $this->limitInt){
if ($i > $this->limitInt) {
break;
}
$str = fgets($this->file);
if (!empty($str)){
if (!empty($str)) {
$str = str_replace("\n", "", $str);
$str = str_replace("\r", "", $str);
$item = explode(";", $str);
$item = array_combine($this->headKeys, $item);
$cnt = count($this->query['column']);
for ($j = 0; $j < $cnt; $j++) {
if (strlen($this->searchInStr($this->query['value'][$j], $item, $this->query['column'][$j])) > 0) {
$flag = true;
} else {
$flag = false;
break;
$flag = false;
foreach ($this->query as $key => $value) {
foreach ($this->query[$key] as $k => $v) {
if (strlen($this->searchInStr($v, $item, $key)) > 0) {
$flag = true;
break;
} else {
$flag = false;
}
}
if(!$flag) break;
}
if ($flag){
if ($flag) {
$resArr[] = $item;
$i++;
}
}
}
return $resArr;
}
@ -155,10 +150,38 @@ class FileDatabaseDriver
fclose($this->file);
}
// public function updateServiceInfo(string $string): void
// {
// $id = $this->serviceInfo['last_id'];
// $this->serviceInfo['last_id'] = ++$id;
// foreach ($this->serviceInfo as $key => $value) {
// $arr[] = $key . '=' . $value;
// }
// $fileArr = file($this->filePath);
// if (isset($arr)) {
// $serviceStr = implode(';', $arr) . "\n";
// }
// $fileArr[0] = $serviceStr;
// file_put_contents($this->filePath, $fileArr);
//
// $str = $id . ';' . $string;
// $this->save($str);
// }
public function save(string $string): void
{
// $file = fopen($fileName, "a");
fwrite($this->file, $string);
// fclose($file);
$this->serviceInfo['last_id']++;
foreach ($this->serviceInfo as $key => $value) {
$arr[] = $key . '=' . $value;
}
$fileArr = file($this->filePath);
if (isset($arr)) {
$serviceStr = implode(';', $arr) . "\n";
}
$fileArr[0] = $serviceStr;
file_put_contents($this->filePath, $fileArr);
$str = $this->serviceInfo['last_id'] . ';' . $string;
fwrite($this->file, $str);
}
}