Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: PHP Composer

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer test
22 changes: 18 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
"require": {
"php": "^8.0",
"http-interop/http-factory-guzzle": "^1.2",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0",
"laravel/scout": "^8.0|^9.0|^10.0",
"meilisearch/meilisearch-php": "^1.11"
},
"require-dev": {
"orchestra/testbench": "^9.5",
"phpunit/phpunit": "^9.6|^10.5|^11.4"
"laravel/pint": "^1.22",
"orchestra/testbench": "^9.5|^10.0",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^9.6|^10.5|^11.4",
"rector/rector": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -45,7 +48,18 @@
}
},
"scripts": {
"test": "phpunit tests"
"refactor": "rector",
"lint": "pint",
"test:refactor": "rector --dry-run",
"test:lint": "pint --test",
"test:types": "phpstan analyse --ansi",
"test:unit": "phpunit tests",
"test": [
"@test:refactor",
"@test:lint",
"@test:types",
"@test:unit"
]
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 5
paths:
- src

reportUnmatchedIgnoredErrors: true
44 changes: 44 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"preset": "laravel",
"rules": {
"array_push": true,
"backtick_to_shell_exec": true,
"declare_strict_types": true,
"final_class": true,
"fully_qualified_strict_types": true,
"global_namespace_import": {
"import_classes": true,
"import_constants": true,
"import_functions": true
},
"ordered_class_elements": {
"order": [
"use_trait",
"case",
"constant",
"constant_public",
"constant_protected",
"constant_private",
"property_public",
"property_protected",
"property_private",
"construct",
"destruct",
"magic",
"phpunit",
"method_abstract",
"method_public_static",
"method_public",
"method_protected_static",
"method_protected",
"method_private_static",
"method_private"
],
"sort_algorithm": "none"
},
"ordered_interfaces": true,
"ordered_traits": true,
"protected_to_private": true,
"strict_comparison": true
}
}
20 changes: 20 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__.'/src',
__DIR__.'/tests',
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
typeDeclarations: true,
privatization: true,
earlyReturn: true,
strictBooleans: true,
)
->withPhpSets();
2 changes: 2 additions & 0 deletions src/Contracts/QueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Chr15k\MeilisearchAdvancedQuery\Contracts;

use Closure;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/QuerySegment.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Chr15k\MeilisearchAdvancedQuery\Contracts;

interface QuerySegment
Expand Down
70 changes: 36 additions & 34 deletions src/Expression.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace Chr15k\MeilisearchAdvancedQuery;

use Chr15k\MeilisearchAdvancedQuery\Contracts\QuerySegment;

class Expression implements QuerySegment
final class Expression implements QuerySegment
{
public function __construct(
public string $column,
Expand All @@ -22,37 +24,57 @@ public function compile(): string
));
}

private function build($operator, $field, $value)
public function operatorFunc(?string $operator = null): string
{
return match (strtolower($operator ?? '')) {
'=' => 'eq',
'!=' => 'neq',
'in' => 'in',
'not in' => 'nin',
'>=' => 'gte',
'<=' => 'lte',
'>' => 'gt',
'<' => 'lt',
'to' => 'to',
'not' => 'not',
'exists' => 'exists',
'is empty' => 'empty',
'is null' => 'null',
default => 'eq'
};
}

private function build(string $operator, string $field, $value): string
{
return sprintf("%s {$operator} %s", $field, $this->escape($value));
}

protected function gte(string $field, mixed $value): string
private function gte(string $field, mixed $value): string
{
return $this->build('>=', $field, $value);
}

protected function lte(string $field, mixed $value): string
private function lte(string $field, mixed $value): string
{
return $this->build('<=', $field, $value);
}

protected function eq(string $field, mixed $value): string
private function eq(string $field, mixed $value): string
{
return $this->build('=', $field, $value);
}

protected function not(string $field, mixed $value): string
private function not(string $field, mixed $value): string
{
return 'NOT '.$this->build('=', $field, $value);
}

protected function neq(string $field, mixed $value): string
private function neq(string $field, mixed $value): string
{
return $this->build('!=', $field, $value);
}

protected function in(string $field, array $array): string
private function in(string $field, array $array): string
{
$values = collect($array)
->map(fn ($value) => $this->escape($value))
Expand All @@ -61,7 +83,7 @@ protected function in(string $field, array $array): string
return sprintf('%s IN [%s]', $field, $values);
}

protected function nin(string $field, array $array): string
private function nin(string $field, array $array): string
{
$values = collect($array)
->map(fn ($value) => $this->escape($value))
Expand All @@ -70,27 +92,27 @@ protected function nin(string $field, array $array): string
return sprintf('%s NOT IN [%s]', $field, $values);
}

protected function exists(string $field): string
private function exists(string $field): string
{
return sprintf('%s EXISTS', $field);
}

protected function null(string $field): string
private function null(string $field): string
{
return sprintf('%s IS NULL', $field);
}

protected function empty(string $field): string
private function empty(string $field): string
{
return sprintf('%s IS EMPTY', $field);
}

protected function to(string $field, mixed $value): string
private function to(string $field, mixed $value): string
{
return sprintf('%s %s TO %s', $field, ...$value);
}

protected function bool(string $field, bool $bool): string
private function bool(string $field, bool $bool): string
{
return sprintf('%s = %s', $field, $this->escape($bool));
}
Expand All @@ -105,24 +127,4 @@ private function escape($data)
is_int($data) || is_float($data) ? $data : "'{$data}'"
);
}

public function operatorFunc(?string $operator = null): string
{
return match (strtolower($operator ?? '')) {
'=' => 'eq',
'!=' => 'neq',
'in' => 'in',
'not in' => 'nin',
'>=' => 'gte',
'<=' => 'lte',
'>' => 'gt',
'<' => 'lt',
'to' => 'to',
'not' => 'not',
'exists' => 'exists',
'is empty' => 'empty',
'is null' => 'null',
default => 'eq'
};
}
}
Loading