PHP code example of lit / parameter

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

    

lit / parameter example snippets




use Lit\Parameter\V2\Parameter;
use Lit\Parameter\V2\Types\Types;

/**
 * @property Types $id
 * @property Types $name
 * @property Types $email
 * @property Types $tags
 */
class UserMapper extends Parameter
{
    protected $defaultError = true;
    protected $defaultErrorCode = 77777;
    protected $defaultErrorMsg = '参数错误(%s)';

    public function __construct($params = []) {
        $this->id->isInteger()->gt(0)->setCode(1001)->setMsg('ID 错误');
        $this->name->isString()->notEmpty()->maxLength(32)->setCode(1002)->setMsg('名称错误');
        $this->email->isString()->emailFormat()->setCode(1003)->setMsg('邮箱错误');
        $this->tags->isArray()->minSize(1)->setDefault([]);

        parent::__construct($params);
    }
}

$mapper = new UserMapper([
    'id' => 1,
    'name' => 'lit',
    'email' => '[email protected]',
    'tags' => ['php'],
]);

if (!$mapper->must(['id', 'name'])->check()) {
    var_export($mapper->errAll());
    return;
}

var_dump($mapper->id->value());
var_dump($mapper->toArray());

use Lit\Parameter\V2\Parameter;
use Lit\Parameter\V2\Types\Types;

/**
 * @property Types $age
 * @property Types $height
 * @property Types $gender
 * @property Types $children
 * @property Types $data
 * @property Types $create_time
 */
class ExampleMapper extends Parameter
{
    protected $defaultError = true;
    protected $defaultErrorCode = 390;
    protected $defaultErrorMsg = '参数错误(%s)';

    public function __construct($params = []) {
        $this->age->isInteger()->between(12, 18)->setCode(10001)->setMsg('年龄错误');
        $this->height->isInteger()->ge(120)->le(180)->setCode(10002)->setMsg('身高错误');
        $this->gender->isString()->in(['m', 'f'])->setCode(10003)->setMsg('性别错误');
        $this->children->isArray()->minSize(2)->maxSize(3)->setCode(10004)->setMsg('子集错误');
        $this->data->isArray()->excFields(['aa', 'bb'])->incFields(['cc', 'dd'])->setCode(10005)->setMsg('数据集错误');
        $this->create_time->isDateTime()->formatIs('Y-m-d H')->setCode(10006)->setMsg('创建时间错误');

        parent::__construct($params);
    }
}

$mapper = new ExampleMapper();

$mapper->age = 12;
$mapper->height = 170;
$mapper->gender = 'f';
$mapper->children = ['a', 'b'];
$mapper->data = ['cc' => 11, 'dd' => 22];
$mapper->create_time = '2026-06-16 10';

// 读取单个字段值
var_dump($mapper->age->value());

// 读取字段类型
var_dump($mapper->age->type());

// 读取已赋值字段
var_dump($mapper->getAssigned());

// 读取所有已声明字段
var_dump($mapper->toArray());

// 赋值后不验证数据, 强制转换类型
var_dump($mapper->format());

// 判断是否未赋值
var_dump($mapper->isEmpty());

$mapper = new ExampleMapper([
    'age' => 12,
    'height' => 170,
    'gender' => 'f',
    'children' => ['a', 'b'],
    'data' => ['cc' => 11, 'dd' => 22],
    'create_time' => '2026-06-16 10',
]);

if ($mapper->must(['age', 'gender'])->check()) {
    var_dump('验证成功');
} else {
    var_export($mapper->errAll());
}

$mapper = new ExampleMapper();

$params = [
    'age' => 12,
    'height' => 170,
    'gender' => 'm',
    'children' => ['a', 'b'],
    'data' => ['cc' => 11, 'dd' => 22],
    'create_time' => '2026-06-16 10',
];

if (!$mapper->must(['age'])->check($params)) {
    var_export($mapper->errAll());
}

function createUser($id, $name, $email) {
    $mapper = new UserMapper();

    if (!$mapper->must(['id', 'name'])->check(get_defined_vars())) {
        var_export($mapper->errAll());
        return false;
    }

    return true;
}

$mapper->must(['id', 'name'])->check($params);

var_export($mapper->errAll());

[
    'errCode' => 1001,
    'errMsg' => 'ID 错误',
    'errName' => 'id',
    'errValue' => 0,
    'errCondition' => 'gt',
]

$mapper->errCode();
$mapper->errMsg();
$mapper->errName();
$mapper->errValue();
$mapper->errCondition();

Parameter::debugOn();

$this->create_time->isDateTime()->formatIs('Y-m-d H');
bash
php bin/MapperWithDDL.php