yuriitatur/search
Composer 安装命令:
composer require yuriitatur/search
包简介
A simple framework/db agnostic api search package
README 文档
README
Search module
A framework-agnostic PHP 8.2+ library for building filterable, sortable, and paginated API queries. It validates incoming request options, composes a QueryBuilder, and executes the query — all driven by a set of composable filters you define.
Installation
composer require yuriitatur/search
Core concepts
Seeker
Seeker is the main entry point. It wires together a validator, a query composer, an executor, and a map of filters:
$seeker = new Seeker(
new RequestValidator(Validation::createValidator()),
new QueryComposer,
new PaginatorQueryExecutor($repository),
DefaultFilters::get()
);
$result = $seeker->seek($request->query->all());
seek() validates the options against the registered filters, builds a QueryBuilder, executes it, and returns an IteratorAggregate result.
Filters
Filters implement FilterInterface:
interface FilterInterface
{
public function applyFilter(QueryBuilder $builder, mixed $value): void;
/** @return Constraint[] */
public function getValidationRules(): array;
}
Each filter both mutates the query and declares its own Symfony Validator constraints. If any option fails validation a ValidationException is thrown before the query is built.
Built-in filters
DefaultFilters::get(array $mergeFilters = [], int $defaultLimit = 100) returns a ready-made filter map:
| Key | Filter | Behaviour |
|---|---|---|
limit | LimitFilter | Sets result limit (default 100) |
pagination | PaginationFilter | Offset-based page navigation, shares the same LimitFilter instance |
all | NoLimit | Removes the limit |
cursor | CursorFilter | Enables cursor-based pagination |
id | PositiveIntegerFilter | Filters by a single positive integer id |
ids | InConcatenatedFilter | Filters by a comma-separated list of ids |
sort | MultiValueFilter(OrderByFilter) | Accepts one or more column:asc\|desc values |
Additional filters available to use directly:
LikeFilter($field, $minLength)— full-text LIKE search on a fieldEnumFilter($field, $enum)— restricts a field to a backed enum's valuesLimitedStringFilter($field, $maxLength)— string equality with a length capCompositeFilter— groups multiple filters under a single keyConcatenatedMultiValueFilter— parses a comma-separated string into multiple filter values
Pass any overrides or additions as the first argument to DefaultFilters::get():
DefaultFilters::get([
'name' => new LikeFilter('name'),
'status' => new EnumFilter('status', StatusEnum::class),
])
Sorting
The sort key accepts column:asc or column:desc. Pass multiple values as an array for multi-column sorting:
$seeker->seek(['sort' => ['created_at:desc', 'name:asc']]);
QueryComposer defaults
QueryComposer applies a default limit (100), offset (0), and sort (id desc) when those are not set by any filter. Override via constructor:
new QueryComposer(defaultLimit: 50, defaultSortColumn: 'created_at', defaultSortDirection: 'asc')
Executors
Offset pagination — PaginatorQueryExecutor
Returns a PaginatorResult with data, totalCount, and helpers like getCurrentPage(), getLastPage(), getPerPage().
$executor = new PaginatorQueryExecutor($repository);
Cursor pagination — CursorQueryExecutor
Returns a CursorResult with data, currentCursor, nextCursor, and previousCursor. Cursor values are opaque strings. The previous cursor is computed lazily and cached.
$executor = new CursorQueryExecutor($repository, $cache, $cursorQueryBuilder, $extractor);
Auto-routing — CombinedExecutor
Routes to CursorQueryExecutor when a cursor option is present in the query, otherwise falls back to PaginatorQueryExecutor. This lets a single endpoint support both pagination styles.
$executor = new CombinedExecutor($paginatorExecutor, $cursorExecutor);
CursorResult and PaginatorResult both implement IteratorAggregate, so the result can always be iterated directly regardless of which executor was used.
Cursor hydrators
CursorQueryExecutor needs to extract the sort-column value from result entities. Three hydrator implementations are provided:
| Class | Requires |
|---|---|
PropertyAccessCursorHydrator | symfony/property-access |
SymfonySerializerCursorHydrator | symfony/serializer |
JmsSerializerCursorHydrator | jms/serializer |
Laravel integration
A Laravel service provider is auto-discovered from composer.json. It registers Symfony Validator, PropertyAccessCursorHydrator as the default hydrator, and a Symfony Cache adapter. No manual registration is needed.
Testing
composer test
License
This code is under MIT license, read more in the LICENSE file.
yuriitatur/search 适用场景与选型建议
yuriitatur/search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「search」 「query」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yuriitatur/search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yuriitatur/search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yuriitatur/search 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
A PSR-7 compatible library for making CRUD API endpoints
Pimcore 10.x Website Indexer (powered by Zend Search Lucene)
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-09