arnapou / pfdb
库 - 带查询功能的平面文件存储。
v6.3
2024-04-14 15:48 UTC
Requires
- php: ~8.3.0
- arnapou/ensure: ^2.3
- arnapou/lock: ^1.2
Requires (Dev)
- ext-redis: *
- ext-yaml: *
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
- symfony/yaml: ^5.4
Suggests
- ext-yaml: If you need YAML file storage. Alternative: symfony/yaml.
- symfony/yaml: If you need YAML file storage. Alternative: ext-yaml.
README
此库允许您查询平面文件 "数据库"。
安装
composer require arnapou/pfdb
packagist 👉️ arnapou/pfdb
简介
这是什么
- 纯面向对象
- 轻量级
- 可扩展(接口,...)
这不是什么
- SQL数据库
- 关系型数据库
- ORM
- DBDAL
何时使用
- 您绝对需要平面文件
- 读取量大于写入量(即使您可以使用锁定策略)
- 轻量级数据
- 简单的文件,如配置或小型数据(少于几千项)
实现文件格式
- YAML
- PHP
注意,您很容易自己实现
由于这是我为自己做的项目,我认为一些示例和阅读代码应该足够开发者使用。示例是您将找到的最好的文档。
条件
$storage = new \Arnapou\PFDB\Storage\PhpFileStorage($somePath);
$database = new \Arnapou\PFDB\Database($storage);
$table = $database->getTable('vehicle');
$expr = $table->expr()->and(
$table->expr()->gt('price', 10000),
$table->expr()->match('model', '^C[0-9]+')
);
$iterator = $table->find($expr)
->sort('constructor' , ['model' , 'DESC'])
->limit(0, 50);
foreach($iterator as $key => $row) {
// do whatever you want
}
扩展表达式
类
class IsUppercaseExpr implements \Arnapou\PFDB\Query\Helper\Expr\ExprInterface {
private $field;
public function __construct(string $field)
{
$this->field = $field;
}
public function __invoke(array $row, $key = null): bool
{
if(!isset($row[$this->field]) {
return false;
}
$testedValue = (string)$row[$this->field];
return $testedValue === strtoupper($testedValue);
}
}
使用
$storage = new \Arnapou\PFDB\Storage\PhpFileStorage($somePath);
$database = new \Arnapou\PFDB\Database($storage);
$table = $database->getTable('vehicle');
$expr = new IsUppercaseExpr('model');
foreach($table->find($expr) as $key => $row) {
// do whatever you want
}
在存储上下文之外使用 PFDB 迭代器
如果您只想选择、过滤、排序、限制、分组、排序任何迭代器
$data = [
['name' => 'John', 'age' => 20],
['name' => 'Edith', 'age' => 25],
['name' => 'Steve', 'age' => 30],
['name' => 'Matthew', 'age' => 22],
);
$query = (new \Arnapou\PFDB\Query\Query())
->from(new \ArrayIterator($data))
->where($query->expr()->gt('age', 24));
foreach($query as $key => $row) {
// do whatever you want
}
构建自己的存储
您想使用CSV文件而不是PHP转储数组吗?
简单:扩展或实现自己的存储,并使用它来加载/存储/删除数据。
查看现有存储并编写自己的。
PHP版本
日期 | 参考 | 8.3 | 8.2 | 8.1 | 8.0 | 7.2 | 5.4 |
---|---|---|---|---|---|---|---|
26/11/2023 | 6.x,主版本 | × | |||||
11/12/2022 | 5.x | × | |||||
30/01/2022 | 4.x | × | |||||
15/05/2021 | 3.x | × | |||||
27/02/2019 | 2.x | × | |||||
07/11/2013 | 1.x | × |