scheb / in-memory-data-storage
简单的内存数据存储
Requires
- php: ^7.1.3
- scheb/comparator: ^1.0.0
- scheb/property-access: ^1.0.1
Requires (Dev)
- phpunit/phpunit: ^7.2
This package is auto-updated.
Last update: 2022-01-11 13:25:22 UTC
README
这是一个快速的内存在PHP中的数据存储。它可以作为一个数据库的测试双胞胎,以便将测试用例与数据库解耦并加快它们的执行速度。
功能
- CRUD操作
- 数据选择和操作方面的便捷方法
- 命名项
安装
composer require scheb/in-memory-data-storage
如何使用
您可以在 doc
文件夹中找到一个可执行的示例。
<?php use Scheb\InMemoryDataStorage\DataRepositoryBuilder; use Scheb\InMemoryDataStorage\DataStorage\ArrayDataStorage; use function \Scheb\InMemoryDataStorage\Repository\compare; $foo = 'I am foo'; $bar = 'I am bar'; $repositoryBuilder = new DataRepositoryBuilder(); $repository = $repositoryBuilder // ->setDataStorage(new ArrayDataStorage()) ->build(); // Simple CRUD $repository->addItem($foo); $repository->containsItem($foo); // returns true $repository->getAllItems(); // returns [$foo] $repository->removeItem($foo); // Named items $repository->setNamedItem('foo', $foo); $repository->namedItemExists('foo'); // returns true $repository->getNamedItem('foo'); // returns $foo $repository->replaceNamedItem('foo', $bar); $repository->getNamedItem('foo'); // returns $bar $repository->removeNamedItem('foo'); // Advanced get $repository->getAllItemsByCriteria(['property' => 'value']); // $repository->getOneItemByCriteria(...); // The same, but only one item is retrieved // Advanced update $repository->updateAllItemsByCriteria( ['property' => 'value'], // Match criteria ['property' => 'newValue', 'otherProperty' => 42] // Property updates ); // $repository->updateOneByCriteria(...); // The same, but only one item is updated // Advanced remove $repository->removeAllItemsByCriteria(['property' => 'value']); // $repository->removeOneItemByCriteria(...); // The same, but only one item is removed // Comparision functions in matching criteria $repository->getAllItemsByCriteria(['property' => compare()->notNull()]); $repository->getAllItemsByCriteria(['property' => compare()->lessThan(3)]); $repository->getAllItemsByCriteria(['property' => compare()->between(1, 3)]); $repository->getAllItemsByCriteria(['property' => compare()->isInArray([1, 2, 3])]); $repository->getAllItemsByCriteria(['property' => compare()->arrayContains('arrayElement')]); $repository->getAllItemsByCriteria(['property' => compare()->dateGreaterThan(new \DateTime('2018-01-01'))]); // ... and many more, see Scheb\InMemoryDataStorage\Comparison
自定义
ItemNotFoundException
当数据存储中没有匹配项时,单条项(读取、更新、删除)操作将优雅地处理。读取将返回 null
,更新/删除不会更改任何内容。如果您想在这些情况下引发异常,可以配置每个操作类型的构建器以实现这种行为。
$repository = $repositoryBuilder ->strictGet() ->strictUpdate() ->strictRemove() ->build();
数据存储
库附带一个简单的基于数组的存储,但您可以通过实现 Scheb\InMemoryDataStorage\DataStorage\DataStorageInterface
来替换您喜欢的任何数据存储引擎。然后,将实例传递给构建器
$repository = $repositoryBuilder ->setDataStorage(new MyCustomDataStorage()) ->build();
值匹配
使用PHP的 ===
操作符检查值是否相等。如果您想使用较宽松的 ==
操作符,可以通过构建器更改此行为。
$repository = $repositoryBuilder ->useStrictTypeComparison(false) ->build();
库集成了 scheb/comparator
以检查值是否相等。如果您需要自定义比较规则,实现 Scheb\Comparator\ValueComparisonStrategyInterface
并将您的比较策略传递给构建器。
$repository = $repositoryBuilder ->addComparisonStrategy(new MyCustomValueComparisonStrategy()) ->build();
您可以根据需要添加任意多的比较策略。它们将按照添加的顺序进行检查,并将优先于默认比较策略。
如果您想完全自行实现比较逻辑,实现 Scheb\InMemoryDataStorage\Matching\ValueMatcherInterface
并将实例传递给构建器
$repository = $repositoryBuilder ->setValueMatcher(new MyCustomValueMatcher()) ->build();
属性访问
使用以下访问策略读取和写入属性
- 数组访问
- 公共属性
- 驼峰式获取器和设置器
如果这些在值对象上不起作用,则可能是使用 null
(在读取的情况下)或在写入时失败。
如果您想完全自行实现属性访问逻辑,实现 Scheb\PropertyAccess\PropertyAccessInterface
并将实例传递给构建器
$repository = $repositoryBuilder ->setPropertyAccess(new MyCustomPropertyAccess()) ->build();
贡献
欢迎您通过在问题区域创建拉取请求或功能请求来为这个库做出贡献。[贡献链接](https://github.com/scheb/in-memory-data-storage/graphs/contributors "贡献链接")。对于拉取请求,请遵循以下指南
- Symfony 代码风格
- PHP7.1 类型提示适用于所有内容(包括:返回类型、
void
、可空类型) - 请添加/更新测试用例
- 测试方法应命名为
[方法]_[场景]_[预期结果]
要运行测试套件,请使用composer install
安装依赖项,然后执行bin/phpunit
。
许可证
此组件受MIT 许可证的约束。