vaened / laravel-criteria
eloquent的搜索引擎构建器
v1.1
2024-08-30 19:45 UTC
Requires
- php: ^8.1
- lambdish/phunctional: ^2.1
- laravel/framework: ^10.21|^v11.15
- vaened/php-criteria-core: ^1.0
- vaened/support: ^4.0
Requires (Dev)
- orchestra/testbench: ^8|^9
- phpunit/phpunit: ^10.4
README
eloquent的搜索引擎构建器
composer require vaened/laravel-criteria
用法
将php类转换成强大的、易读的、友好的查询构建器
。
use Flagable, Indexed; public function __construct( private readonly IndexRepository $index, private readonly FlagFiltrator $filter ) { $this->apply(Criterias\PatientDeletionDate::without()); } public function affiliatedBetween(DateTimeInterface $start, DateTimeInterface $end): self { $this->apply(Criterias\PatientAffiliation::between($start, $end)); } public function observationLikeTo(string $observation): self { $this->apply(Criterias\PatientObservation::startsWith($observation)); } public function onlyObserved(): self { $this->apply(Criterias\PatientObservation::isNotNull()); } public function withoutObservation(): self { $this->apply(Criterias\PatientObservation::isNull()); } public function historyEqualsTo(string $clinicHistory): self { $this->apply(Criterias\PatientClinicHistory::equals($clinicHistory)); } public function documentNotEqualsTo(string $documentNumber): self { $this->apply(Criterias\PatientIdentityDocument::notEquals($documentNumber)); } public function inDocuments(array $documentNumbers): self { $this->apply(Criterias\PatientIdentityDocument::in($documentNumbers)); } protected function query(): Builder { return Patient::query(); }
已索引
通过索引
进行查询,评估搜索字符串并根据给定的模式确定索引。
public function indexes(): FilterBag { return FilterBag::open() ->register(PatientIndex::Patient, $this->patientMustBe()) ->register(PatientIndex::Name, $this->nameStartsWith()) ->register(PatientIndex::Document, $this->documentIsEqualsTo()); } protected function documentIsEqualsTo(): Closure { return static fn(string $documentNumber) => PatientIdentityDocument::equals($documentNumber); } protected function nameStartsWith(): Closure { return static fn(string $name) => PatientName::startsWith($name); } private function patientMustBe(): callable { return static fn(string $queryString) => QueryStringMatcher::of( Query::must( target: 'document', mode: FilterOperator::Equal, expression: new NumericFixer(8) ), )->solve($queryString) ?: PatientName::startsWith($queryString); }
过滤器
通过PHP枚举应用动态的过滤器
。
public function flags(): FilterBag { return FilterBag::open() ->register(PatientFlag::Observed, $this->onlyObserved()) ->register(PatientFlag::WithAccount, $this->onlyWithAccount()); } private function onlyObserved(): Closure { return static fn() => PatientObservation::isNotNull(); } private function onlyWithAccount(): Closure { return static fn() => PatientScope::accounted(); }
更多文档
你可以在源代码以及位于tests
目录中的测试中找到很多注释。