symandy / resource
一组可重用的资源接口和特性
v1.3.1
2022-11-24 14:59 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.5
README
这个包包含了一组可重用的组件,包含可用于任何PHP项目的接口和特性。它主要被设计用于Symfony实体。
这个包的设计受到了Sylius Resource Bundle的强烈启发。
安装
$ composer require symandy/resource
组件
组件存储在 Symandy\Component\Resource\Model
命名空间。
每个接口都有一个对应的特性和一个或多个属性。
使用方法
资源创建
使用这些组件的最佳方式是为每个资源创建一个类和一个接口。
也可以只创建类并添加相应的特性。
示例
<?php namespace App; use Symandy\Component\Resource\Model\ResourceInterface; use Symandy\Component\Resource\Model\TimestampableInterface; use Symandy\Component\Resource\Model\ToggleableInterface; interface PostInterface extends ResourceInterface, ToggleableInterface, TimestampableInterface { # Other methods }
<?php namespace App; use Symandy\Component\Resource\Model\ResourceTrait; use Symandy\Component\Resource\Model\TimestampableTrait; use Symandy\Component\Resource\Model\ToggleableTrait; class Post implements PostInterface { use ResourceTrait, ToggleableTrait, TimestampableTrait; # Other attributes with getters / setters }
在您的应用中使用资源
<?php use App\Post; # ... $post = new Post(); $id = $post->getId(); $post->enable(); $post->create(); # ...