[PHP-NOTES] note 130581 added to class.yaf-router

[email protected] ("[email protected]") Thu, 18 Dec 2025 14:09:37 +0000
Newsgroups php.notes
Message-ID <[email protected]>
<?php
namespace app\controller;

use Exception;
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()
    {
        $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
        $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
        $price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT);
        $quantity = filter_input(INPUT_POST, 'quantity', FILTER_VALIDATE_INT);

        if ($id === false || $id == null 
            || $price === false || $price == null 
                || $quantity === false || $quantity === null 
                    || $name === false || $name === null) {
            $error = "Error : Les entrées doivent etre au bon format !";
            require "view" . DIRECTORY_SEPARATOR . "addProduct.php";
            return;
        }

        $productInsert = new \app\model\entity\ProductEntity();
        $productInsert->setId($id);
        $productInsert->setName($name);
        $productInsert->setPrice($price);
        $productInsert->setQuantity($quantity);

        try {
            $product = $this->repository->add($productInsert);
            require "view" . DIRECTORY_SEPARATOR . "displayProduct.php";
        } catch (Exception $e) {
            $error = $e->getMessage();
            require "view" . DIRECTORY_SEPARATOR . "addProduct.php";
        }

    }

    function display(array $params)
    {
        //$params est positionné par le routeur
        //comme étant un tableau indexé contenant les paramètres
        //TODO
    }

    function update(array $params)
    {
        $id = $params[0];
        require "view" . DIRECTORY_SEPARATOR . "updateProduct.php";
    }

    function updateProduct()
    {
        //TODO
    }

    function delete(array $params)
    {
        //TODO
    }
}
----
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/130581
Del: integrated  -- https://main.php.net/note/delete/130581/integrated
Del: useless     -- https://main.php.net/note/delete/130581/useless
Del: bad code    -- https://main.php.net/note/delete/130581/bad+code
Del: spam        -- https://main.php.net/note/delete/130581/spam
Del: non-english -- https://main.php.net/note/delete/130581/non-english
Del: in docs     -- https://main.php.net/note/delete/130581/in+docs
Del: other reasons-- https://main.php.net/note/delete/130581
Reject      -- https://main.php.net/note/reject/130581
Search      -- https://main.php.net/manage/user-notes.php