saidqb/core-php

core-php

v8.1.5 2024-07-24 02:24 UTC

This package is auto-updated.

Last update: 2024-09-24 02:49:23 UTC


README

PHP 项目的依赖项

描述

进度构建...

要求

PHP >= 8.1

安装

composer require saidqb/core-php

包包含

Arr

use Saidqb\CorePhp\Lib\Arr;

$arr = [1, 2, 3];
Arr::collection($arr)->filter(function($element) {
    return $element > 1;
}); // [2, 3]

Arr::collection($arr)->contains(1); // true

Arr::collection($arr)->filter(function($element) {
    return $element > 1;
}); // [2, 3]

详情 文档

Str

use Saidqb\CorePhp\Lib\Str;

$str = 'string';
Str::make($str)->startsWith($substring);
Str::make($str)->endsWith($substring);

详情 文档

生成

use Saidqb\CorePhp\Lib\Generate;


Generate::str()->lowerAlpha($length);
Generate::str()->upperAlpha($length);
Generate::str()->mixedAlpha($length);
Generate::str()->numeric($length);
Generate::str()->alphaNumeric($length);
Generate::str()->special($length);
Generate::str()->all($length);
Generate::str()->custom($length, $charset);

详情 文档

分页

use Saidqb\CorePhp\Pagination;

$pagination = Pagination::make()->totalItems(100)->itemPerPage(10)->currentPage(1)->get();

响应

use Saidqb\CorePhp\Response;

Response::make()->response([], ResponseCode::HTTP_OK, ResponseCode::HTTP_OK_MESSAGE, 0)->send();

// list item
Response::make()->response(['items' => $items, 'pagination' => $pagination])->send();

// single item
Response::make()->response(['item' => $item])->send();

在控制器中使用

BaseController

use Saidqb\CorePhp\Response;
use Saidqb\CorePhp\ResponseCode;

public $res;

public function __construct()
{
    $this->initResponse();
}

public function initResponse()
{
    $this->res = new Response();
    return $this->res;
}

public function response($data, $code = ResponseCode::HTTP_OK, $message = ResponseCode::HTTP_OK_MESSAGE, $errorCode = 0)
{
    $this->res->response($data, $code, $message, $errorCode)->send();
}

扩展到 BaseController

use Saidqb\CorePhp\ResponseCode;

public function __construct()
{
    parent::__construct();

    $this->initResponse()->hide(['password']);

}

public function index()
{
    $this->response($data, ResponseCode::HTTP_OK);
}

可用数据操作

->hide(['password'])
->decode(['extra'])
->decodeChild(['extra.user'])
->decodeArray(['extra_list'])
->addFields(['field1' => '1', 'field2' => '2'])
->addField('field1', '1')
->hook('item', function($data){ return $data})

COFFEE FOR BEST PERFORMANCE

COFFEE HERE 获取更多创新

或者

Trakteer Saya