note 130579 added to class.yaf-router

From: Date: Thu, 18 Dec 2025 13:08:53 +0000
Subject: note 130579 added to class.yaf-router
Groups: php.notes 
Request: Send a blank email to php-notes+get-222462@lists.php.net to get a copy of this message
<?php namespace app\controller; use app\controller\Home; class Product { private \app\model\repository\ProductRepositoryInterface $repository; public function __construct() { $this->repository = new \app\model\repository\DbProductRepository(); } function add() { require "view" . DIRECTORY_SEPARATOR . "addProduct.php"; } function addProduct() { if (isset($_POST["name"]) && isset($_POST["price"]) && isset($_POST["quantity"])) { $name = $_POST["name"]; $price = $_POST["price"]; $quantity = $_POST["quantity"]; $id = random_int(1, 100000); while ($this->repository->findById($id) !== null) { $id = random_int(1, 100000); } $products = $this->repository->findAll(); $nomsproduits = array_map(function ($prod) { return $prod['name']; }, $products); if (in_array($name, $nomsproduits, true)) { throw new \Exception("Le nom du produit existe déjà."); } if ($price < 0) { throw new \Exception("Le prix du produit ne peut pas être négatif."); } if ($quantity < 0) { throw new \Exception("La quantité du produit ne peut pas être négative."); } $nproduit = new \app\model\entity\ProductEntity(); $nproduit->setId($id); $nproduit->setName($name); $nproduit->setPrice((float)$price); $nproduit->setQuantity((int)$quantity); $this->repository->add($nproduit); } $this->display([$id]); } public function display(array $params) { $produitcherche = null; if (isset($params[0])) { $produitcherche = $this->repository->findById(intval($params[0])); } if ($produitcherche === null) { throw new \Exception("Produit non trouvé pour l'affichage."); } require "view" . DIRECTORY_SEPARATOR . "displayProduct.php"; } function update(array $params) { $produitcherche = null; if (isset($params[0])) { $produitcherche = $this->repository->findById(intval($params[0])); } if ($produitcherche === null) { throw new \Exception("Produit non trouvé pour la mise à jour."); } require "view" . DIRECTORY_SEPARATOR . "updateProduct.php"; } function updateProduct() { if (isset($_POST["name"]) && isset($_POST["price"]) && isset($_POST["quantity"]) && isset($_POST["id"])) { $id = $_POST["id"]; $name = $_POST["name"]; $price = $_POST["price"]; $quantity = $_POST["quantity"]; $products = $this->repository->findAll(); $nomsproduits = array_map(function ($prod) use ($id) { if ($id != $prod['id']) { return $prod['name']; } }, $products); if (in_array($name, $nomsproduits, true)) { throw new \Exception("Le nom du produit existe déjà."); } if ($price < 0) { throw new \Exception("Le prix du produit ne peut pas être négatif."); } if ($quantity < 0) { throw new \Exception("La quantité du produit ne peut pas être négative."); } $nproduit = new \app\model\entity\ProductEntity(); $nproduit->setId($id); $nproduit->setName($name); $nproduit->setPrice((float)$price); $nproduit->setQuantity((int)$quantity); $this->repository->update($id, $nproduit); } $this->display([$id]); } function delete(array $params) { if (isset($params[0])) { $id = intval($params[0]); $this->repository->delete($id); } header("Location: /workspace/code_demo/04-MVC/app/Home"); } } ---- Server IP: 206.189.2.9 Probable Submitter: 2001:660:7220:385:193:52:103:33 ---- Manual Page -- https://php.net/manual/en/class.yaf-router.php Edit -- https://main.php.net/note/edit/130579 Del: integrated -- https://main.php.net/note/delete/130579/integrated Del: useless -- https://main.php.net/note/delete/130579/useless Del: bad code -- https://main.php.net/note/delete/130579/bad+code Del: spam -- https://main.php.net/note/delete/130579/spam Del: non-english -- https://main.php.net/note/delete/130579/non-english Del: in docs -- https://main.php.net/note/delete/130579/in+docs Del: other reasons-- https://main.php.net/note/delete/130579 Reject -- https://main.php.net/note/reject/130579 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222462) next »