1. Go to this page and download the library: Download rice/basic 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/ */
namespace App\Entity;
use Rice\Basic\Components\Entity\BaseEntity;
class UserEntity extends BaseEntity
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $name;
// 业务方法
public function changeName(string $newName): void
$this->name = $newName;
// 触发相关业务逻辑
}
}
namespace App\Assembler;
use App\DTO\UserDTO;
use App\Entity\UserEntity;
use Rice\Basic\Components\Assembler\BaseAssembler;
use Illuminate\Http\Request;
class UserAssembler implements BaseAssembler
{
public function toDTO(Request $request): UserDTO
{
return (new UserDTO())
->setName($request->input('name'))
->setEmail($request->input('email'));
}
public function toEntity(UserDTO $dto): UserEntity
{
return (new UserEntity())
->setName($dto->getName())
->setEmail($dto->getEmail());
}
}
namespace App\Enum;
use Rice\Basic\Components\Enum\BaseEnum;
class UserStatusEnum extends BaseEnum
{
/**
* @var int 活跃状态
*/
public const ACTIVE = 1;
/**
* @var int 禁用状态
*/
public const DISABLED = 0;
/**
* 获取状态描述
*/
public static function getDescription($value): string
{
$descriptions = [
self::ACTIVE => '活跃',
self::DISABLED => '禁用'
];
return $descriptions[$value] ?? '未知';
}
}
use Rice\Basic\Support\Traits\AutoFillProperties;
class User {
use AutoFillProperties;
private $name;
private $email;
}
// 自动填充属性
$user = new User();
$user->fill([
'name' => 'John Doe',
'email' => '[email protected]'
]);
use Rice\Basic\Support\Traits\Singleton;
class Config {
use Singleton;
private $settings = [];
public function set($key, $value): void
{
$this->settings[$key] = $value;
}
public function get($key, $default = null)
{
return $this->settings[$key] ?? $default;
}
}
// 获取单例实例
$config = Config::getInstance();
$config->set('app_name', 'My Application');
use Rice\Basic\Components\Exception\InvalidRequestException;
// 抛出异常
if (empty($data)) {
throw new InvalidRequestException('数据不能为空');
}
// 异常观察者会自动记录异常信息
class MyClass {
use MagicMethodManager;
// 可选:注册自定义处理器
public function initializeMagicMethodManager()
{
// 注意:这个方法仅为了保持向后兼容性,实际处理逻辑应在registerDefaultHandlers中实现
}
}
$cat->speak;
$cat->getSpeak();
$cat->setSpeak($val);
class Cat
{
use AutoFillProperties;
/**
* @var string
*/
public $eyes;
/**
* @var Eat
*/
public $eat;
/**
* @var Speak
*/
public $speak;
/**
* @var string[]
*/
public $hair;
}
class Cat
{
use AutoFillProperties;
#[Doc(var: 'Eye[]', text: '眼睛')]
public $eyes;
#[Doc(var: 'Eat')]
public $eat;
#[Doc(var: 'Speak')]
public $speak;
#[Doc(var: 'string[]')]
public $hair;
}
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Rice\Basic\Support\Traits\Accessor;
use Rice\Basic\Support\Traits\AutoFillProperties;
class BaseRequest extends FormRequest
{
use AutoFillProperties, Accessor;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
namespace App\Http\Controllers;
use App\Logic\TestLogic;
use Illuminate\Http\Request;
use App\Assembler\TestAssembler;
use App\Http\Requests\TestRequest;
use Illuminate\Support\Facades\Response;
class TestController extends BaseController
{
public function test(Request $request): \Illuminate\Http\JsonResponse
{
$testRequest = new TestRequest($request->all());
$testRequest->check();
$testLogic = (new TestLogic());
$dto = TestAssembler::toDTO($request);
$resp = $testLogic->doSomethink($dto);
return Response::json($resp);
}
}
abstract class LaravelClient extends GuzzleClient
{
public static function build()
{
return new static(LaravelLog::build());
}
}
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Rice\Basic\Support\Traits\Scene;
class SceneRequest extends FormRequest
{
use Scene;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'state' => '
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.