Skip to content

Commit

Permalink
#2 add lib faker
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoujols committed Oct 8, 2023
1 parent d167f36 commit 1a09243
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 49 deletions.
162 changes: 113 additions & 49 deletions app/Controller/InitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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'}";
Expand Down
90 changes: 90 additions & 0 deletions app/Core/StandardRaw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Core;

/**
* Class StandardRaw
*
* @author Benoit Foujols
*/
class StandardRaw
{
/**
* Normalize to Standard Raw Upper
* Rules Raw -> 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));
}

}
66 changes: 66 additions & 0 deletions var/configDataset.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
}
}
}
}

}

0 comments on commit 1a09243

Please sign in to comment.