FormClasses/src/file_db_driver/FileDatabaseDriver.php
2024-06-26 11:10:17 +03:00

187 lines
5.0 KiB
PHP

<?php
namespace src\file_db_driver;
use src\debug\Debug;
class FileDatabaseDriver
{
public string $filePath;
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
{
$this->filePath = $filePath;
if (file_exists($this->filePath)) {
$this->file = fopen($this->filePath, "a+");
if ($this->file) {
$this->getServiceInfo();
$this->setHeadKeys();
// $this->setHeadValues();
return true;
}
}
return false;
}
public function getServiceInfo(): void
{
$serviceInfo = explode(';', fgets($this->file));
foreach ($serviceInfo as $item){
$item = str_replace("\n", "", $item);
$item = str_replace("\r", "", $item);
$itemArr = explode("=", $item);
$this->serviceInfo[$itemArr[0]] = $itemArr[1];
}
}
public function setHeadKeys(): void
{
$headArr = explode(';', fgets($this->file));
foreach ($headArr as $item){
$item = str_replace("\n", "", $item);
$item = str_replace("\r", "", $item);
$this->headKeys[] = $item;
}
// $this->head = explode(';', fgets($this->file));
}
public function getHeadKeys(): array
{
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 find(array $query): self
{
foreach ($query as $key => $value) {
$this->query[$key] = $value;
}
return $this;
}
public function limit(int $limit): self
{
$this->limitInt = $limit;
return $this;
}
public function search(): array
{
$resArr = [];
$i = 1;
while (!feof($this->file)) {
if ($i > $this->limitInt) {
break;
}
$str = fgets($this->file);
if (!empty($str)) {
$str = str_replace("\n", "", $str);
$str = str_replace("\r", "", $str);
$item = explode(";", $str);
$item = array_combine($this->headKeys, $item);
$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) {
$resArr[] = $item;
$i++;
}
}
}
return $resArr;
}
protected function searchInStr(string $strForSearch, array $item, string $column): ?string
{
if (isset($item[$column])) {
if (preg_match("/" . $strForSearch . "/", $item[$column])) {
return $item[$column];
}
}
return null;
}
public function all(): array
{
$this->resArr = $this->search();
return $this->resArr;
}
public function close(): void
{
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
{
$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);
}
}