PHP code example of mechta-market / php-enhance

1. Go to this page and download the library: Download mechta-market/php-enhance library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mechta-market / php-enhance example snippets


public function index(Request $request, SomeUsecase $usecase)
{
    $input = new SomeInput($request->get('product_id'));

    $usecase->setInput($input);

    $usecase->execute();

    $response = $usecase->getOutput();

    if ($response->isFailed()) {
        abort($response->getStatusCode(), $response->getArrayResponse());
    }

    return $response->getArrayResponse();
}

class SomeInput extends BaseInput
{
    public function __construct(private int $product_id){
    }

    public function getProductId(): int {
        return $this->product_id;
    }
}

class SomeUsecase extends BaseUsecase
{
    /**
     * @property SomeUsecaseData $data
     * @property SomeInput $input
     */
    public function execute(): void
    {
        try {
            $product_id = $this->input->getProductId();

            if ($product_id === 0) {
                throw new \Exception("Product id is zero");
            }

            $product = new Product(
                $product_id, "iPhone 15 Pro Max Ultra GigaByte",
                "iphone-15-super-puper", 999990
            );

            $this->data->setProduct($product);

        } catch (\Exception $ex) {
            $this->errors->addClientError($ex->getMessage(), 400);
        } catch (\Throwable $th) {
            $this->errors->addServerError($th->getMessage());
        }
    }

    protected function setData(): void
    {
        $this->data = new SomeUsecaseData();
    }
}

class SomeUsecaseData implements UsecaseDataInterface
{
    private Product $product;

    public function setProduct(Product $product): void {
        $this->product = $product;
    }

    public function getData(): array
    {
        return [
            "id" => $this->product->getId(),
            "name" => $this->product->getName(),
            "code" => $this->product->getCode(),
            "price" => $this->product->getPrice(),
        ];
    }
}