From b9425ee8cf381a9b7b874bb142d7ad75fedc94aa Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 26 Jun 2024 12:42:42 +0300 Subject: [PATCH] model --- .gitignore | 1 + info.php | 19 ++++------ search.php | 21 +++--------- src/Processing.php | 2 ++ src/dto/BaseDTO.php | 1 + src/dto/InformationDTO.php | 4 ++- src/file_db_driver/FileDatabaseDriver.php | 13 ++++--- src/models/BaseModel.php | 42 +++++++++++++++++++++-- src/models/UserModel.php | 37 ++++++++++++++++++++ Information.txt => user.txt | 5 +-- 10 files changed, 107 insertions(+), 38 deletions(-) create mode 100644 src/models/UserModel.php rename Information.txt => user.txt (90%) diff --git a/.gitignore b/.gitignore index 485dee6..331c58f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +vendor \ No newline at end of file diff --git a/info.php b/info.php index 94dcbdf..2e9ffbd 100644 --- a/info.php +++ b/info.php @@ -1,25 +1,20 @@ load($_REQUEST); - -$string = $fullInformation->toString(); - -$obj = new FileDatabaseDriver(); -$obj->connect(__DIR__ . "/Information.txt"); -//$obj->updateServiceInfo($string); -$obj->save($string); -$obj->close(); +$user = new UserModel(); +$user->load($_REQUEST); +$user->save(); require "views/search.php"; \ No newline at end of file diff --git a/search.php b/search.php index dfecc69..3042c00 100644 --- a/search.php +++ b/search.php @@ -5,31 +5,20 @@ use src\Processing; require_once "vendor/autoload.php"; -$rep = new \src\file_db_driver\FileDatabaseDriver(); -$rep->connect(__DIR__ . "/Information.txt"); +ini_set("display_errors", true); +error_reporting(-1); -$columnKeys = $rep->getHeadKeys(); -//$columnValues = $rep->getHeadValues(); -$columnValues = ['id','Фамилия','Имя','Отчество','Дата рождения','Пол','Код страны','Номер телефона','Email','Статус']; +$user = new \src\models\UserModel(); -$columns = array_combine($columnKeys, $columnValues); //$res = $rep->find([$_REQUEST['key'] => $_REQUEST['result_search']])->limit(7)->all(); //$res = $rep->find($_REQUEST)->limit(10)->all(); -$res = $rep->find(['name' => ['Станислав', 'Анастасия', 'Анжела'], 'status' => ['0']])->limit(10)->all(); +$res = $user->find(['name' => ['Анжела']])->limit(10)->all(); -$rep->close(); - -$infArr = []; -foreach ($res as $key){ - $inf = new InformationDTO(); - $inf->load($key); - $infArr[] = $inf; -} $json = new Processing(); -$infArr = $json->createJsonArray($columns, $infArr, "form1"); +$infArr = $json->createJsonArray($user->labels(), $res, "form1"); $table = new \Itguild\Tables\ListJsonTable($infArr); $table->create(); diff --git a/src/Processing.php b/src/Processing.php index 5c5e40b..6c52da9 100644 --- a/src/Processing.php +++ b/src/Processing.php @@ -18,6 +18,8 @@ class Processing "meta" => [ "title" => $title, "columns" => $columns, + "perPage" => 10, + "currentPage" => 1, "params" => [ "class" => "table table-bordered", diff --git a/src/dto/BaseDTO.php b/src/dto/BaseDTO.php index 3a761e6..ef582c8 100644 --- a/src/dto/BaseDTO.php +++ b/src/dto/BaseDTO.php @@ -2,6 +2,7 @@ namespace src\dto; +#[\AllowDynamicProperties] class BaseDTO { public int $id; diff --git a/src/dto/InformationDTO.php b/src/dto/InformationDTO.php index 5535c65..3e4e1b1 100644 --- a/src/dto/InformationDTO.php +++ b/src/dto/InformationDTO.php @@ -24,6 +24,8 @@ class InformationDTO extends BaseDTO public string $countryCode; public string $phoneNumber; public string $email; + public int $status = 0; + public function __construct() { @@ -35,7 +37,7 @@ class InformationDTO extends BaseDTO public function fillable(): array { // return [ -// 'surname', 'name', 'patronymic', 'dateOfBirth', 'gender', 'countryCode', 'phoneNumber', 'email' +// 'surname', 'name', 'patronymic', 'dateOfBirth', 'gender', 'countryCode', 'phoneNumber', 'email', 'status' // ]; return [ $this->surname, $this->name, $this->patronymic, $this->dateOfBirth, $this->gender, $this->countryCode, $this->phoneNumber, $this->email, "\n" diff --git a/src/file_db_driver/FileDatabaseDriver.php b/src/file_db_driver/FileDatabaseDriver.php index af80b72..c2b60fe 100644 --- a/src/file_db_driver/FileDatabaseDriver.php +++ b/src/file_db_driver/FileDatabaseDriver.php @@ -76,7 +76,7 @@ class FileDatabaseDriver // return $this->headValues; // } - public function find(array $query): self + public function find(array $query = []): self { foreach ($query as $key => $value) { $this->query[$key] = $value; @@ -110,7 +110,7 @@ class FileDatabaseDriver $flag = false; foreach ($this->query as $key => $value) { foreach ($this->query[$key] as $k => $v) { - if (strlen($this->searchInStr($v, $item, $key)) > 0) { + if ($this->searchInStr($v, $item, $key)) { $flag = true; break; } else { @@ -128,11 +128,11 @@ class FileDatabaseDriver return $resArr; } - protected function searchInStr(string $strForSearch, array $item, string $column): ?string + protected function searchInStr(string $strForSearch, array $item, string $column): ?array { if (isset($item[$column])) { if (preg_match("/" . $strForSearch . "/", $item[$column])) { - return $item[$column]; + return $item; } } @@ -142,6 +142,7 @@ class FileDatabaseDriver public function all(): array { $this->resArr = $this->search(); + $this->close(); return $this->resArr; } @@ -181,7 +182,9 @@ class FileDatabaseDriver $fileArr[0] = $serviceStr; file_put_contents($this->filePath, $fileArr); - $str = $this->serviceInfo['last_id'] . ';' . $string; + $str = $this->serviceInfo['last_id'] . ';' . $string . "\n"; fwrite($this->file, $str); + + $this->close(); } } \ No newline at end of file diff --git a/src/models/BaseModel.php b/src/models/BaseModel.php index 8d20b30..95e7731 100644 --- a/src/models/BaseModel.php +++ b/src/models/BaseModel.php @@ -2,13 +2,15 @@ namespace src\models; +use src\debug\Debug; use src\file_db_driver\FileDatabaseDriver; +#[\AllowDynamicProperties] class BaseModel { protected FileDatabaseDriver $fdd; - public int $id; +// public int $id; public function __construct() { @@ -24,6 +26,15 @@ class BaseModel return []; } + public function load(array $array): void + { + if (is_array($array)) { + foreach ($array as $key => $value) { + $this->{$key} = $value; + } + } + } + /** * @return string */ @@ -32,9 +43,36 @@ class BaseModel return ""; } - public function save() + public function labels(): array { + return []; + } + public function save(): void + { + $this->fdd->save($this->toString()); + } + + public function find(array $request = []): FileDatabaseDriver + { + return $this->fdd->find($request); + } + + public function toString(): string + { + $str = ''; + $lastItems = array_key_last($this->fdd->getHeadKeys()); + foreach ($this->fdd->getHeadKeys() as $key => $value) { + if (isset($this->{$value})) { + if ($key === $lastItems) { + $str .= $this->{$value}; + } else { + $str .= $this->{$value} . ";"; + } + } + } + + return $str; } } \ No newline at end of file diff --git a/src/models/UserModel.php b/src/models/UserModel.php new file mode 100644 index 0000000..a59e1c4 --- /dev/null +++ b/src/models/UserModel.php @@ -0,0 +1,37 @@ + 'Фамилия', + 'name' => 'Имя', + 'patronymic' => 'Отчество', + 'dateOfBirth' => 'Дата рождения', + 'gender' => 'Пол', + 'countryCode' => 'Код страны', + 'phoneNumber' => 'Номер телефона', + 'email' => 'Email', + 'status'=> 'Статус' + ]; + } + +} \ No newline at end of file diff --git a/Information.txt b/user.txt similarity index 90% rename from Information.txt rename to user.txt index eef77cb..38d933a 100644 --- a/Information.txt +++ b/user.txt @@ -1,4 +1,4 @@ -autoincrement=id;last_id=24;table_name=user_info +autoincrement=id;last_id=36;table_name=user id;surname;name;patronymic;dateOfBirth;gender;countryCode;phoneNumber;email;status 3;Билай;Станислав;Романович;2002-03-11;Мужчина;+7;9493761924;stasbilay02@yandex.ru;0 4;Иванова;Мария;Сергеевна;2002-04-05;Женщина;+38;2222222222;sdguhnlnoih@gmail.com;1 @@ -17,4 +17,5 @@ id;surname;name;patronymic;dateOfBirth;gender;countryCode;phoneNumber;email;stat 17;Зайцева;Анжела;Игоревна;7777-07-07;Женщина;+7;4444444444;aksjfhfka@mail.ru;0 18;Зайцева;Анжела;Игоревна;7777-07-07;Женщина;+7;4444444444;aksjfhfka@mail.ru;1 19;Зайцева;Анжела;Игоревна;7777-07-07;Женщина;+7;4444444444;aksjfhfka@mail.ru;1 -24;ddd;Кирилл;ddd;2024-06-25;Мужчина;+7;9281234567;apuc06@mail.ru; +24;ddd;Кирилл;dfdf;2024-06-25;Мужчина;+7;9281234567;apuc06@mail.ru;1 +36;ddd4;Кирилл;ddd4;2024-06-25;Мужчина;+7;9281234567;apuc06@mail.ru;1