samedi 6 avril 2019

How to use correct pattern for input data in PHP?

I'm looking for a pattern to accept input for reuse in PHP

<?php
namespace App\Invoice\Services;

use App\Invoice\Models\Invoice;
use Illuminate\Http\Request;

class InvoiceService
{
    /**
     * @param Request|array $input
     * @return Invoice
     */
    public function create($input)
    {
        $invoice = new Invoice();
        $product_id = null;

        if (gettype($input) === 'array')
            $product_id = $input['product_id'];
        else
            $product_id = $input->product->id;

        $invoice->product()->associate($product_id);
        $invoice->save();

        if (gettype($input) === Request::class)
        {
            if ($input->file)
                uploadFile($input->file);
        }

        return $invoice;
    }
}

and how to use:

public function storeInvoice(Request $request)
{
    $service->create($request);
}

or

public function storeOrder(Request $request)
{
    $array = ['product_id' => 123]];
    $service->create($array);
}

How to use a pattern? I know that I can use the Request class method to get attributes, but I also need to load the file.

Can I use the pattern builder or something? Thank you very much

Aucun commentaire:

Enregistrer un commentaire