Skip to content

Commit 220a839

Browse files
authored
Refactor/Interface improvement. (#21)
1 parent a937e7f commit 220a839

File tree

13 files changed

+274
-136
lines changed

13 files changed

+274
-136
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Documentation
2+
3+
on:
4+
push:
5+
branches: [ 'main' ]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
matrix:
13+
php-version: [ '8.2' ]
14+
15+
steps:
16+
- name: Checkout source code
17+
uses: actions/checkout@v2
18+
19+
- name: Install PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php-version }}
23+
extensions: mbstring
24+
coverage: xdebug
25+
tools: composer:v2
26+
27+
- name: Cache dependencies
28+
uses: actions/cache@v2
29+
with:
30+
path: ~/.composer/cache
31+
key: php-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
32+
restore-keys: php-${{ matrix.php-version }}-composer-
33+
34+
- name: Validate composer.json and composer.lock
35+
run: composer validate
36+
37+
- name: Install Dependencies
38+
run: composer install --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
39+
40+
- name: Copy Files
41+
run: cp README.md wiki/Home.md
42+
43+
- name: Publish documentation to Wiki
44+
uses: SwiftDocOrg/github-wiki-publish-action@v1
45+
with:
46+
path: "wiki/"
47+
env:
48+
GH_PERSONAL_ACCESS_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Criteria (a.k.a Filter)
1+
# Criteria
22

33
[![Test](https://github.com/ComplexHeart/php-criteria/actions/workflows/test.yml/badge.svg)](https://github.com/ComplexHeart/php-criteria/actions/workflows/test.yml)
44
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ComplexHeart_php-criteria&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=ComplexHeart_php-criteria)
55
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=ComplexHeart_php-criteria&metric=coverage)](https://sonarcloud.io/summary/new_code?id=ComplexHeart_php-criteria)
66

7-
Small implementation of a filter criteria pattern in PHP for Complex Heart SDK. Compose several filters using fluent
7+
Small implementation of a criteria pattern in PHP for Complex Heart SDK. Compose several filters using fluent
88
interface.
99

1010
## Installation
@@ -58,7 +58,6 @@ A `FilterGroup` is a set of filters or conditions that must match all together (
5858
(`OR`), just add more `FilterGroup`.
5959

6060
```php
61-
6261
// Match articles with given term in title, or in tagline, or in content.
6362
$criteria = Criteria::default()
6463
->withFilterGroup(FilterGroup::create()->addFilterContains('title', $term))
@@ -68,3 +67,5 @@ $criteria = Criteria::default()
6867
->withOrderType(Order::TYPE_ASC)
6968
->withPageNumber(3);
7069
```
70+
71+

src/Contracts/CriteriaSource.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ public function orderBy(): string;
5050
public function pageLimit(): int;
5151

5252
/**
53-
* Provides the offset, by default should be 0.
53+
* Provides the offset, by default should be 0. This value will be discarded
54+
* if page number returns a value > 0.
5455
*
5556
* @return int
5657
*/
5758
public function pageOffset(): int;
59+
60+
/**
61+
* Provides the page number. If value > 0, will be used to compute the offset.
62+
* @return int
63+
*/
64+
public function pageNumber(): int;
5865
}

src/Criteria.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public static function create(array $groups, Order $order, Page $page): self
6868
return new self(groups: $groups, order: $order, page: $page);
6969
}
7070

71+
public static function default(): self
72+
{
73+
return self::create(
74+
groups: [],
75+
order: Order::none(),
76+
page: Page::default()
77+
);
78+
}
79+
7180
/**
7281
* Creates a new instance of Criteria from the given data source.
7382
*
@@ -76,21 +85,18 @@ public static function create(array $groups, Order $order, Page $page): self
7685
*/
7786
public static function fromSource(CriteriaSource $source): self
7887
{
79-
return Criteria::create(
88+
return self::create(
8089
groups: map(
81-
fn(array $g): FilterGroup => FilterGroup::createFromArray($g),
90+
fn(array $g): FilterGroup => FilterGroup::fromArray($g),
8291
$source->filterGroups()
8392
),
8493
order: Order::create($source->orderBy(), OrderType::make($source->orderType())),
85-
page: Page::create($source->pageLimit(), $source->pageOffset())
94+
page: $source->pageNumber() > 0
95+
? Page::number($source->pageNumber(), $source->pageLimit())
96+
: Page::create($source->pageLimit(), $source->pageOffset())
8697
);
8798
}
8899

89-
public static function default(): self
90-
{
91-
return self::create(groups: [], order: Order::none(), page: Page::create());
92-
}
93-
94100
/**
95101
* Returns a new instance of the criteria with the given FilterGroups.
96102
*
@@ -114,9 +120,7 @@ public function withFilterGroups(array $groups): self
114120
*/
115121
public function withFilterGroup(FilterGroup|Closure $group): self
116122
{
117-
if (is_callable($group)) {
118-
$group = $group(new FilterGroup());
119-
}
123+
$group = $group instanceof FilterGroup ? $group : $group(FilterGroup::create());
120124

121125
// push single FilterGroup into an array.
122126
$group = is_array($group) ? $group : [$group];
@@ -157,21 +161,21 @@ public function withPage(Page $page): self
157161
return self::create(groups: $this->groups, order: $this->order, page: $page);
158162
}
159163

160-
public function withPageOffset(int $offset): self
164+
public function withPageLimit(int $limit): self
161165
{
162166
return self::create(
163167
groups: $this->groups,
164168
order: $this->order,
165-
page: Page::create($this->pageLimit(), $offset)
169+
page: Page::create($limit, $this->pageOffset())
166170
);
167171
}
168172

169-
public function withPageLimit(int $limit): self
173+
public function withPageOffset(int $offset): self
170174
{
171175
return self::create(
172176
groups: $this->groups,
173177
order: $this->order,
174-
page: Page::create($limit, $this->pageOffset())
178+
page: Page::create($this->pageLimit(), $offset)
175179
);
176180
}
177181

@@ -180,7 +184,7 @@ public function withPageNumber(int $number, int $size = null): self
180184
return self::create(
181185
groups: $this->groups,
182186
order: $this->order,
183-
page: Page::number($number, is_null($size) ? $this->page->limit() : $size)
187+
page: Page::number($number, is_null($size) ? $this->pageLimit() : $size)
184188
);
185189
}
186190

