phpolar/model

提供对对象属性进行配置以进行验证、格式化和存储的支持。

1.2.0 2023-09-02 17:39 UTC

This package is auto-updated.

Last update: 2024-08-28 01:40:35 UTC


README

PHPolar 模型

提供对对象属性进行配置以进行验证、格式化和存储的支持。

Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

示例 1

<!DOCTYPE html>
<?php

namespace MyApp;

use Phpolar\Phpolar\FormControlTypes;

/**
 * @var PersonForm
 */
$view = $this;
?>
<html>
    // ...
    <body style="text-align:center">
        <h1><?= $view->title ?></h1>
        <div class="container">
            <form action="<?= $view->action ?>" method="post">
                <?php foreach ($view as $propName => $propVal): ?>
                    <label><?= $view->getLabel($propName) ?></label>
                    <?php switch ($view->getFormControlType($propName)): ?>
                        <?php case FormControlTypes::Input: ?>
                            <input type="text" value="<?= $propVal ?>" />
                        <?php case FormControlTypes::Select: ?>
                            <select>
                                <?php foreach ($propVal as $name => $item): ?>
                                    <option value="<?= $item ?>"><?= $name ?></option>
                                <?php endforeach ?>
                            </select>
                    <?php endswitch ?>
                <?php endforeach ?>
            </form>
        </div>
    </body>
</html>

使用属性来配置模型

use Phpolar\Phpolar\AbstractModel;

class Person extends AbstractModel
{
    public string $title = "Person Form";

    public string $action = "somewhere";

    #[MaxLength(20)]
    public string $firstName;

    #[MaxLength(20)]
    public string $lastName;

    #[Column("Residential Address")]
    #[Label("Residential Address")]
    #[MaxLength(200)]
    public string $address1;

    #[Column("Business Address")]
    #[Label("Business Address")]
    #[MaxLength(200)]
    public string $address2;
}

阈值