FormClasses/src/file_db_driver/FileDatabaseDriver.php

164 lines
4.1 KiB
PHP
Raw Normal View History

2024-06-14 15:55:36 +03:00
<?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
{
$j = 0;
foreach ($query as $key => $value) {
if(strlen($value) > 0) {
$this->query['column'][$j] = $key;
$this->query['value'][$j] = $value;
$j++;
}
}
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);
$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;
}
}
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 save(string $string): void
{
// $file = fopen($fileName, "a");
fwrite($this->file, $string);
// fclose($file);
}
}