定制 rmtram/array-query 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rmtram/array-query

Composer 安装命令:

composer require rmtram/array-query

包简介

Array query

README 文档

README

Build Status Latest Stable Version Total Downloads License

ArrayQuery

This library provides ORM-like Array filtering.

Install

$ composer require rmtram/array-query

Usage

use Rmtram\ArrayQuery\ArrayQuery;
use Rmtram\ArrayQuery\Queries\Where;

$aq = new ArrayQuery([
    [
        'id' => 1,
        'name' => 'hoge',
        'blog' => [
            'title' => 'hoge blog',
            'category' => 'programming',
            'url'   => '#hoge',
            'end_date' => '2010-10-10',
        ]
    ],
    [
        'id' => 2,
        'name' => 'fuga',
        'blog' => [
            'title' => 'fuga blog',
            'category' => 'anime',
            'url'   => '#fuga',
            'end_date' => null
        ]
    ],
        [
        'id' => 3,
        'name' => 'piyo',
        'blog' => [
            'title' => 'piyo blog',
            'category' => 'anime',
            'url'   => '#piyo',
            'end_date' => '2010-10-14',

        ]
    ],
]);

$results = $aq->in('blog.category', ['anime', 'programming'])
    ->and(function (Where $where) {
        $where->eq('blog.end_date', '2010-10-10')->or(function (Where $where) {
            $where->null('blog.end_date');
        });
    })
    ->all(); // [['id' => 1, ...], ['id' => 2, ...]]

Contents

Methods

Initializations

constructor

Definition

constructor(array $items, bool $resettable = true)

Source code

// resettable = true (default)
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 2)
$aq->eq('id', 2)->count(); // 1

// resettable = false
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
], false);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 1)->eq('id', 2)
$aq->eq('id', 2)->count(); // 0

reset

Definition

reset(): self

Source code

// resettable = false
$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
], false);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 1)->eq('id', 2)
$aq->eq('id', 2)->count(); // 0

// state: eq('id', 2)
$aq->reset()->eq('id', 2)->count(); // 1

Operations

eq

Definition

eq(string $key, mixed $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
]);
$aq->eq('id', 1)->all(); // [['id' => 1, 'age' => 18]]
$aq->eq('id', 1)->eq('age', 18)->all(); // [['id' => 1, 'age' => 18]]
$aq->eq('id', -1)->all(); // []

notEq

Definition

notEq(string $key, mixed $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
    ['id' => 2, 'age' => 18],
]);
$aq->notEq('id', 1)->all(); // [['id' => 2, 'age' => 18]]
$aq->eq('id', 1)->notEq('age', 18)->all(); // []
$aq->notEq('id', -1)->all(); // [['id' => 1, 'age' => 18], ['id' => 2, 'age' => 18]]

in

Definition

in(string $key, array $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
    ['id' => 2, 'age' => 18],
]);
$aq->in('id', [1])->all(); // [['id' => 1, 'age' => 18]]
$aq->in('id', [1, 2])->all(); // [['id' => 1, 'age' => 18], ['id' => 2, 'age' => 18]]
$aq->in('id', [2, 3])->all(); // [['id' => 2, 'age' => 18]]
$aq->in('id', [-1])->all(); // []

notIn

Definition

notIn(string $key, array $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 18],
    ['id' => 2, 'age' => 18],
]);
$aq->notIn('id', [1])->all(); // [['id' => 2, 'age' => 18]]
$aq->notIn('id', [1, 2])->all(); // []
$aq->notIn('id', [2, 3])->all(); // [['id' => 1, 'age' => 18]]
$aq->notIn('id', [-1])->all(); // [['id' => 1, 'age' => 18], ['id' => 2, 'age' => 18]]

null

Definition

null(string $key, bool $checkExistsKey = false): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'address' => null],
    ['id' => 2],
    ['id' => 3, 'address' => 'x'],
]);

