386 lines
7.1 KiB
PHP
386 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* =========================================================
|
|
* API : CREATION D'UN ENREGISTREMENT DANS indice
|
|
* =========================================================
|
|
*
|
|
* Méthode :
|
|
* POST
|
|
*
|
|
* Content-Type :
|
|
* application/json
|
|
*
|
|
* Données attendues :
|
|
*
|
|
* {
|
|
* "indice": "CAC40",
|
|
* "nom": "Accor",
|
|
* "isin": "FR0000120404",
|
|
* "symbole": "AC",
|
|
* "actif": 1,
|
|
* "afficher": 1
|
|
* }
|
|
*
|
|
* =========================================================
|
|
*/
|
|
|
|
require_once __DIR__ . '/database.php';
|
|
|
|
header(
|
|
'Content-Type: application/json; charset=utf-8'
|
|
);
|
|
|
|
/*
|
|
* =========================================================
|
|
* FONCTION REPONSE JSON
|
|
* =========================================================
|
|
*/
|
|
|
|
function reponseJson(
|
|
array $data,
|
|
int $httpCode = 200
|
|
): never {
|
|
|
|
http_response_code($httpCode);
|
|
|
|
echo json_encode(
|
|
$data,
|
|
JSON_UNESCAPED_UNICODE |
|
|
JSON_UNESCAPED_SLASHES
|
|
);
|
|
|
|
exit;
|
|
}
|
|
|
|
/*
|
|
* =========================================================
|
|
* VERIFICATION METHODE HTTP
|
|
* =========================================================
|
|
*/
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Méthode HTTP non autorisée. Utilisez POST.'
|
|
],
|
|
405
|
|
);
|
|
}
|
|
|
|
/*
|
|
* =========================================================
|
|
* LECTURE DU JSON
|
|
* =========================================================
|
|
*/
|
|
|
|
$contenu = file_get_contents('php://input');
|
|
|
|
if ($contenu === false || trim($contenu) === '') {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Aucune donnée reçue.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
$data = json_decode(
|
|
$contenu,
|
|
true
|
|
);
|
|
|
|
/*
|
|
* Vérification JSON
|
|
*/
|
|
|
|
if (
|
|
!is_array($data) ||
|
|
json_last_error() !== JSON_ERROR_NONE
|
|
) {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Les données JSON sont invalides.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
/*
|
|
* =========================================================
|
|
* RECUPERATION DES CHAMPS
|
|
* =========================================================
|
|
*/
|
|
|
|
$indice = isset($data['indice'])
|
|
? trim((string) $data['indice'])
|
|
: '';
|
|
|
|
$nom = isset($data['nom'])
|
|
? trim((string) $data['nom'])
|
|
: '';
|
|
|
|
$isin = isset($data['isin'])
|
|
? strtoupper(trim((string) $data['isin']))
|
|
: '';
|
|
|
|
$symbole = isset($data['symbole'])
|
|
? strtoupper(trim((string) $data['symbole']))
|
|
: '';
|
|
|
|
$actif = isset($data['actif'])
|
|
? (int) $data['actif']
|
|
: 1;
|
|
|
|
$afficher = isset($data['afficher'])
|
|
? (int) $data['afficher']
|
|
: 1;
|
|
|
|
/*
|
|
* =========================================================
|
|
* VALIDATION
|
|
* =========================================================
|
|
*/
|
|
|
|
if ($indice === '') {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Le champ indice est obligatoire.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
if ($nom === '') {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Le champ nom est obligatoire.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
if ($isin === '') {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Le champ ISIN est obligatoire.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
if ($symbole === '') {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Le champ symbole est obligatoire.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
/*
|
|
* =========================================================
|
|
* VALIDATION ISIN
|
|
* =========================================================
|
|
*
|
|
* Un ISIN comporte normalement 12 caractères.
|
|
*
|
|
* =========================================================
|
|
*/
|
|
|
|
if (strlen($isin) !== 12) {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Le code ISIN doit comporter exactement 12 caractères.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
/*
|
|
* Vérification du format général :
|
|
*
|
|
* 2 lettres pays
|
|
* 9 caractères
|
|
* 1 chiffre de contrôle
|
|
*/
|
|
|
|
if (
|
|
!preg_match(
|
|
'/^[A-Z]{2}[A-Z0-9]{9}[0-9]$/',
|
|
$isin
|
|
)
|
|
) {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Le format du code ISIN est invalide.'
|
|
],
|
|
400
|
|
);
|
|
}
|
|
|
|
/*
|
|
* =========================================================
|
|
* NORMALISATION DES BOOLÉENS
|
|
* =========================================================
|
|
*/
|
|
|
|
$actif = $actif === 1 ? 1 : 0;
|
|
|
|
$afficher = $afficher === 1 ? 1 : 0;
|
|
|
|
/*
|
|
* =========================================================
|
|
* INSERTION MYSQL
|
|
* =========================================================
|
|
*/
|
|
|
|
try {
|
|
|
|
$sql = "
|
|
INSERT INTO indice
|
|
(
|
|
indice,
|
|
nom,
|
|
isin,
|
|
symbole,
|
|
actif,
|
|
afficher
|
|
)
|
|
VALUES
|
|
(
|
|
:indice,
|
|
:nom,
|
|
:isin,
|
|
:symbole,
|
|
:actif,
|
|
:afficher
|
|
)
|
|
";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
$stmt->execute(
|
|
[
|
|
':indice' => $indice,
|
|
':nom' => $nom,
|
|
':isin' => $isin,
|
|
':symbole' => $symbole,
|
|
':actif' => $actif,
|
|
':afficher' => $afficher
|
|
]
|
|
);
|
|
|
|
/*
|
|
* ID créé
|
|
*/
|
|
|
|
$id = (int) $pdo->lastInsertId();
|
|
|
|
/*
|
|
* =====================================================
|
|
* RELIRE L'ENREGISTREMENT CREE
|
|
* =====================================================
|
|
*/
|
|
|
|
$sqlLecture = "
|
|
SELECT
|
|
id,
|
|
indice,
|
|
nom,
|
|
isin,
|
|
symbole,
|
|
actif,
|
|
afficher,
|
|
updated_at,
|
|
created_at
|
|
FROM indice
|
|
WHERE id = :id
|
|
LIMIT 1
|
|
";
|
|
|
|
$stmtLecture = $pdo->prepare(
|
|
$sqlLecture
|
|
);
|
|
|
|
$stmtLecture->execute(
|
|
[
|
|
':id' => $id
|
|
]
|
|
);
|
|
|
|
$enregistrement =
|
|
$stmtLecture->fetch();
|
|
|
|
/*
|
|
* =====================================================
|
|
* REPONSE
|
|
* =====================================================
|
|
*/
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'success',
|
|
'message' => 'Enregistrement créé avec succès.',
|
|
'id' => $id,
|
|
'data' => $enregistrement
|
|
],
|
|
201
|
|
);
|
|
} catch (PDOException $e) {
|
|
|
|
/*
|
|
* =====================================================
|
|
* GESTION ERREUR MYSQL
|
|
* =====================================================
|
|
*/
|
|
|
|
error_log(
|
|
'api_create_table_indice.php : ' .
|
|
$e->getMessage()
|
|
);
|
|
|
|
/*
|
|
* Détection d'une contrainte UNIQUE
|
|
*/
|
|
|
|
if ((int) $e->errorInfo[1] === 1062) {
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Cet enregistrement existe déjà.'
|
|
],
|
|
409
|
|
);
|
|
}
|
|
|
|
reponseJson(
|
|
[
|
|
'status' => 'error',
|
|
'message' => 'Erreur lors de la création de l\'enregistrement.'
|
|
],
|
|
500
|
|
);
|
|
}
|