Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TP-PHP-LPPRISM
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sofiane Lasri
TP-PHP-LPPRISM
Commits
29d5ae87
Commit
29d5ae87
authored
2 years ago
by
Sofiane Lasri
Browse files
Options
Downloads
Patches
Plain Diff
TP7 Création de la fonction Objet::modifyObject
parent
ed87b442
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
models/Objet.php
+80
-29
80 additions, 29 deletions
models/Objet.php
with
80 additions
and
29 deletions
models/Objet.php
+
80
−
29
View file @
29d5ae87
...
...
@@ -44,35 +44,9 @@ class Objet
$result
[
"status"
]
=
"success"
;
$result
[
"message"
]
=
"Insertion réussie"
;
// On va vérifier que $tableName est bien en format alphanumérique
if
(
!
isAlphaNumeric
(
$tableName
)){
return
[
"status"
=>
"fail"
,
"message"
=>
"Le format du nom de la table est incorrect."
];
}
// Maintenant on va vérifier que la table existe
try
{
Database
::
pdo
()
->
query
(
"SELECT 1 FROM
$tableName
LIMIT 1"
);
}
catch
(
PDOException
$e
)
{
return
[
"status"
=>
"fail"
,
"message"
=>
"La table
$tableName
ne semble pas exister."
,
"pdoError"
=>
$e
];
}
// Nous pouvons commencer l'insertion
// On va vérifier que l'index de chaque est bien en format alphanumérique
foreach
(
$columns
as
$index
=>
$value
){
if
(
!
isAlphaNumeric
(
$index
)){
return
[
"status"
=>
"fail"
,
"message"
=>
"Le format "
.
htmlspecialchars
(
$index
)
.
" est incorrect."
];
}
$validation
=
Objet
::
verifyTableAndFormat
(
$tableName
,
$columns
);
if
(
$validation
[
"status"
]
!==
"success"
){
return
$validation
;
}
// On prépare la requête
...
...
@@ -124,4 +98,81 @@ class Objet
];
}
}
public
static
function
modifyObject
(
String
$tableName
,
array
$id
,
array
$columns
)
:
array
{
$result
[
"status"
]
=
"success"
;
$result
[
"message"
]
=
"Modification réussie"
;
$validation
=
Objet
::
verifyTableAndFormat
(
$tableName
,
$columns
);
if
(
$validation
[
"status"
]
!==
"success"
){
return
$validation
;
}
if
(
empty
(
$id
[
"name"
])
||
empty
(
$id
[
"value"
])
||
!
isAlphaNumeric
(
$id
[
"name"
])){
return
[
"status"
=>
"fail"
,
"message"
=>
"L'identifiant de la table envoyé est incorrect!"
];
}
// On prépare la requête
$queryString
=
(
"UPDATE
$tableName
SET "
.
http_build_query
(
$columns
,
''
,
','
)
.
" WHERE "
.
$id
[
"name"
]
.
"=:"
.
$id
[
"name"
]);
$insertQuery
=
Database
::
pdo
()
->
prepare
(
$queryString
);
try
{
// Et on l'exécute
$insertQuery
->
execute
(
$columns
);
}
catch
(
PDOException
$e
)
{
return
[
"status"
=>
"fail"
,
"message"
=>
"La modification de l'objet a échoué."
,
"pdoError"
=>
$e
];
}
return
$result
;
}
/**
* Permet de vérifier le format du nom de la table et des nom des colonnes + vérifier l'existance de la table.
* @param String $tableName
* @param array $columns
* @return array|string[]
*/
public
static
function
verifyTableAndFormat
(
string
$tableName
,
array
$columns
)
:
array
{
// On va vérifier que $tableName est bien en format alphanumérique
if
(
!
isAlphaNumeric
(
$tableName
)){
return
[
"status"
=>
"fail"
,
"message"
=>
"Le format du nom de la table est incorrect."
];
}
// Maintenant on va vérifier que la table existe
try
{
Database
::
pdo
()
->
query
(
"SELECT 1 FROM
$tableName
LIMIT 1"
);
}
catch
(
PDOException
$e
)
{
return
[
"status"
=>
"fail"
,
"message"
=>
"La table
$tableName
ne semble pas exister."
,
"pdoError"
=>
$e
];
}
// Nous pouvons commencer l'insertion
// On va vérifier que l'index de chaque est bien en format alphanumérique
foreach
(
$columns
as
$index
=>
$value
){
if
(
!
isAlphaNumeric
(
$index
)){
return
[
"status"
=>
"fail"
,
"message"
=>
"Le format "
.
htmlspecialchars
(
$index
)
.
" est incorrect."
];
}
}
return
[
"status"
=>
"success"
,
"message"
=>
"Validation réussie."
];
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment