adinan-cenci / json-lines
一个用于读取和写入json-lines的库。
v3.0.0
2023-07-22 10:31 UTC
Requires
- php: >=7.0
- adinan-cenci/file-editor: ^1.1
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-22 13:10:23 UTC
README
一个用于读取和写入json lines格式文件的库。注意:仍在早期开发阶段。
如何使用它
实例化
use AdinanCenci\JsonLines\JsonLines; $associative = true; $file = new JsonLines('my-file.jsonl', $associative);
$associative
:如果设置为false
,则将条目渲染为对象;如果设置为true
,则将条目渲染为关联数组,默认为false
。
迭代
foreach ($file->objects as $line => $object) { echo $object->myProperty . '<br>'; // or $object['myProperty'] if ::$associative is true. }
向文件末尾添加对象
$object = ['foo' => 'bar']; $file->addObject($object);
$object
不需要是数组,它也可以是一个实际的对象。
向文件中间添加对象
$line = 5; $object = ['foo' => 'bar']; $file->addObject($object, $line);
如果文件行数少于$line
行,则空行将填充空白。
向文件末尾添加多个对象
$objects = [ // line => object 0 => ['name' => 'foo'], 5 => ['name' => 'bar'], ]; $objects->addObjects($objects);
向文件中间添加多个对象
$objects = [ // line => object / array 2 => ['name' => 'foo'], 6 => ['name' => 'bar'], ]; $objects->addObjects($objects, false);
设置对象
$line = 10; $object = ['foo' => 'bar']; $file->setObject($line, $object);
::addObject()
和::setObject()
之间的区别在于,::setObject()
将在$line
处覆盖现有内容。
设置多个对象
$objects = [ // line => object / array 0 => ['name' => 'foo'], 5 => ['name' => 'bar'], ]; $objects->setObjects($objects);
检索对象
$line = 10; $object = $file->getObject($line);
如果条目不存在或json无效,则返回null
。
检索多个对象
$lines = [0, 1, 2]; $objects = $file->getObjects($lines);
删除对象
$line = 10; $file->deleteObject($line);
删除多个对象
$lines = [0, 1, 2]; $file->deleteObjects($lines);
搜索
该库还提供了一种查询文件的方式。
创建一个新的Search
对象,给出条件并调用::find()
方法,它将返回一个数组,其中包含按文件中行索引匹配的对象。
$search = $file->search(); $search->condition("object's property", 'value to compare', 'operator'); $results = $search->find();
空值运算符
$search->condition('title', null, 'IS NULL'); // Will match entries where the "title" property equals null or is // not defined.
等于运算符
$search->condition('title', 'Iliad', '='); // Will match entries where the "title" property equals "Iliad" // ( case insensitive ).
在运算符
$search->condition('title', ['Iliad', ' Odyssey'], 'IN'); // Will match entries where the "title" property equals to either // "Iliad" or "Odyssey" ( case insensitive ).
类似运算符
$search->condition('title', 'foo', 'LIKE'); // Will match entries where the "title" property contains the word "foo" // e.g: "foo", "foo bar", "foofighters" etc ( case insensitive ). $search->condition('title', ['foo', 'bar'], 'LIKE'); // It also accept arrays. This will match match // "fool", "barrier", "barista" etc.
正则表达式运算符
$search->condition('rating', '#\d stars?#', 'REGEX'); // Will match entries where the "rating" property matching "#\d stars?#" // e.g: "1 star", "2 star", "3 stars" etc ( case insensitive ).
数字比较运算符
它还支持“小于”、“大于”、“小于等于”、“大于等于”和“介于”。
$search ->condition('year', 2022, '<') ->condition('year', 1990, '>') ->condition('age', 60, '<=') ->condition('age', 18, '>=') ->condition('price', [10, 50], 'BETWEEN');
否定运算符
您也可以否定运算符。
$search ->condition('title', 'Iliad', '!=') // Different to ( case insensitive ). ->condition('title', ['Iliad', ' Odyssey'], 'NOT IN') // case insensitive. ->condition('price', [10, 50], 'NOT BETWEEN') ->condition('title', ['foo', 'bar'], 'UNLIKE');
多个条件
您可以在搜索中添加多个条件。默认情况下,所有条件都必须满足。
$search = $file->search(); $search ->condition('band', 'Iron Maiden', '=') ->condition('release', 2000, '<'); $results = $search->find(); // Will match entries for Iron Maiden from before the yar 2000.
但您可以将其设置为只需满足一个条件。
$search = $file->search('OR'); $search ->condition('band', 'Blind Guardian', '=') ->condition('band', 'Demons & Wizards', '='); $results = $search->find(); // Will match entries for both Blind Guardian and Demons & Wizards.
条件组
您还可以将条件分组以创建复杂的查询。
$search = $file->search('OR'); $search->andConditionGroup() ->condition('band', 'Angra', '=') ->condition('release', 2010, '<'); $search->andConditionGroup() ->condition('band', 'Almah', '=') ->condition('release', 2013, '>'); $results = $search->find(); // Will match entries for Angra from before 2010 OR // entries for Almah from after 2013
许可证
MIT
如何安装它
使用composer。
composer require adinan-cenci/json-lines