$aq->null('address')->all(); // [['id' => 1, 'address' => null], ['id' => 2]]
$aq->null('address', true)->all(); // [['id' => 1, 'address' => null]]

notNull

Definition

notNull(string $key): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'address' => null],
    ['id' => 2],
    ['id' => 3, 'address' => 'x'],
]);

$aq->notNull('address')->all(); // [['id' => 3, 'address' => 'x']]

like

Definition

like(string $key, string $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'name' => 'hoge'],
    ['id' => 2, 'name' => 'fuga'],
]);
$aq->like('name', 'h%')->all(); // [['id' => 1, 'name' => 'hoge']]
$aq->like('name', '%g%')->all(); // [['id' => 1, 'name' => 'hoge'], ['id' => 2, 'name' => 'fuga']]
$aq->like('name', 'nothing')->all(); // []

notLike

Definition

notLike(string $key, string $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'name' => 'hoge'],
    ['id' => 2, 'name' => 'fuga'],
]);
$aq->notLike('name', 'h%')->all(); // [['id' => 2, 'name' => 'fuga']]
$aq->notLike('name', '%g%')->all(); // []
$aq->notLike('name', 'nothing')->all(); // [['id' => 1, 'name' => 'hoge'], ['id' => 2, 'name' => 'fuga']]

gt

gt is an alias for greater than, compare a > b.

Definition

gt(string $key, mixed $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->gt('age', 14)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gt('age', 15)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gt('age', 16)->all(); // [['id' => 3, 'age' => 17]]
$aq->gt('age', 17)->all(); // []

gte

gte is an alias for greater equal than, compare a >= b.

Definition

gte(string $key, mixed $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->gte('age', 15)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gte('age', 16)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->gte('age', 17)->all(); // [['id' => 3, 'age' => 17]]
$aq->gte('age', 18)->all(); // []

lt

lt is an alias for less than, compare a < b.

Definition

lt(string $key, mixed $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->lt('age', 15)->all(); // []
$aq->lt('age', 16)->all(); // [['id' => 3, 'age' => 17]]
$aq->lt('age', 17)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->lt('age', 18)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]

lte

lte is an alias for less equal than, compare a < b.

Definition

lte(string $key, mixed $val): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 15],
    ['id' => 2, 'age' => 16],
    ['id' => 3, 'age' => 17],
]);

$aq->lte('age', 14)->all(); // []
$aq->lte('age', 15)->all(); // [['id' => 3, 'age' => 17]]
$aq->lte('age', 16)->all(); // [['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]
$aq->lte('age', 17)->all(); // [['id' => 2, 'age' => 16], ['id' => 2, 'age' => 16], ['id' => 3, 'age' => 17]]

and

Definition

and(callable $callback(\Rmtram\ArrayQuery\Queries\Where $where)): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'status' => 'active', 'age' => 18],
    ['id' => 2, 'status' => 'active', 'age' => 20],
    ['id' => 3, 'status' => 'active', 'age' => 19],
]);
$aq->eq('status', 'active')
    ->and(function (\Rmtram\ArrayQuery\Queries\Where $where) {
        $where->eq('id', 1)->or(function (\Rmtram\ArrayQuery\Queries\Where $where) {
            $where->eq('age', 19);
        });
    })->all(); 
    // [
    //     ['id' => 1, 'status' => 'active', 'age' => 18],
    //     ['id' => 3, 'status' => 'active', 'age' => 19]
    // ]

or

Definition

or(callable $callback(\Rmtram\ArrayQuery\Queries\Where $where)): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'status' => 'active', 'age' => 18],
    ['id' => 2, 'status' => 'active', 'age' => 20],
    ['id' => 3, 'status' => 'active', 'age' => 19],
]);
$aq->eq('id', 1)
    ->or(function (\Rmtram\ArrayQuery\Queries\Where $where) {
        $where->eq('age', 20);
    })->all(); 
    // [
    //     ['id' => 1, 'status' => 'active', 'age' => 18],
    //     ['id' => 2, 'status' => 'active', 'age' => 20]
    // ]

