nahid / qarray
QArray 是查询数组的 PHP 抽象
Requires
- php: >=5.6
- ext-json: *
- myclabs/deep-copy: ^1.8
Requires (Dev)
- phpunit/phpunit: ^5.0 || ^6.0
- symfony/var-dumper: ^3.4
This package is auto-updated.
Last update: 2024-09-21 11:56:49 UTC
README
# QArray - 数组查询引擎QArray 是 PHP 数组数据的查询引擎。它帮助开发者以 ORM 类似的方式查询任何类型的数组数据。
安装
composer require nahid/qarray
实现
由于 QArray 是代码的抽象层,因此您必须使用大量预构建的功能来实现自己的系统。
让我们开始我们的第一个 JSON 数据实现。
class JsonQueryEngine extends \Nahid\QArray\QueryEngine { public function readPath($path) { $data = file_get_contents($path); return json_decode($data, true); } public function parseData($data) { return json_decode($data, true); } }
在这里,我们实现了一个新的 JSON 数据引擎。 QueryEngine
是一个抽象类,因此我们必须实现它的两个抽象方法 readPath
和 parseData
用法
我们的实现已经完成,现在我们可以使用这个实现了。
class Product { protected $query; public function __construct(\Nahid\QArray\QueryEngine $query) { $this->query = $query; } public function getMacbook() { try { return $this->query ->from('.') ->where('cat', 2) ->get(); } catch (\Exception $e) { return false; } } }
在这里,我们开发了一个用于产品的类,该类使用 JsonQueryEngine
从 JSON 文件中获取数据。
让我们看看如何使用它。
这是我们 JSON 文件 data.json
[ {"id":1, "name":"iPhone", "cat":1, "price": 80000}, {"id":2, "name":"macbook pro 2017", "cat": 2, "price": 210000}, {"id":3, "name":"Redmi 3S Prime", "cat": 1, "price": 12000}, {"id":4, "name":"Redmi 4X", "cat":1, "price": 15000}, {"id":5, "name":"macbook air", "cat": 2, "price": 110000} ]
现在我们读取它进行数据查询。
$data = new JsonQueryEngine('data.json'); $query = new SomethingBuilder($data); dump($query->getMacbook()->toArray());
输出
array:2 [
1 => array:4 [
"id" => 2
"name" => "macbook pro 2017"
"cat" => 2
"price" => 210000
]
4 => array:4 [
"id" => 5
"name" => "macbook air"
"cat" => 2
"price" => 110000
]
]
相当整洁,不是吗?
让我们探索完整的 API,看看这个库还能为你做些什么。好吗?
API
API 列表
- fetch
- find
- at
- from
- select
- except
- then
- collect
- json
- import
- where
- orWhere
- whereIn
- whereNotIn
- whereNull
- whereNotNull
- whereStartsWith
- whereEndsWith
- whereContains
- whereMatch
- whereInstance
- whereDataType
- sum
- count
- size
- max
- min
- avg
- first
- last
- nth
- column
- implode
- exists
- groupBy
- sort
- sortBy
- reset
- copy
- toJson
- keys
- values
- filter
- transform
- each
- pipe
- chunk
- macro
where 子句可用的操作
-
key
-- 数据的属性名。或者您也可以在这里传递一个函数来对多个查询进行分组。请参阅示例中的详细信息 -
val
-- 要匹配的值。它可以是 int、string、bool,甚至是 Function - 依赖于op
。 -
op
-- 用于匹配的运算符。以下运算符可供使用=
: 用于弱相等匹配eq
: 与=
相同!=
: 用于弱不等匹配neq
: 与!=
相同==
: 用于严格相等匹配seq
: 与==
相同!==
: 用于严格不等匹配sneq
: 与!==
相同>
: 检查数据中 key 的值是否大于 valgt
: 与>
相同<
: 检查数据中 key 的值是否小于 vallt
: 与<
相同>=
: 检查数据中 key 的值是否大于或等于 valgte
: 与>=
相同<=
: 检查数据中 key 的值是否小于或等于 vallte
: 与<=
相同null
:检查数据中给定 键 的值是否为 null(对于此op
,where()
中的val
参数可以省略)notnull
:检查数据中给定 键 的值是否为 非 null(对于此op
,where()
中的val
参数可以省略)in
:检查数据中给定 键 的值是否存在于给定的 val 中。 val 应该是一个普通的 数组。notin
:检查数据中给定 键 的值是否不存在于给定的 val 中。 val 应该是一个普通的 数组。startswith
:检查数据中给定 键 的值是否以给定的 val 开头。这仅适用于 字符串 类型数据。endswith
:检查数据中给定 键 的值是否以给定的 val 结尾。这仅适用于 字符串 类型数据。contains
:检查数据中给定 键 的值是否包含给定的 val 子串。这仅适用于 字符串 类型数据。match
:检查数据中给定 键 的值是否与给定的 val 匹配正则表达式。对于此op
,val
参数应是一个 RegExp。instance
:检查数据中给定key
的值是否有实例。
示例
假设你想找到具有 'users' 的 id
为 1
的用户。你可以这样做
$q = new Jsonq('data.json'); $res = $q->from('users')->where('id', '=', 1)->get();
你可以添加多个 where 条件。它将通过 AND 逻辑运算符将多个 where 条件的结果结合起来。
$q = new Jsonq('data.json'); $res = $q->from('users') ->where('id', '=', 1) ->where('location', '=', 'barisal') ->get();
详细了解示例 在此。
orWhere(key, op, val)
where()
的参数与 where()
相同。 where()
和 orWhere()
之间的唯一区别是:由 orWhere()
方法提供的条件将与其他条件进行 OR 运算。
例如,如果你想找到具有 id 为 1
或 2
的用户,你可以这样做
$q = new Jsonq('data.json'); $res = $q->from('users') ->where('id', '=', 1) ->orWhere('id', '=', 2) ->get();
详细了解示例 在此。
whereIn(key, val)
key
:数据的属性名val
:它应该是一个 数组
此方法的行为类似于调用 where(key, 'in', val)
方法。
whereNotIn(key, val)
key
:数据的属性名val
:它应该是一个 数组
此方法的行为类似于调用 where(key, 'notin', val)
方法。
whereNull(key)
key
:数据的属性名
此方法的行为类似于调用 where(key, 'null')
或 where(key, '=', null)
方法。
whereNotNull(key)
key
:数据的属性名
此方法的行为类似于调用 where(key, 'notnull')
或 where(key, '!=', null)
方法。
whereStartsWith(key, val)
key
:数据的属性名val
:它应该是一个 字符串
此方法的行为类似于调用 where(key, 'startswith', val)
方法。
whereEndsWith(key, val)
key
:数据的属性名val
:它应该是一个 字符串
此方法的行为类似于调用 where(key, 'endswith', val)
方法。
whereContains(key, val)
key
:数据的属性名val
:它应该是一个 字符串
此方法的行为类似于调用 where(key, 'contains', val)
方法。
whereDataType(key, val)
key
:数据的属性名val
:它应该是一个 字符串
此方法的行为类似于调用 whereDataType(key, 'type', val)
方法。
sum(column)
column
:数据的属性名
错误和问题
如果你遇到任何错误或问题,请随意在 github 上 打开一个问题。
此外,你也可以通过 mailto:nahid.dns@gmail.com 发送邮件给我,无论是关于拥抱还是错误。