Skip to content

Commit 2ff53bc

Browse files
authored
Feature/Add documentation workflow. (#2)
1 parent 120862e commit 2ff53bc

File tree

5 files changed

+213
-2
lines changed

5 files changed

+213
-2
lines changed

.github/workflows/documentation.yml

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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
# Complex Heart On Laravel
22

3-
Laravel integration with Complex Heart library.
3+
[![Test](https://github.com/ComplexHeart/on-laravel/actions/workflows/test.yml/badge.svg)](https://github.com/ComplexHeart/on-laravel/actions/workflows/test.yml)
4+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ComplexHeart_on-laravel&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=ComplexHeart_on-laravel)
5+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=ComplexHeart_on-laravel&metric=coverage)](https://sonarcloud.io/summary/new_code?id=ComplexHeart_on-laravel)
6+
7+
Laravel integration with Complex Heart SDK.
8+
9+
## Installation
10+
11+
Just install the package from Packagist using composer:
12+
13+
```bash
14+
composer require complex-heart/on-laravel
15+
```
16+
17+
## Usage
18+
19+
- [HTTP Request as Criteria Source](https://github.com/ComplexHeart/on-laravel/wiki/HTTP-Request-as-Criteria-Source)
20+
- [Eloquent Criteria Parser](https://github.com/ComplexHeart/on-laravel/wiki/Eloquent-Criteria-Parser)
21+
22+
23+

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
sonar.projectName=Complex Heart on Laravel
1+
sonar.projectName=On Laravel
22
sonar.projectKey=ComplexHeart_on-laravel
33
sonar.organization=complexheart
44
sonar.language=php

wiki/Eloquent-Criteria-Parser.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Eloquent Criteria Parser
2+
3+
Using the Eloquent Criteria Parser is quite simple. Just instantiate the class, next, pass a Criteria instance to
4+
the `applyCriteria()` method along with a `EloquentQueryBuilder` instance:
5+
6+
```php
7+
$parser = new BasicCriteriaParser();
8+
$query = $parser->applyCriteria(User::query(), $criteria);
9+
```
10+
11+
The returned `EloquentQueryBuilder` has the criteria applied. You just need to call the `get` method to fetch the data
12+
from the database.
13+
14+
```php
15+
$users = $query->get();
16+
```
17+
18+
Alternatively, you can pass an array of strings to map the attributes between the domain and the database.
19+
20+
```php
21+
$parser = new BasicCriteriaParser([
22+
'domain-attribute' => 'database-attribute',
23+
]);
24+
```
25+
26+
For example,
27+
given the following table:
28+
29+
```php
30+
$this->builder->create('users', function (Blueprint $table) {
31+
$table->uuid('id');
32+
$table->string('first_name');
33+
$table->string('last_name');
34+
$table->string('email')->unique();
35+
$table->string('bio')->nullable();
36+
$table->timestamps();
37+
});
38+
```
39+
40+
You may use the following configuration to use `name` and `surname` instead of `first_name` and `last_name`:
41+
42+
```php
43+
$parser = new BasicCriteriaParser([
44+
'name' => 'first_name',
45+
'surname' => 'last_name'
46+
]);
47+
```
48+
49+
A criteria search will be something like this:
50+
51+
```php
52+
$criteria = Criteria::default()
53+
->withFilterGroup(FilterGroup::create()
54+
->addFilterEqual('name', 'Vicent'));
55+
56+
$builder = User::query();
57+
58+
$parser = new BasicCriteriaParser();
59+
$users = $parser
60+
->applyCriteria($builder, $criteria)
61+
->get();
62+
```
63+
64+
This is useful to expose different attributes from different interfaces as HTTP, or CLI.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# HTTP Request as Criteria source
2+
3+
Let's see a brief example of how to implement a `CriteriaSource` along with **Laravel** HTTP Request.
4+
5+
First, create a [Form Request](https://laravel.com/docs/10.x/validation#creating-form-requests) to implement
6+
the `CriteriaSource` interface.
7+
8+
```bash
9+
php artisan make:request SearchUserRequest
10+
```
11+
12+
Next, implement the interface:
13+
14+
```php
15+
namespace LaravelMade\Http\Requests;
16+
17+
use ComplexHeart\Domain\Criteria\Contracts\CriteriaSource;
18+
use Illuminate\Foundation\Http\FormRequest;
19+
20+
class SearchUserRequest extends FormRequest implements CriteriaSource
21+
{
22+
public function filterGroups(): array
23+
{
24+
// get the filter groups from the request.
25+
// you can also return N groups of filters (OR).
26+
return [$this->input('filters', [])];
27+
}
28+
29+
public function orderType(): string
30+
{
31+
return $this->input('order', 'none');
32+
}
33+
34+
public function orderBy(): string
35+
{
36+
return $this->input('orderBy', '');
37+
}
38+
39+
public function pageLimit(): int
40+
{
41+
return $this->input('limit', 25);
42+
}
43+
44+
public function pageOffset(): int
45+
{
46+
return $this->input('offset', 0);
47+
}
48+
49+
public function pageNumber(): int
50+
{
51+
return $this->input('page', 0);
52+
}
53+
}
54+
```
55+
56+
Done, now you only need to call the `fromSource` method of the `Criteria` object.
57+
58+
```php
59+
Route::get('users', function (SearchUserRequest $request): JsonResponse {
60+
$criteria = Criteria::fromSource($request);
61+
62+
// use criteria to fetch the users.
63+
});
64+
```
65+
66+
Additionally, you can add rules to the `FormRequest` object to ensure the `Criteria` is properly instantiated. If the
67+
`Criteria` object cannot be instantiated a `CriteriaError` will be thrown.
68+
69+
```php
70+
Route::get('users', function (SearchUserRequest $request): JsonResponse {
71+
try {
72+
$criteria = Criteria::fromSource($request);
73+
} catch (CriteriaError $e) {
74+
// handle the Criteria error
75+
}
76+
77+
// use criteria to fetch the users.
78+
});
79+
```

0 commit comments

Comments
 (0)