pchouse / php-attributes
一个库,用于在类和属性中使用PHP属性来定义元数据,例如数据库、HTML等。
1.0.0-BETA-7
2024-07-16 13:34 UTC
Requires
- php: ~8.2 || ~8.3
Requires (Dev)
- jetbrains/phpstorm-attributes: >=1
- joaomfrebelo/date: ^2.2.2
- joaomfrebelo/decimal: ^4.0.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- sebastianbergmann/php-text-template: ^1.1
- squizlabs/php_codesniffer: ^3.8
This package is auto-updated.
Last update: 2024-09-16 14:00:15 UTC
README
A PHP
安装
composer require pchouse/php-attributes
此库是一组PHP属性,用于定义类和属性的元数据,以帮助实现HTML模板和数据库模型。
用法
对象
<?php /** @noinspection ALL */ declare(strict_types=1); namespace PChouse\Resources; use PChouse\Attributes\Db\Column; use PChouse\Attributes\Db\Table; use PChouse\Attributes\Db\Types; use PChouse\Attributes\HTML\Element; use PChouse\Attributes\HTML\Form; use PChouse\Attributes\HTML\InputType; use PChouse\Attributes\HTML\Option; use PChouse\Attributes\HTML\Tag; use Rebelo\Date\Date; use Rebelo\Decimal\Decimal; #[Table] #[Form(novalidate: true)] #[Element(tag: Tag::INPUT, type: InputType::HIDDEN, name: "hidden1")] #[Element(tag: Tag::INPUT, type: InputType::HIDDEN, name: "hidden2")] #[Element(tag: Tag::SELECT, name: "select1")] #[Option(value: "", text: "Select", selected: false)] #[Option(value: "1", text: "Option1", selected: true)] class MyClass { #[Element(tag: Tag::INPUT, type: InputType::TEXT, position: 9)] #[Column] private string $columnString; #[Element(tag: Tag::INPUT, type: InputType::NUMBER, position: 7)] #[Column] private int $columnInt; #[Element(tag: Tag::INPUT, type: InputType::RADIO, id: "my_class_column_float_1")] #[Element(tag: Tag::INPUT, type: InputType::RADIO, id: "my_class_column_float_2")] #[Element(tag: Tag::INPUT, type: InputType::RADIO, id: "my_class_column_float_3")] #[Column] private float $columnFloat; #[Element(tag: Tag::INPUT, type: InputType::CHECKBOX,)] #[Column] private bool $columnBool; #[Element(tag: Tag::INPUT, type: InputType::CHECKBOX,)] #[Column] private Date $columnDate; #[Element(tag: Tag::SELECT, name: "select2")] #[Option(value: "", text: "Select")] #[Option(value: "9", text: "Option with 9")] #[Option(value: "99", text: "Option with 999")] #[Column] private Decimal $columnDecimal; #[Element(tag: Tag::INPUT, type: InputType::TEXT, position: 1)] #[Column] private ?string $columnStringNull; }
类型映射器
<?php declare(strict_types=1); namespace PChouse\Resources; use PChouse\Attributes\Db\TypeMap; use PChouse\Attributes\Db\Types; use Rebelo\Date\Date; use Rebelo\Decimal\Decimal; class MyTypeMap extends TypeMap { public function dbTypeFor(string $propertyType): ?Types { return match ($propertyType) { Decimal::class => Types::DB_DECIMAL, Date::class => Types::DB_DATE, default => parent::dbTypeFor($propertyType) }; } }
获取数据库元数据
$reflectionClass = new \ReflectionClass(MyClass::class); $myTypeMap = new MyTypeMap(); $table = Table::parse($reflectionClass); // Table $columns = Column::parse($reflectionClass, $myTypeMap); // array<string, Column>
获取表单元数据
$form = Form::parse(new \ReflectionClass(MyClass::class)); if($form instanceof \PChouse\Attributes\HTML\Form)){ // Set other properties } $array = $form->toStackArray(); // The returned array can be used with twig macros
许可证 MIT
版权所有 2023 Reflexão, Sistemas e Estudos Informáticos, Lda
在此特此授予任何获得此软件及其相关文档文件(“软件”)副本的人,免费使用该软件的权利,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许获得软件的人这样做,但受以下条件约束:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定目的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论是在合同、侵权或其他行为中,源于、因之或与此软件或其使用或其他交易有关。