Simplify the cloud generation code
This commit is contained in:
parent
31d43467ce
commit
12045275d5
1 changed files with 37 additions and 22 deletions
57
db.php
57
db.php
|
@ -131,16 +131,40 @@ class DataBase
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public function getWordsList(string $id)
|
/**
|
||||||
|
* Get the list of the words inside the cloud
|
||||||
|
*
|
||||||
|
* If there is no $code parameter and the cloud isn't already loaded,
|
||||||
|
* return null.
|
||||||
|
*
|
||||||
|
* Return a list of words, orderer from the most used to the least,
|
||||||
|
* with the following fields:
|
||||||
|
* - word => the word
|
||||||
|
* - count => the number of occurences of the word
|
||||||
|
* - relative => the relative number of occurences,
|
||||||
|
* relative to the occurence max
|
||||||
|
* - percent => the percent of word in the cloud,
|
||||||
|
* relative to the total number of words
|
||||||
|
* @param string $code Code of the cloud
|
||||||
|
* @return array|null The words list
|
||||||
|
*/
|
||||||
|
public function getWordsList(string $code = null)
|
||||||
{
|
{
|
||||||
|
if (isset($code)) {
|
||||||
|
$this->loadCloudByCode($code);
|
||||||
|
}
|
||||||
|
if (!isset($code) && !$this->isCloudSet()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$stmt = $this->db->prepare("
|
$stmt = $this->db->prepare("
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM words
|
FROM words
|
||||||
JOIN clouds
|
JOIN clouds
|
||||||
ON words.cloud_id = clouds.id_cloud
|
ON words.cloud_id = clouds.id_cloud
|
||||||
WHERE code = :id;
|
WHERE code = :code;
|
||||||
");
|
");
|
||||||
$stmt->bindValue(':id', $id);
|
$stmt->bindValue(':code', $this->cloud['code']);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$words = [];
|
$words = [];
|
||||||
$values = [];
|
$values = [];
|
||||||
|
@ -193,34 +217,25 @@ class DataBase
|
||||||
$duration = self::DEFAULT_DURATION;
|
$duration = self::DEFAULT_DURATION;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cpt = 0;
|
for ($i = 0; $i < 10; $i++) {
|
||||||
$codeIsUsed = true;
|
|
||||||
while ($codeIsUsed && $cpt < 0) {
|
|
||||||
$code = bin2hex(random_bytes(self::CLOUD_CODE_LENGTH));
|
$code = bin2hex(random_bytes(self::CLOUD_CODE_LENGTH));
|
||||||
$stmt = $this->db->prepare("
|
if (!$this->loadCloudByCode($code)) {
|
||||||
SELECT *
|
echo 'LOAD CLOUD';
|
||||||
FROM clouds
|
break;
|
||||||
WHERE code = :code;
|
|
||||||
");
|
|
||||||
$stmt->bindValue(':code', $code);
|
|
||||||
$stmt->execute();
|
|
||||||
if (!$data = $stmt->fetch()) {
|
|
||||||
$codeIsUsed = false;
|
|
||||||
}
|
}
|
||||||
$cpt++;
|
|
||||||
}
|
}
|
||||||
if ($codeIsUsed) {
|
if ($this->isCloudSet()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$duration = date('Y-m-d H:i:s', strtotime(self::OPTIONS_DURATION[$duration]));
|
$delete = date('Y-m-d H:i:s', strtotime(self::OPTIONS_DURATION[$duration]));
|
||||||
$stmt = $this->db->prepare("
|
$stmt = $this->db->prepare("
|
||||||
INSERT INTO clouds(code, text, size, delete_t)
|
INSERT INTO clouds(code, text, size, delete_t)
|
||||||
VALUES (:code, :text, :size, :duration);
|
VALUES (:code, :text, :size, :delete);
|
||||||
");
|
");
|
||||||
$stmt->bindValue(':code', $code, PDO::PARAM_STR);
|
$stmt->bindValue(':code', $code, PDO::PARAM_STR);
|
||||||
$stmt->bindValue(':text', $text, PDO::PARAM_STR);
|
$stmt->bindValue(':text', $text, PDO::PARAM_STR);
|
||||||
$stmt->bindValue(':size', $size, PDO::PARAM_INT);
|
$stmt->bindValue(':size', $size, PDO::PARAM_INT);
|
||||||
$stmt->bindValue(':duration', $duration, PDO::PARAM_STR);
|
$stmt->bindValue(':delete', $delete, PDO::PARAM_STR);
|
||||||
try {
|
try {
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
} catch (PDOEXception $e) {
|
} catch (PDOEXception $e) {
|
||||||
|
@ -230,7 +245,7 @@ class DataBase
|
||||||
'id' => $this->db->lastInsertId(),
|
'id' => $this->db->lastInsertId(),
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
'size' => $size,
|
'size' => $size,
|
||||||
'delete_t' => $duration,
|
'delete_t' => $delete,
|
||||||
'text' => $text,
|
'text' => $text,
|
||||||
);
|
);
|
||||||
return $this->cloud;
|
return $this->cloud;
|
||||||
|
|
Loading…
Reference in a new issue