PHP code example of jot / hf-repository

1. Go to this page and download the library: Download jot/hf-repository 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/ */

    

jot / hf-repository example snippets

 


declare(strict_types=1);

namespace Jot\HfShield\Entity\Order;

use Jot\HfRepository\Entity;
use Jot\HfRepository\Trait\HasTimestamps;
use Jot\HfRepository\Trait\HasLogicRemoval;
use Hyperf\Swagger\Annotation as SA;

#[SA\Schema(schema: "app.entity.order.order")]
class Order extends Entity
{

    use HasLogicRemoval, HasTimestamps;

        #[SA\Property(
        property: "created_at",
        type: "string",
        format: "string",
        readOnly: true,
        x: ["php_type" => "\DateTime"]
    )]
    protected ?\DateTime $createdAt = null;

    #[SA\Property(
        property: "customer",
        ref: "#/components/schemas/app.entity.order.customer",
        x: ["php_type" => "\Jot\HfShield\Entity\Order\Customer"]
    )]
    protected ?\Jot\HfShield\Entity\Order\Customer $customer = null;

    #[SA\Property(
        property: "id",
        type: "string",
        example: "749ef2bd-1372-4ef2-998c-0cbec9bc1496"
    )]
    protected ?string $id = null;

    #[SA\Property(
        property: "installment_count",
        type: "integer",
        example: 5
    )]
    protected ?int $installmentCount = null;

    // ...

}




namespace App\Repository;

use Jot\HfRepository\Repository;
use Jot\HfShield\Entity\Order\Order as Entity;

class OrderRepository extends Repository
{
    protected string $entity = Entity::class;

}
 


declare(strict_types=1);

namespace App\Controller\V1;

use App\Controller\AbstractController;
use Jot\HfShield\Entity\Order\Order;
use App\Repository\OrderRepository;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\RateLimit\Annotation\RateLimit;
use Hyperf\Swagger\Annotation as SA;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;

#[SA\HyperfServer('http')]
#[SA\Tag(
    name: 'Order',
    description: 'Endpoints related to orders management'
)]
#[SA\Schema(schema: "app.error.response", ation",
                in: "query",
                    in: "query",
                      new SA\Property(
                            property: "error",
                            type: "string",
                            example: null,
                            nullable: true
                        )
                    ],
                    type: "object"
                )
            ),
            new SA\Response(
                response: 400,
                description: "Bad Request",
                content: new SA\JsonContent(ref: "#/components/schemas/app.error.response")
            ),
            new SA\Response(
                response: 500,
                description: "Application Error",
                content: new SA\JsonContent(ref: "#/components/schemas/app.error.response")
            )
        ]
    )]
    #[RateLimit(create: 10, consume: 1)]
    #[GetMapping('orders[/]')]
    public function getOrdersList(): PsrResponseInterface
    {
        $response = $this->repository->paginate($this->request->all());
        if ($response['result'] === 'error') {
            return $this->response->withStatus(400)->json($response);
        }
        return $this->response
            ->withHeader('Access-Control-Allow-Origin', '*')
            ->json($response);

    }

    #[SA\Get(
        path: "/orders/{id}",
        description: "Retrieve the details of a specific orders identified by ID.",
        summary: "Get Order Data",
        tags: ["Order"],
        parameters: [
            new SA\Parameter(
                name: "id",
                description: "Unique identifier of the orders",
                in: "path",
                s to the system.",
        summary: "Create a New Order",
        requestBody: new SA\RequestBody(
            /schemas/app.error.response")
            ),
            new SA\Response(
                response: 404,
                description: "Order Not Found",
                content: new SA\JsonContent(ref: "#/components/schemas/app.error.response")
            ),
            new SA\Response(
                response: 500,
                description: "Application Error",
                content: new SA\JsonContent(ref: "#/components/schemas/app.error.response")
            )
        ]
    )]
    #[RateLimit(create: 10, consume: 5)]
    #[PutMapping('orders/{id}')]
    public function updateOrder(string $id): PsrResponseInterface
    {
        $entity = new Order(['id' => $id, ...$this->request->all()]);

        try {
            $response = $this->repository->update($entity);
        } catch (\Throwable $e) {
            return $this->response->withStatus(400)->json([
                'data' => null,
                'result' => 'error',
                'message' => $e->getMessage()
            ]);
        }

        return $this->response->json([
            'data' => $response->toArray(),
            'result' => 'success',
            'message' => null,
        ]);
    }

    #[SA\Delete(
        path: "/orders/{id}",
        description: "Delete an existing orders by its unique identifier.",
        summary: "Delete an Order",
        tags: ["Order"],
        parameters: [
            new SA\Parameter(
                name: "id",
                description: "Unique identifier of the orders",
                in: "path",
                
shell
php bin/hyperf.php vendor:publish hyperf/redis
php bin/hyperf.php vendor:publish hyperf/rate-limit
php bin/hyperf.php vendor:publish hyperf/swagger
php bin/hyperf.php vendor:publish jot/hf_elastic
shell
php bin/hyperf.php repo:crud --index=orders
shell
php bin/hyperf.php repo:entity --index=orders
diff
+ [OK] ./app/Entity/Order/Customer.php
+ [OK] ./app/Entity/Order/Invoice.php
+ [OK] ./app/Entity/Order/Item.php
+ [OK] ./app/Entity/Order/OrderHistory.php
+ [OK] ./app/Entity/Order/Payment.php
+ [OK] ./app/Entity/Order/Shipment.php
+ [OK] ./app/Entity/Order/Order.php
shell
php bin/hyperf.php repo:repository --index=orders
diff 
+ [OK] ./app/Repository/OrderRepository.php
shell
php bin/hyperf.php repo:controller --index=orders
diff
+ [OK] ./app/Controller/V1/OrderController.php
shell
php bin/hyperf.php repo:controller --index=orders --api-version=v2
shell
php bin/hyperf.php repo:controller --index=orders

The file ./app/Controller/V1/OrderController.php already exists. Overwrite file? [y/n/a] [n]:
shell
php bin/hyperf.php repo:controller --index=orders --force