tobento / service-repository
PHP应用程序的存储库接口。
1.0.1
2023-04-11 14:46 UTC
Requires
- php: >=8.0
- tobento/service-collection: ^1.0.5
- tobento/service-iterable: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- tobento/service-event: ^1.0
- vimeo/psalm: ^4.0
README
PHP应用程序的存储库接口。
目录
入门
运行此命令添加正在运行的存储库服务项目的最新版本。
composer require tobento/service-repository
要求
- PHP 8.0 或更高版本
亮点
- 框架无关,适用于任何项目
- 解耦设计
文档
接口
存储库接口
namespace Tobento\Service\Repository; interface RepositoryInterface extends ReadRepositoryInterface, WriteRepositoryInterface { // }
读取存储库接口
namespace Tobento\Service\Repository; interface ReadRepositoryInterface { /** * Returns the found entity using the specified id (primary key) * or null if none found. * * @param int|string $id * @return null|object * @throws RepositoryReadException */ public function findById(int|string $id): null|object; /** * Returns the found entity using the specified id (primary key) * or null if none found. * * @param int|string ...$ids * @return iterable<object> * @throws RepositoryReadException */ public function findByIds(int|string ...$ids): iterable; /** * Returns the found entity using the specified where parameters * or null if none found. * * @param array $where * @return null|object * @throws RepositoryReadException */ public function findOne(array $where = []): null|object; /** * Returns the found entities using the specified parameters. * * @param array $where Usually where parameters. * @param array $orderBy The order by parameters. * @param null|int|array $limit The limit e.g. 5 or [5(number), 10(offset)]. * @return iterable<object> * @throws RepositoryReadException */ public function findAll(array $where = [], array $orderBy = [], null|int|array $limit = null): iterable; /** * Returns the number of items using the specified where parameters. * * @param array $where * @return int * @throws RepositoryReadException */ public function count(array $where = []): int; }
写入存储库接口
namespace Tobento\Service\Repository; interface WriteRepositoryInterface { /** * Create an entity. * * @param array $attributes * @return object The created entity. * @throws RepositoryCreateException */ public function create(array $attributes): object; /** * Update an entity by id. * * @param string|int $id * @param array $attributes The attributes to update the entity. * @return object The updated entity. * @throws RepositoryUpdateException */ public function updateById(string|int $id, array $attributes): object; /** * Update entities. * * @param array $where The where parameters. * @param array $attributes The attributes to update the entities. * @return iterable<object> The updated entities. * @throws RepositoryUpdateException */ public function update(array $where, array $attributes): iterable; /** * Delete an entity by id. * * @param string|int $id * @return object The deleted entity. * @throws RepositoryDeleteException */ public function deleteById(string|int $id): object; /** * Delete entities. * * @param array $where The where parameters. * @return iterable<object> The deleted entities. * @throws RepositoryDeleteException */ public function delete(array $where): iterable; }
实体工厂接口
namespace Tobento\Service\Repository; interface EntityFactoryInterface { /** * Create an entity from array. * * @param array $attributes * @return object The created entity. */ public function createEntityFromArray(array $attributes): object; }
只读存储库适配器
实现 RepositoryInterface::class
的任何存储库都可以通过使用 ReadOnlyRepositoryAdapter::class
进行装饰来使其变为只读。
use Tobento\Service\Repository\ReadOnlyRepositoryAdapter; use Tobento\Service\Repository\RepositoryInterface; $readOnlyRepository = new ReadOnlyRepositoryAdapter( repository: $repository, // RepositoryInterface );
事件存储库适配器
实现 ReadRepositoryInterface::class
或 WriteRepositoryInterface::class
的任何存储库都可以通过使用 EventsRepositoryAdapter::class
进行装饰来触发默认事件。
use Psr\EventDispatcher\EventDispatcherInterface; use Tobento\Service\Repository\EventsRepositoryAdapter; use Tobento\Service\Repository\ReadRepositoryInterface; use Tobento\Service\Repository\WriteRepositoryInterface; use Tobento\Service\Repository\Event; $eventsRepository = new EventsRepositoryAdapter( eventDispatcher: $eventDispatcher, // EventDispatcherInterface repository: $repository, // ReadRepositoryInterface or WriteRepositoryInterface // if false (default) event attributes get used on write methods immutableAttributes: false, );
默认事件
事件器
如果只想触发特定事件,可以使用事件器轻松创建 EventsRepositoryAdapter::class
。
创建事件器
use Psr\EventDispatcher\EventDispatcherInterface; use Tobento\Service\Repository\EventerInterface; use Tobento\Service\Repository\Eventer; $eventer = new Eventer( eventDispatcher: $eventDispatcher, // EventDispatcherInterface ); var_dump($eventer instanceof EventerInterface); // bool(true)
使用事件器
use Tobento\Service\Repository\EventerInterface; class SomeService { public function createAction(EventerInterface $eventer) { $entity = $eventer ->repository($this->someRepository) ->create(['title' => 'Lorem']); // or $entity = $eventer ->repository( repository: $this->someRepository, immutableAttributes: true, // default is false ) ->create(['title' => 'Lorem']); } }