init
This commit is contained in:
commit
e41402c600
17
category.php
Normal file
17
category.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/functions.php';
|
||||||
|
|
||||||
|
$cat = " na2t5u25re spo12r54t funn82y cake2! 8sea au#!tumn!";
|
||||||
|
$cat_temp = parseCategories($cat);
|
||||||
|
|
||||||
|
$categories = [];
|
||||||
|
foreach ($cat_temp as $cat) {
|
||||||
|
$digits = preg_replace('/\D/', '', $cat);
|
||||||
|
$letters = preg_replace('/[^a-zA-Z]/', '', $cat);
|
||||||
|
$random = random_int(1000, 5000);
|
||||||
|
|
||||||
|
$categories[$random + intval($digits)] = $letters;
|
||||||
|
}
|
||||||
|
ksort($categories);
|
||||||
|
|
||||||
|
var_dump($categories);
|
||||||
18
config.php
Normal file
18
config.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
const PARAMS = [
|
||||||
|
"HOST" => 'localhost',
|
||||||
|
"USER" => 'integ_user',
|
||||||
|
"PASSWORD" => '323446',
|
||||||
|
"DB" => 'web_prog_task_15',
|
||||||
|
"CHARSET" => 'utf8mb4'
|
||||||
|
];
|
||||||
|
|
||||||
|
$dsn = "mysql:host=" . PARAMS['HOST'] . ";dbname=" . PARAMS['DB'] . ";charset=" . PARAMS['CHARSET'];
|
||||||
|
|
||||||
|
$pdoOptions = [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||||
|
PDO::ATTR_EMULATE_PREPARES => false
|
||||||
|
];
|
||||||
71
functions.php
Normal file
71
functions.php
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . "/config.php";
|
||||||
|
|
||||||
|
$pdo = connectDatabase($dsn, $pdoOptions);
|
||||||
|
|
||||||
|
/** Function tries to connect to database using PDO
|
||||||
|
* @param string $dsn
|
||||||
|
* @param array $pdoOptions
|
||||||
|
* @return PDO
|
||||||
|
*/
|
||||||
|
function connectDatabase(string $dsn, array $pdoOptions): PDO
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = new PDO($dsn, PARAMS['USER'], PARAMS['PASSWORD'], $pdoOptions);
|
||||||
|
} catch (\PDOException $e) {
|
||||||
|
var_dump($e->getCode());
|
||||||
|
throw new \PDOException($e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $pdo;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseCategories(string $cat_string)
|
||||||
|
{
|
||||||
|
$cat_string = trim($cat_string);
|
||||||
|
$cats = explode(" ", $cat_string);
|
||||||
|
|
||||||
|
$filtered_cats = array_filter($cats, function($cat) {
|
||||||
|
if(mb_strlen($cat) <= 4)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return(ctype_alnum($cat));
|
||||||
|
});
|
||||||
|
|
||||||
|
return $filtered_cats;
|
||||||
|
}
|
||||||
|
|
||||||
|
function insertCategories(array $cats) {
|
||||||
|
global $pdo;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo->beginTransaction();
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare("INSERT INTO categories (name, code) VALUES (:name, :code)");
|
||||||
|
foreach ($cats as $cat_code => $cat_name) {
|
||||||
|
$stmt->execute([
|
||||||
|
':name' => $cat_name,
|
||||||
|
':code' => $cat_code
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$pdo->commit();
|
||||||
|
}
|
||||||
|
catch (Exception $e) {
|
||||||
|
$pdo->rollBack();
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class User {
|
||||||
|
function createUser() {
|
||||||
|
|
||||||
|
}
|
||||||
|
function createUserStmt() {
|
||||||
|
|
||||||
|
}
|
||||||
|
function createUsers() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
4
insert.php
Normal file
4
insert.php
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . "/category.php";
|
||||||
|
|
||||||
|
insertCategories($categories);
|
||||||
3
readme.md
Normal file
3
readme.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
1. Umesto `getCategories` bolje nazvati funkciju `parseCategories` zato sta `get...` podrazumeva samo dobijanje podataka (npr., iz db), a ne odbradu string-a.
|
||||||
|
2. `createUsers` osim toga, sta kreira korisnika rade parsiranje iz string-ova i autogenerisanje random podataka šta predstavlja **God Function** i uništava **Single Responsibility Principle**.
|
||||||
|
Bolje uraditi klas `User` koji će imati `createUserStmp`, `createUser` i `createUsers` koji uzimaju prototip objekata od klasa User.
|
||||||
Loading…
x
Reference in New Issue
Block a user