diff --git a/app/Controller/InitController.php b/app/Controller/InitController.php index 5ffeaf1..086ec9d 100644 --- a/app/Controller/InitController.php +++ b/app/Controller/InitController.php @@ -11,47 +11,44 @@ namespace Controller; +use Core\StandardRaw; +use Core\TokenHandler; use Studoo\EduFramework\Core\Controller\ControllerInterface; use Studoo\EduFramework\Core\Controller\Request; use Core\DatabaseService; class InitController implements ControllerInterface { + /** + * @throws \JsonException + */ public function execute(Request $request): string|null { - $db = DatabaseService::getConnect(); + if (file_exists(__DIR__ . '/../../var/ecoledirecte.db')) { + unlink(__DIR__ . '/../../var/ecoledirecte.db'); + } + + ##<< Insertion des conf dataset + if (file_exists(__DIR__ . '/../../var/configDataset.json')) { + $dataSet = json_decode( + file_get_contents(__DIR__ . '/../../var/configDataset.json'), + true, + 512, + JSON_THROW_ON_ERROR + ); - ## Création de la table classes - $db->exec("CREATE TABLE IF NOT EXISTS classes ( + $db = DatabaseService::getConnect(); + + ## Création de la table classes + $db->exec("CREATE TABLE IF NOT EXISTS classes ( id INTEGER PRIMARY KEY, libelle TEXT, code TEXT ) "); - ##<< Insertion des classes - $classes = [ - "107" => [ - "libelle" => "1 TS SIO A", - "code" => "1TSSIOA" - ], - "140" => [ - "libelle" => "Manager Solutions Digitals et Data 2ème année alternance", - "code" => "MS2D2ALT" - ], - "150" => [ - "libelle" => "1 TS SIO B", - "code" => "1TSSIOB" - ], - "102" => [ - "libelle" => "2TS SIO SLAM", - "code" => "2TSSIOSLAM" - ] - ]; - ##>> Insertion des classes - - ## Création de la table users - $db->exec("CREATE TABLE IF NOT EXISTS users ( + ## Création de la table users + $db->exec("CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, idLogin INTEGER, nom TEXT, @@ -60,7 +57,7 @@ public function execute(Request $request): string|null sexe TEXT, email TEXT, typeCompte TEXT, - classeId INTEGER, + classeId TEXT, login TEXT, password TEXT, uid TEXT, @@ -72,31 +69,98 @@ classeId INTEGER, "); - $faker = \Faker\Factory::create('fr_FR'); - - ##<< Insertion des users dans la classe - foreach ($classes as $id => $classe) { - $db->exec("INSERT INTO classes (id, libelle, code) VALUES ($id, '" . $classe['libelle'] . "', '" . $classe['code'] . "')"); + $faker = \Faker\Factory::create('fr_FR'); - $genre = ['male', 'female']; - $pos = array_rand($genre); + ##<< Insertion des users dans la classe + foreach ($dataSet["organisation"]["v3"]["classes"] as $id => $classe) { + $db->exec("INSERT INTO classes (id, libelle, code) VALUES ($id, '" . $classe['libelle'] . "', '" . $classe['code'] . "')"); - $nbEleves = rand(20, 30); - for ($i = 0; $i < $nbEleves; $i++) { - $nom = $faker->lastName($genre[$pos]); - $prenom = $faker->firstName($genre[$pos]); - $telPortable = $faker->phoneNumber(); - $sexe = strtoupper(substr($genre[$pos], 0, 1)); - $email = $faker->email(); + ##<< Global etudiants dataset + $idClasse = '{ "id": ' . $id . ', "libelle": "' . $classe['libelle'] . '", "code": "' . $classe['code'] . '" }'; $typeCompte = "E"; - $login = $nom . '.' . $prenom; - $password = "test"; - $uid = "uid" . $i; - $tokenExpiration = date("Y-m-d H:i:s"); - $lastConnexion = date("Y-m-d H:i:s"); - $anneeScolaireCourante = "2020-2021"; - $db->exec("INSERT INTO users (idLogin, nom, prenom, telPortable, sexe, email, typeCompte, classeId, login, password, uid, tokenExpiration, lastConnexion, anneeScolaireCourante) VALUES ($id, '$nom', '$prenom', '$telPortable', '$sexe', '$email', '$typeCompte', $id, '$login', '$password', '$uid', '$tokenExpiration', '$lastConnexion', '$anneeScolaireCourante')"); - } + + ##<< Insertion des etudiants dataset + if (array_key_exists('etudiants', $classe)) { + $db->exec("INSERT INTO users ( idLogin, + nom, + prenom, + telPortable, + sexe, + email, + typeCompte, + classeId, + login, + password, + uid, + tokenExpiration, + lastConnexion, + anneeScolaireCourante) + VALUES ( '" . $classe['etudiants'][1]['idLogin'] . "', + '" . $classe['etudiants'][1]['nom'] . "', + '" . $classe['etudiants'][1]['prenom'] . "', + '" . $classe['etudiants'][1]['telPortable'] . "', + '" . $classe['etudiants'][1]['sexe'] . "', + '" . $classe['etudiants'][1]['email'] . "', + '$typeCompte', + '$idClasse', + '" . $classe['etudiants'][1]['login'] . "', + '" . $classe['etudiants'][1]['password'] . "', + '" . $classe['etudiants'][1]['uid'] . "', + '" . $classe['etudiants'][1]['tokenExpiration'] . "', + '" . $classe['etudiants'][1]['lastConnexion'] . "', + '" . $classe['etudiants'][1]['anneeScolaireCourante'] . "' + ) + "); + } ##>> Insertion des etudiants dataset + + ##<< Insertion des etudiants faker + $genre = ['male', 'female']; + $posGenre = array_rand($genre); + $nbEleves = rand(15, 35); + for ($i = 0; $i < $nbEleves; $i++) { + $idLogin = $faker->numberBetween(1000, 999999); + $nom = $faker->lastName($genre[$posGenre]); + $prenom = $faker->firstName($genre[$posGenre]); + $telPortable = $faker->phoneNumber(); + $sexe = strtoupper(substr($genre[$posGenre], 0, 1)); + $email = $faker->email(); + + $login = (new \Core\StandardRaw)->normalizeSRString(substr($prenom, 0, 1)).(new \Core\StandardRaw)->normalizeSRString($nom).$faker->numberBetween(1000, 9999); + $password = "test"; + $uid = $faker->uuid(); + $tokenExpiration = date("Y-m-d H:i:s"); + $lastConnexion = date("Y-m-d H:i:s"); + $anneeScolaireCourante = $dataSet["organisation"]["v3"]["promo"]; + $db->exec("INSERT INTO users ( idLogin, + nom, + prenom, + telPortable, + sexe, + email, + typeCompte, + classeId, login, + password, + uid, + tokenExpiration, + lastConnexion, + anneeScolaireCourante) + VALUES ( $idLogin, + '$nom', + '$prenom', + '$telPortable', + '$sexe', + '$email', + '$typeCompte', + '$idClasse', + '$login', + '$password', + '$uid', + '$tokenExpiration', + '$lastConnexion', + '$anneeScolaireCourante') + "); + } ##>> Insertion des etudiants faker + } ##>> Insertion des conf dataset } return "{message: 'initialisation the mock API Ecole Directe'}"; diff --git a/app/Core/StandardRaw.php b/app/Core/StandardRaw.php new file mode 100644 index 0000000..aa11ea0 --- /dev/null +++ b/app/Core/StandardRaw.php @@ -0,0 +1,90 @@ + Clean -> Upper -> Raw (Standard) + * @param String $raw + * @return String|null + */ + public function normalizeSRString(string $raw, bool $space = false): ?string + { + return ($space === true) ? strtoupper($this->clean($this->cleanBeginToEndWhiteSpace($raw))) : strtoupper($this->clean($raw)); + } + + /** + * Normalize to Standard Raw UCFirst + * Rules Raw -> Clean -> UCFirst -> Raw (Standard) + * @param String $raw + * @return String|null + */ + public function normalizeSRSUcfirst(string $raw, bool $space = false): ?string + { + return ($space === true) ? ucfirst(strtolower($this->clean($this->cleanBeginToEndWhiteSpace($raw)))) : ucfirst(strtolower($this->clean($raw))); + } + + + /** + * Normalize to Standard Raw UTF8 + * Rules Raw -> Clean -> Raw (Standard) + * @param String $raw Source à nettoyer + * @param bool $space Supprimer les espaces avant et après la source + * @param bool $nonbreaking Supprimer les + * @return String|null + */ + public function normalizeSRUtf8(string $raw, bool $space = false, bool $nonbreaking = false): ?string + { + return ($space === true) ? $this->clean($this->cleanBeginToEndWhiteSpace($raw), $nonbreaking) : $this->clean($raw, $nonbreaking); + } + + /** + * Clean String UTF8 to Normal + * @param String $text + * @return String|null + */ + private function clean(string $text, bool $nonbreaking = false): ?string + { + $utf8 = array( + '/[áàâãªä]/u' => 'a', + '/[ÁÀÂÃÄ]/u' => 'A', + '/[ÍÌÎÏ]/u' => 'I', + '/[íìîï]/u' => 'i', + '/[éèêë]/u' => 'e', + '/[ÉÈÊË]/u' => 'E', + '/[óòôõºö]/u' => 'o', + '/[ÓÒÔÕÖ]/u' => 'O', + '/[úùûü]/u' => 'u', + '/[ÚÙÛÜ]/u' => 'U', + '/ç/' => 'c', + '/Ç/' => 'C', + '/ñ/' => 'n', + '/Ñ/' => 'N', + '/–/' => '-', // UTF-8 hyphen to "normal" hyphen + '/[’‘‹›‚]/u' => ' ', // Literally a single quote + '/[“”«»„]/u' => ' ', // Double quote + '/[.]/' => '', + ); + $utf8['/ /'] = ($nonbreaking === true) ? ' ' : '-'; // nonbreaking space (equiv. to 0x160) + $utf8['/-/'] = ($nonbreaking === true) ? ' ' : '-'; + return preg_replace(array_keys($utf8), array_values($utf8), $text); + } + + /** + * Clean whitespace Begin To End + * @param String $raw + * @return String|null + */ + private function cleanBeginToEndWhiteSpace(string $raw): ?string + { + return rtrim(ltrim($raw)); + } + +} diff --git a/var/configDataset.json b/var/configDataset.json new file mode 100644 index 0000000..bd49172 --- /dev/null +++ b/var/configDataset.json @@ -0,0 +1,66 @@ +{ + "organisation" : { + "v3" : { + "promo" : "2023-2024", + "classes" : { + "107" : { + "libelle" : "1 TS SIO A", + "code" : "1TSSIOA", + "etudiants" : { + "1" : { + "nom": "Boukari", + "prenom": "Abdoul", + "email": "test@test.fo", + "telPortable": "0606060606", + "sexe": "M", + "login": "ABOUKARI", + "idLogin" : "ABOUKARI", + "password": "ABOUKARI", + "uid": "98432dd9-c483-3669-a31e-153fec2eac72", + "tokenExpiration": "2023-10-08 10:10:03", + "lastConnexion": "2023-10-08 10:10:03", + "anneeScolaireCourante": "2020-2021" + } + } + }, + "120" : { + "libelle" : "1 TS SIO B", + "code" : "1TSSIOB" + }, + "134" : { + "libelle" : "1 TS SIO - année alternance", + "code" : "1TSSIOALT" + }, + "109" : { + "libelle" : "2TS SIO SLAM", + "code" : "2TSSIOSLAM" + }, + "130" : { + "libelle" : "2TS SIO SISR", + "code" : "2TSSIOSISR" + }, + "152" : { + "libelle" : "2TS SIO SLAM - année alternance", + "code" : "2TSSIOSLAMALT" + }, + "162" : { + "libelle" : "2TS SIO SISR - année alternance", + "code" : "2TSSIOSISRALT" + }, + "140" : { + "libelle" : "Bachelor CSI - année alternance", + "code" : "CSIALT" + }, + "148" : { + "libelle" : "Manager Solutions Digitals et Data 1ème - année alternance", + "code" : "MS2D1ALT" + }, + "141" : { + "libelle" : "Manager Solutions Digitals et Data 2ème - année alternance", + "code" : "MS2D2ALT" + } + } + } + } + +} \ No newline at end of file