PHP code example of uniondrug / structs

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

    

uniondrug / structs example snippets


return [
    'default' => [
        ......
        'providers'           => [
            ......
            \Uniondrug\Structs\StructManagerServiceProvider::class,
        ],
    ],
];


use Uniondrug\Structs\Struct;

class OrderItemStruct extends Struct
{
    /**
     * @var string
     */
     public $name;

    /**
     * @var float
     */
     public $unitPrice;

    /**
     * @var int[]
     */
     public $cats;
}

class MemberStruct extends Struct
{
    /**
     * @var string
     */
     public $username;

    /**
     * @var string
     */
     public $mobile;
}

/**
 * @property float $price
 */
class OrderStruct extends Struct
{
    /**
     * @var string
     */
     public $orderNo;

    /**
     * @var float
     */
     protected $price = 0.0;

    /**
     * @var MemberStruct
     */
     public $customer;

    /**
     * @var OrderItemStruct[]
     */
     public $items;
}

/**
 * @property float $price
 */
class OrderStruct extends Struct
{
    /**
     * @var string
     * @Validatior(type=string, rice = 0.0;

    /**
     * @var MemberStruct
     */
     public $customer;

    /**
     * @var OrderItemStruct[]
     */
     public $items;
}

class IndexController extents Controller
{
    public function indexAction()
    {
        $input = $this->request->getJsonRawBody();

        $orderStruct = OrderStruct::factory($input);

        ....
    }
}


$orderItem = OrderItemStruct::factory([
    'name' => '板蓝根冲剂',
    'unitPrice' => '12.30',
]);


$order = Order::fineFirst(1);
$orderStruct = OrderStruct::factory($order);


namespace App\Structs;

class AreaPaginatorStruct extends PaginatorStruct
{
    /**
     * @var \App\Structs\AreaStruct[]
     */
    public $body = [];
}

....

        // 分页查询
        $queryBuilder = new QueryBuilder(
            [
                'builder' => $builder,
                'limit'   => $limit,
                'page'    => $page,
            ]
        );

        // 构造结构体
        return AreaPaginatorStruct::factory($queryBuilder->getPaginate());


[
    'body' => [
       ...
    ],
    'paging' => [
       ...
    ]
]