nilportugues / filesystem-repository
文件系统仓库实现
Requires
- php: >=7.0
- nilportugues/assert: ^1.0
- nilportugues/repository: ^3.0
Requires (Dev)
- fabpot/php-cs-fixer: 1.9.*
- phpunit/phpunit: 4.8.*
This package is not auto-updated.
Last update: 2024-09-14 18:36:48 UTC
README
FileSystem Repository使用nilportugues/repository作为基础。
FileSystem Repository允许您轻松获取、分页和操作数据,而不需要增加开销和遵循良好实践。
目录
特性
- 从一开始就是仓库模式。
- 所有操作从一开始就可用
- 使用PHP对象搜索仓库
- 使用过滤对象进行过滤。
- 使用字段对象获取特定字段。
- 使用页面和分页对象解决分页问题。
- 想要更改持久层?提供的仓库替代方案是
- InMemoryRepository:用于测试目的
- SQL Repository:可以迁移到SQL数据库。
- MongoDBRepository:因为您的模式不断变化
- 需要缓存层?易于添加!
- 需要从Composer安装Repository Cache包以添加所有操作的持久性缓存。
安装
使用Composer安装包
$ composer require nilportugues/filesystem-repository
使用
<?php use NilPortugues\Foundation\Infrastructure\Model\Repository\FileSystem\Drivers\NativeFileSystem; use NilPortugues\Foundation\Infrastructure\Model\Repository\FileSystem\FileSystemRepository; //------------------------------------------------------------------- // Setting up the repository directory and how it will be access: //------------------------------------------------------------------- $baseDir = __DIR__.'/data/colors'; $fileSystem = new NativeFileSystem($baseDir); $fileRepository = new FileSystemRepository($fileSystem); //------------------------------------------------------------------- // Create sample data //------------------------------------------------------------------- $red = new Color('Red', 1); $blue = new Color('Blue', 2) $fileRepository->addAll([$red, $blue]); //------------------------------------------------------------------- // Now let's try filtering by id //------------------------------------------------------------------- $filter = new Filter(); $filter->must()->equal('id', 1); //id is a Color property. print_r($fileRepository->findBy($filter)); //------------------------------------------------------------------- // Now let's try filtering by contaning 'e' in the name and sort them. //------------------------------------------------------------------- $filter = new Filter(); $filter->must()->contain('name', 'e'); //name is a Color property. $sort = new Sort(); $sort->setOrderFor('name', new Order('DESC')); print_r($fileRepository->findBy($filter, $sort)); // This will return both values. //------------------------------------------------------------------- //Lets remove all colors from the repository //------------------------------------------------------------------- $fileRepository->removeAll();
仓库
仓库类实现了所有与数据交互和过滤所需的方法。
public function add($value)public function addAll(array $values)public function remove(Identity $id)public function removeAll(Filter $filter = null)public function transactional(callable $transaction)public function find(Identity $id, Fields $fields = null)public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null, Fields $fields = null)public function findAll(Pageable $pageable = null)public function count(Filter $filter = null)public function exists(Identity $id)
数据操作
所有数据都可以通过字段名称、使用过滤器、应用排序和分页来提取,能够应用字段、过滤器和排序标准。
字段
通过字段选择将使填充失败。目前不支持部分对象填充。
类: NilPortugues\Foundation\Domain\Model\Repository\Fields
方法
public function __construct(array $fields = [])public function add($field)public function get()
过滤
类: NilPortugues\Foundation\Domain\Model\Repository\Filter
方法
public function filters()public function must()public function mustNot()public function should()public function clear()
对于 must()、mustNot() 和 should(),可用的方法有
public function notStartsWith($filterName, $value)public function notEndsWith($filterName, $value)public function notEmpty($filterName)public function empty($filterName)public function startsWith($filterName, $value)public function endsWith($filterName, $value)public function equal($filterName, $value)public function notEqual($filterName, $value)public function includeGroup($filterName, array $value)public function notIncludeGroup($filterName, array $value)public function range($filterName, $firstValue, $secondValue)public function notRange($filterName, $firstValue, $secondValue)public function notContain($filterName, $value)公共函数 contain($filterName, $value)公共函数 beGreaterThanOrEqual($filterName, $value)公共函数 beGreaterThan($filterName, $value)公共函数 beLessThanOrEqual($filterName, $value)公共函数 beLessThan($filterName, $value)public function clear()public function get()公共函数 hasEmpty($filterName)
分页
分页处理由两个对象完成,一个是Pageable,它负责分页,另一个是Page,它实际上包含了页数据,如页码、总页数和数据。
分页的
类: NilPortugues\Foundation\Domain\Model\Repository\Pageable
方法
公共函数 __construct($pageNumber, $pageSize, Sort $sort = null, Filter $filter = null, Fields $fields = null)公共函数 offset()公共函数 pageNumber()公共函数 sortings()公共函数 next()公共函数 pageSize()公共函数 previousOrFirst()公共函数 hasPrevious()公共函数 first()public function filters()公共函数 fields()
分页对象
类: NilPortugues\Foundation\Domain\Model\Repository\Page
方法
公共函数 __construct(array $elements, $totalElements, $pageNumber, $totalPages, Sort $sort = null, Filter $filter = null, Fields $fields = null)公共函数 content()公共函数 hasPrevious()公共函数 isFirst()公共函数 isLast()公共函数 hasNext()公共函数 pageSize()公共函数 pageNumber()公共函数 totalPages()公共函数 nextPageable()公共函数 sortings()public function filters()公共函数 fields()公共函数 previousPageable()公共函数 totalElements()公共函数 map(callable $converter)
排序
类: NilPortugues\Foundation\Domain\Model\Repository\Sort
方法
公共函数 __construct(array $properties = [], Order $order = null)公共函数 andSort(SortInterface $sort)公共函数 orders()公共函数 equals(SortInterface $sort)公共函数 orderFor($propertyName)公共函数 setOrderFor($propertyName, Order $order)公共函数 property($propertyName)
排序
有时您需要按多个字段排序,这就是Order发挥作用的地方。
类: NilPortugues\Foundation\Domain\Model\Repository\Order
方法
公共函数 __construct($direction)公共函数 isDescending()公共函数 isAscending()公共函数 __toString()公共函数 equals($object)公共函数 direction()
质量
要在命令行运行PHPUnit测试,请进入测试目录并执行phpunit。
如果您发现遵守上的疏忽,请通过Pull Request发送补丁。
贡献
欢迎为该包做出贡献!
支持
使用以下方式之一与我联系
- 通过contact@nilportugues.com给我发邮件
- 打开一个Issue
作者
许可证
代码库采用MIT许可证。