Skip to content

Commit 248e5d5

Browse files
author
Marius
committed
init
1 parent d4be9db commit 248e5d5

File tree

6 files changed

+470
-0
lines changed

6 files changed

+470
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/.idea
3+
composer.lock

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# laravel-crud-wizard-client-free
2+
3+
This is a stripped down version of [laravel-lumen-crud-wizard-client](https://github.com/macropay-solutions/laravel-lumen-crud-wizard-client)
4+
5+
that can be used for calling [laravel-crud-wizard-free](https://github.com/macropay-solutions/larave-crud-wizard-free)
6+
7+
## Install
8+
9+
composer require macropay-solutions/laravel-crud-wizard-client-free
10+
11+
## Start using it
12+
13+
```php
14+
15+
$crud = new \MacroPaySolutions\LaravelCrudWizardClient\RequestBuilder(\env('API_BEARER'), \env('APP_URL'));
16+
17+
try {
18+
$result = $crud->list('clients', $crud->getBuilder()->sort('country', 'asc')
19+
->sort('zip')->equals('name', 'alt')->withRelation('relation')
20+
->withRelations(['rel1', 'rel2'])
21+
->addCountRelations(['relation1'])->addExistRelation('relation2');
22+
$result = $crud->get('clients', '73', ['rel1', 'rel2']);
23+
$result = $crud->delete('clients', '73');
24+
$result = $crud->create('clients', [
25+
'active' => '1',
26+
'name' => 'abc',
27+
// ...
28+
]);
29+
$result = $crud->update('clients', '73', [
30+
'active' => '1',
31+
'name' => 'abc',
32+
// ...
33+
]);
34+
} catch (\Exception $e) {
35+
$decodedErrorMessage = \json_decode($e->getMessage());
36+
echo $decodedErrorMessage->message;
37+
}
38+
39+
$decodedResult = \json_decode($result);
40+
```
41+
42+
Discover more methods available by installing it.

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "macropay-solutions/laravel-crud-wizard-client-free",
3+
"type": "library",
4+
"description": "Request builder for calling projects that implement macropay-solutions/laravel-crud-wizard-free",
5+
"keywords": [
6+
"crud",
7+
"wizard",
8+
"builder",
9+
"client",
10+
"initiator",
11+
"free"
12+
],
13+
"minimum-stability": "dev",
14+
"prefer-stable": true,
15+
"license": "MIT",
16+
"require": {
17+
"php": "^7.4|^8.0",
18+
"ext-json": "*"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "^9.4",
22+
"guzzlehttp/guzzle": "^7.5.0",
23+
"psr/log": "^1.0 || ^2.0 || ^3.0"
24+
},
25+
"extra": {
26+
"branch-alias": {
27+
"dev-master": "production"
28+
}
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"MacropaySolutions\\LaravelCrudWizardClient\\": "src/"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"MacropaySolutions\\LaravelCrudWizardClient\\Test\\": "tests/"
38+
}
39+
}
40+
}

src/QueryStringBuilder.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace MacropaySolutions\LaravelCrudWizardClient;
4+
5+
/**
6+
* @see ../README.md
7+
*/
8+
class QueryStringBuilder
9+
{
10+
public bool $sqlDebug = false;
11+
private ?int $simplePaginate = null;
12+
private ?string $cursor = null;
13+
private int $page = 1;
14+
private int $limit = 10;
15+
private array $sort = [];
16+
private array $withRelations = [];
17+
private array $countRelations = [];
18+
private array $existRelations = [];
19+
private ResourceFilterBuilder $resourceFilters;
20+
private array $rawRequest = [];
21+
22+
public function __construct()
23+
{
24+
$this->resourceFilters = new ResourceFilterBuilder();
25+
}
26+
27+
/**
28+
* by using this, all other conditions set by using other methods will be ignored
29+
*/
30+
public function setRawRequest(array $request): self
31+
{
32+
$this->rawRequest = $request;
33+
34+
return $this;
35+
}
36+
37+
public function page(int $page): self
38+
{
39+
$this->page = $page;
40+
41+
return $this;
42+
}
43+
44+
public function limit(int $limit): self
45+
{
46+
$this->limit = $limit;
47+
48+
return $this;
49+
}
50+
51+
public function sort(string $by, string $dir = 'DESC'): self
52+
{
53+
$this->sort[] = ['by' => $by, 'dir' => $dir];
54+
55+
return $this;
56+
}
57+
58+
public function equals(string $column, $is): self
59+
{
60+
$this->resourceFilters->equals($column, $is);
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @param string|null $cursor use '1' for 1st page, null for non cursor pagination
67+
*/
68+
public function simplePaginate(?string $cursor = null): self
69+
{
70+
$this->simplePaginate = 1;
71+
$this->cursor = $cursor;
72+
73+
return $this;
74+
}
75+
76+
public function withRelation(string $relation): self
77+
{
78+
$this->withRelations[] = $relation;
79+
80+
return $this;
81+
}
82+
83+
public function withRelations(array $relations): self
84+
{
85+
$this->withRelations = \array_merge(\array_values($relations), $this->withRelations);
86+
87+
return $this;
88+
}
89+
90+
public function addCountRelation(string $countRelation): self
91+
{
92+
$this->countRelations[] = $countRelation;
93+
94+
return $this;
95+
}
96+
97+
public function addCountRelations(array $countRelations): self
98+
{
99+
$this->countRelations = \array_merge(\array_values($countRelations), $this->countRelations);
100+
101+
return $this;
102+
}
103+
104+
public function addExistRelation(string $existRelation): self
105+
{
106+
$this->existRelations[] = $existRelation;
107+
108+
return $this;
109+
}
110+
111+
public function addExistRelations(array $existRelations): self
112+
{
113+
$this->existRelations = \array_merge(\array_values($existRelations), $this->existRelations);
114+
115+
return $this;
116+
}
117+
118+
public function getUrlQueryString(array $overrides = []): string
119+
{
120+
if ($this->rawRequest !== []) {
121+
return \http_build_query(\array_merge(
122+
$this->rawRequest,
123+
$overrides
124+
), '', '&', PHP_QUERY_RFC3986);
125+
}
126+
127+
return \http_build_query(\array_merge(
128+
$this->getAllFilters(),
129+
$overrides
130+
), '', '&', PHP_QUERY_RFC3986);
131+
}
132+
133+
public function getAllFilters(): array
134+
{
135+
return \array_merge(
136+
$this->getFilters(),
137+
$this->sqlDebug ? ['sqlDebug' => 1] : [],
138+
$this->simplePaginate !== null ? ['simplePaginate' => $this->simplePaginate] : [],
139+
$this->cursor !== null ? ['cursor' => $this->cursor] : [],
140+
);
141+
}
142+
143+
private function getFilters(): array
144+
{
145+
return \array_merge($this->sqlDebug ? ['sqlDebug' => 1] : [], [
146+
'page' => $this->page,
147+
'limit' => $this->limit,
148+
'sort' => $this->sort,
149+
'withRelations' => \array_values(\array_unique($this->withRelations)),
150+
'withRelationsCount' => \array_values(\array_unique($this->countRelations)),
151+
'withRelationsExistence' => \array_values(\array_unique($this->existRelations)),
152+
], $this->resourceFilters->getFilters());
153+
}
154+
}

0 commit comments

Comments
 (0)