icanboogie/facets
Composer 安装命令:
composer require icanboogie/facets
包简介
Makes it easy to implement faceted search.
README 文档
README
Together with the icanboogie/activerecord package, this library makes it easy to implement faceted search. The library makes it especially easy to parse query strings (bag of words), use serialized criterion values such as sets (e.g. "1|2|3") or intervals (.e.g. "1990..2010"), and fetch records matching an array of conditions.
Fetching records matching conditions
A BasicFetcher instance can be used to fetch records matching a set of conditions. The fetcher takes care of the various steps required to build the query and fetch the matching records. These steps can be summarized as follows:
- Parse the specified modifiers and extract conditions, offset, limit, order and query string.
- Build the initial query.
- Invoke criteria to alter the query.
- Alter the query with the conditions.
- Count the total number of records that match the query.
- Alter the query with the order.
- Alter the query with the offset and limit.
- Fetch the records matching the query.
- Invoke criteria to alter the records.
- Return a RecordCollection instance containing the records.
The following example demonstrates how a BasicFetcher instance can be used to fetch online articles that are classified in the "music" category, and were published between 2010 and 2014. A maximum of 10 articles can be fetched, and they are ordered starting with the most recent:
<?php use ICanBoogie\ActiveRecord; use ICanBoogie\Facets\Fetcher\BasicFetcher; $model = ActiveRecord\get_model('articles'); $fetcher = new BasicFetcher($model); $records = $fetcher([ 'year' => "2010..2014", 'is_online' => true, 'category' => "music", 'order' => "-date", 'limit' => 10 ]);
Fetch records using a model
The package adds the fetch_records() and fetch_record() methods to Model instances,
which allow for records to be fetched directly from the model, without requiring a
BasicFetcher instance to be built.
$records = $model->fetch_records([ 'year' => "2010..2014", 'is_online' => true, 'category' => "music", 'order' => "-date", 'limit' => 10 ]);
Note that the BasicFetcher instance created to fetch the records can be obtained using the second argument of the methods.
$records = $model->fetch_records($conditions, $fetcher);
Properties of interest
Once the records have been returned the following properties might be of interest:
query_string: A QueryString instance resolved from theqmodifier.conditions: An array of conditions used to filter the fetched records.order: The order in which records are fetched, as defined by theordermodifier.count: The number of records matching the query before the offset and limit were applied.
Altering the fetched records
Fetched records are returned as a RecordCollection instance, such an instance can be used to
fire the alter event of class RecordCollection\AlterEvent. Event hooks may use this event to
alter the records of the collection, for instance fetching the images associated with a
collection of articles using a single query.
<?php use ICanBoogie\Facets\RecordCollection; $records = $fetcher(…); new RecordCollection\AlterEvent($records);
Fetching records using a CriterionList instance
If using a BasicFetcher instance is not enough of a challenge for you, you can use a CriterionList instead and do all the hard work yourself:
<?php use ICanBoogie\Facets\CriterionList; use App\Modules\Vehicles; $criterion_list = new CriterionList([ 'family' => Vehicles\Families\FamilyCriterion::class, 'brand' => Vehicles\Brands\BrandCriterion::class, 'category' => Vehicles\Categories\CategoryCriterion::class, 'color' => Vehicles\Colors\ColorCriterion::class, 'energy' => Vehicles\Energies\EnergyCriterion::class, 'engine' => Vehicles\Engines\EngineCriterion::class, 'doors' => Vehicles\DoorsCriterion::class, 'year' => Vehicles\YearCriterion::class, 'price' => Vehicles\PriceCriterion::class ]); $modifiers = $_GET + [ 'q' => null, // reserved keyword for query string 'order' => null // reserved keyword for records order ]; if ($modifiers['q']) { $q = $criterion_list->parse_query_string($modifiers['q']); echo "The following words were matched: " . implode(' ', $q->matched) . '<br />'; echo "The following words were not matched: " . implode(' ', $q->not_matched) . '<br />'; // we choose to _OR_ criterion values $modifiers += array_map(function($v) { return implode('|', $v); }, $q->matches); } # # Parameters are passed by reference, $values and $query are likely to be modified. # $conditions = []; $criterion_list ->alter_conditions($conditions, $modifiers) ->alter_query($query) ->alter_query_with_conditions($query, $conditions); if ($modifiers['order']) { $criterion_list->alter_query_with_order($query, $modifiers['order']); } $count = $query->count; // count all the records matching the query $records = $query->limit(20)->all; // fetch a maximum of 20 records
Criterion values
Criterion values are usually created when a CriterionList instance alters a query with values. If a value's key matches a criterion identifier, the parse_value() of that criterion is invoked to retrieve a criterion value, which might be the exact same value, or a CriterionValue instance if the value is complex, for instance an interval or a set.
The resulting criterion value is used to alter the query during alter_query_with_value().
Interval values result in BETWEEN ? AND ?, => ?, and <= ? conditions; while set values
result in IN(?) conditions.
Interval values
Interval values are represented by IntervalCriterionValue instances. When specified as a string
two dots .. are used to separate the lower and the upper bound. An interval value can
be created with any of the following statements:
<?php use ICanBoogie\Facets\CriterionValue\IntervalCriterionValue; $value = IntervalCriterionValue::from('123..456'); // between 123 and 456 $value = IntervalCriterionValue::from('123..'); // >= 123 $value = IntervalCriterionValue::from('..456'); // <= 456 $value = IntervalCriterionValue::from([ 'min' => '123', 'max' => '456' ]); // between 123 and 456 $value = IntervalCriterionValue::from([ 'min' => '123', 'max' => null ]); // >= 123 $value = IntervalCriterionValue::from([ 'min' => null, 'max' => '456' ]); // <= 456 $value = new IntervalCriterionValue(123, 456); // between 123 and 456 $value = new IntervalCriterionValue(null, 456); // >= 123 $value = new IntervalCriterionValue(123, null); // <= 456
IntervalCriterionValue instances can also be used as strings:
<?php use ICanBoogie\Facets\CriterionValue\IntervalCriterionValue; echo new IntervalCriterionValue(123, 456); // "123..456" echo new IntervalCriterionValue(123, null); // "123.." echo new IntervalCriterionValue(null, 456); // "..456" echo new IntervalCriterionValue(123, 123); // "123" echo new IntervalCriterionValue(null, null); // ""
IntervalCriterionValue instances can be used by criteria to create BETWEEN ? AND ?, >= ?,
and <= ? conditions while they alter the query.
Set values
Set values are represented by SetCriterionValue instances. When specified as a string the pipe character "|" is used to separate the values. A set value can be created with any of the following statements:
<?php use ICanBoogie\Facets\CriterionValue\SetCriterionValue; $value = SetCriterionValue::from('1|2'); // 1 or 2 $value = SetCriterionValue::from([ 1 => 'on', 2 => 'on' ]); // 1 or 2 $value = new SetCriterionValue([ 1, 2 ]); // 1 or 2
SetCriterionValue instances can also be used as strings:
<?php use ICanBoogie\Facets\CriterionValue\SetCriterionValue; echo new SetCriterionValue([ 1, 2, 3 ]); // "1|2|3" echo new SetCriterionValue([ 1 ]); // "1" echo new SetCriterionValue([ ]); // ""
SetCriterionValue instances can be used by criteria to create IN(?) conditions while they
alter the query.
Associating criteria with models
Criteria are associated with models using activerecord config fragments and the facets key.
The criteria for a model are specified using the model's identifier.
The activerecord_facets config is synthesized from fragments. The criteria and criterion_list
getters are added to the Model class by the package and are used to respectively retrieve the
config criteria and the CriterionList instance associated with a model.
Note: This feature currently requires the ICanBoogie framework and the
icanboogie/bind-facets package. A similar feature can be implemented using only the
icanboogie/prototype package. In which case, you only need to define the criteria and
criterion_list getters for the Model class.
The following example demonstrates how the nid and slug criteria are associated with the
nodes model, and how the month and year criteria are associated with the articles model:
<?php // config/activerecord.php return [ 'facets' => [ 'nodes' => [ 'nid' => Icybee\Modules\Nodes\NidCriterion::class, 'slug' => Icybee\Modules\Nodes\SlugCriterion::class ], 'articles' => [ 'month' => Icybee\Modules\Articles\MonthCriterion::class, 'year' => Icybee\Modules\Articles\YearCriterion::class ] ] ];
Note that criteria are inherited. In our example, because articles extends nodes it inherits
its nid and slug criteria.
Obtaining the criteria associated with a model
The criteria array can be retrieved from a model using the criteria getter that is added by the
package. The getter returns the criteria and inherited criteria as they are defined in the
activerecord_facets config.
<?php use ICanBoogie\ActiveRecord; $model = ActiveRecord\get_model('articles'); array_keys($model->criteria); # [ 'nid', 'slug', 'month', 'year' ]
The criteria and criterion_list getters are added to the Model class
Obtaining the CriterionList instance associated with a model
The CriterionList instance associated with a model can be retrieved from a model using the
criterion_list getter that is added by the package. The getter returns a CriterionList
instance created from the criteria obtained through the criteria getter.
<?php $criterion_list = $model->criterion_list;
Requirements
The package requires PHP 5.5 or later.
Installation
composer require icanboogie/facets
Documentation
The package is documented as part of the ICanBoogie framework
documentation. You can generate the documentation for the package and its dependencies with
the make doc command. The documentation is generated in the build/docs directory.
ApiGen is required. The directory can later be cleaned with
the make clean command.
Testing
Run make test-container to create and log into the test container, then run make test to run the
test suite. Alternatively, run make test-coverage to run the test suite with test coverage. Open
build/coverage/index.html to see the breakdown of the code coverage.
License
icanboogie/facets is released under the New BSD License.
icanboogie/facets 适用场景与选型建议
icanboogie/facets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.24k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「activerecord」 「criterion」 「faceted」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 icanboogie/facets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 icanboogie/facets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 icanboogie/facets 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ActiveRecord for API
A Laravel package to retrieve data from Google Search Console
CalendarView widget for Yii 2 Framework.
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Relational attributes support for YII2 ActiveRecord
统计信息
- 总下载量: 1.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-07-07