Executions

generator

Definition

generator(): Generator

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$generator = $aq->eq('id', 1)->generator();

// (Generator)[['id' => 1]]
foreach ($generator as $item) {
    echo $item['id']; // 1
}

all

Definition

all(): array

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->all(); // (array)[['id' => 1], ['id' => 2]]
$aq->eq('id', 2)->all(); // (array)[['id' => 2]]

first

Definition

first(): ?array

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->first(); // (array)['id' => 1]
$aq->eq('id', 2)->first(); // (array)['id' => 2]
$aq->eq('id', 3)->first(); // null

last

Definition

last(): ?array

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->last();  // (array)['id' => 2]
$aq->eq('id', 3)->last();  // null

count

Definition

count(): int

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->count(); // 2
$aq->eq('id', 2)->count(); // 1
$aq->eq('id', 3)->count(); // 0

exists

Definition

exists(): bool

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->exists(); // true
$aq->eq('id', 2)->exists(); // true
$aq->eq('id', 3)->exists(); // false

map

Definition

map(callable $callback(array $item)): array

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->map(function(array $item) {
    return $item['id'];
}); // [1, 2]

$aq->eq('id', 1)->map(function(array $item) {
    return $item['id'];
}); // [1]

$aq->eq('id', 3)->map(function(array $item) {
    return $item['id'];
}); // []

pluck

Definition

pluck(array $keys): array

Source code

$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 1, 'address' => 'a'],
    ['id' => 2, 'age' => 2, 'address' => 'b'],
    ['id' => 3, 'age' => 3, 'address' => 'c'],
]);

$aq->pluck(['id', 'age']); // [['id' => 1, 'age' => 1], ['id' => 2, 'age' => 2], ['id' => 3, 'age' => 3]]

pluckFirst

Definition

pluckFirst(array $keys): ?array

Source code

$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 1, 'address' => 'a'],
    ['id' => 2, 'age' => 2, 'address' => 'b'],
    ['id' => 3, 'age' => 3, 'address' => 'c'],
]);

$aq->pluckFirst(['id', 'age']); // ['id' => 1, 'age' => 1]
$aq->eq('id', -1)->pluckFirst(['id', 'age']); // null

pluckLast

Definition

pluckLast(array $keys): ?array

Source code

$aq = new Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'age' => 1, 'address' => 'a'],
    ['id' => 2, 'age' => 2, 'address' => 'b'],
    ['id' => 3, 'age' => 3, 'address' => 'c'],
]);

$aq->pluckLast(['id', 'age']); // ['id' => 3, 'age' => 3]
$aq->eq('id', -1)->pluckFirst(['id', 'age']); // null

Setters

setDelimiter

Definition

setDelimiter(string $delimiter): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1, 'options' => ['address' => 'x']],
    ['id' => 2],
]);

$aq->eq('options.address', 'x')->exists(); // true

$aq->setDelimiter('@');
$aq->eq('options@address', 'x'); // true;

setResettable

Definition

setResettable(bool $resettable): self

Source code

$aq = new \Rmtram\ArrayQuery\ArrayQuery([
    ['id' => 1],
    ['id' => 2],
]);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 2)
$aq->eq('id', 2)->count(); // 1

$aq->setResettable(false);

$aq->eq('id', 1)->count(); // 1

// state: eq('id', 1)->eq('id', 2)
$aq->eq('id', 2)->count(); // 0

Support versions

  • 7.2
  • 7.3
  • 7.4

LICENSE

MIT

rmtram/array-query 适用场景与选型建议

rmtram/array-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「filter」 「array」 「query」 「array query」 「array filter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rmtram/array-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rmtram/array-query 我们能提供哪些服务?
定制开发 / 二次开发

基于 rmtram/array-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-27