@@ -214,14 +218,14 @@ public function page(): Page
214218
return $this->page;
215219
}
216220

217-
public function pageOffset(): int
221+
public function pageLimit(): int
218222
{
219-
return $this->page->offset();
223+
return $this->page->limit();
220224
}
221225

222-
public function pageLimit(): int
226+
public function pageOffset(): int
223227
{
224-
return $this->page->limit();
228+
return $this->page->offset();
225229
}
226230

227231
public function __toString(): string

src/Filter.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function __construct(
3838
* @param string $field
3939
* @param Operator $operator
4040
* @param mixed $value
41-
*
4241
* @return Filter
4342
*/
4443
public static function create(string $field, Operator $operator, mixed $value): self
@@ -48,82 +47,82 @@ public static function create(string $field, Operator $operator, mixed $value):
4847

4948
/**
5049
* @param array<string, scalar>|array<string> $filter
51-
* @return self
50+
* @return Filter
5251
*/
53-
public static function createFromArray(array $filter): self
52+
public static function fromArray(array $filter): self
5453
{
5554
// check if the array is indexed or associative.
5655
$isIndexed = fn($source): bool => ([] !== $source) && array_keys($source) === range(0, count($source) - 1);
5756

5857
return ($isIndexed($filter))
5958
? self::create(
6059
"$filter[0]",
61-
Operator::make("$filter[1]"),
60+
Operator::create("$filter[1]"),
6261
$filter[2]
6362
)
6463
: self::create(
6564
"{$filter['field']}",
66-
Operator::make("{$filter['operator']}"),
65+
Operator::create("{$filter['operator']}"),
6766
"{$filter['value']}"
6867
);
6968
}
7069

71-
public static function createEqual(string $field, mixed $value): self
70+
public static function equal(string $field, mixed $value): self
7271
{
7372
return self::create($field, Operator::EQUAL, $value);
7473
}
7574

76-
public static function createNotEqual(string $field, mixed $value): self
75+
public static function notEqual(string $field, mixed $value): self
7776
{
7877
return self::create($field, Operator::NOT_LIKE, $value);
7978
}
8079

81-
public static function createGreaterThan(string $field, mixed $value): self
80+
public static function greaterThan(string $field, mixed $value): self
8281
{
8382
return self::create($field, Operator::GT, $value);
8483
}
8584

86-
public static function createGreaterOrEqualThan(string $field, mixed $value): self
85+
public static function greaterOrEqualThan(string $field, mixed $value): self
8786
{
8887
return self::create($field, Operator::GTE, $value);
8988
}
9089

91-
public static function createLessThan(string $field, mixed $value): self
90+
public static function lessThan(string $field, mixed $value): self
9291
{
9392
return self::create($field, Operator::LT, $value);
9493
}
9594

96-
public static function createLessOrEqualThan(string $field, mixed $value): self
95+
public static function lessOrEqualThan(string $field, mixed $value): self
9796
{
9897
return self::create($field, Operator::LTE, $value);
9998
}
10099

101-
public static function createIn(string $field, mixed $value): self
100+
public static function in(string $field, mixed $value): self
102101
{
103102
return self::create($field, Operator::IN, $value);
104103
}
105104

106-
public static function createNotIn(string $field, mixed $value): self
105+
public static function notIn(string $field, mixed $value): self
107106
{
108107
return self::create($field, Operator::NOT_IN, $value);
109108
}
110109

111-
public static function createLike(string $field, mixed $value): self
110+
public static function like(string $field, mixed $value): self
112111
{
113112
return self::create($field, Operator::LIKE, $value);
114113
}
115114

116-
public static function createNotLike(string $field, mixed $value): self
115+
public static function notLike(string $field, mixed $value): self
117116
{
118117
return self::create($field, Operator::NOT_LIKE, $value);
119118
}
120119

121-
public static function createContains(string $field, mixed $value): self
120+
public static function contains(string $field, mixed $value): self
122121
{
123122
return self::create($field, Operator::CONTAINS, $value);
124123
}
125124

126-
public static function createNotContains(string $field, mixed $value): self
125+
public static function notContains(string $field, mixed $value): self
127126
{
128127
return self::create($field, Operator::NOT_CONTAINS, $value);
129128
}
@@ -165,7 +164,7 @@ public function __toString(): string
165164
$this->field(),
166165
$this->operator()->value,
167166
is_array($this->value())
168-
? implode('|', $this->value())
167+
? implode(',', $this->value())
169168
: $this->value()
170169
);
171170
}

0 commit comments

Comments
 (0)