Skip to content

Commit 7ad1e66

Browse files
authored
Merge pull request #4 from iazaran/custom_method
Custom method as callback
2 parents 544512c + e227ce9 commit 7ad1e66

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ This package doesn't serve API features, but you can set an endpoint to accept t
1818
- Now update samples.php and run `php samples.php`.
1919
- Some samples:
2020
```php
21-
// Creating instances
21+
// Creating instances and specify the result type like JSON or RAW array of data
2222
$generatrixDB = new GeneratrixDB('locations', 'DBUser', 'DBPassword', 'localhost');
23-
$generatrixAPI = new GeneratrixCRUD($generatrixDB, 'JSON');
23+
$generatrixCRUD = new GeneratrixCRUD($generatrixDB, 'JSON');
2424

2525
// Getting information about a table and some columns
26-
$generatrixAPI::information(['tt_countries' => ['countryCode', 'countryName']]);
26+
$generatrixCRUD::information(['tt_countries' => ['countryCode', 'countryName']]);
2727
// Reading a specific row from a table and related table(s) and columns based on different relationship directions
28-
$generatrixAPI::read(30, ['tt_cities' => ['cityName']], ['tt_hotels' => ['name']], 'LEFT');
29-
$generatrixAPI::read(30, ['tt_hotels' => ['name']], ['tt_cities' => ['cityName']], 'RIGHT');
28+
$generatrixCRUD::read(30, ['tt_cities' => ['cityName']], ['tt_hotels' => ['name']], 'LEFT');
29+
$generatrixCRUD::read(30, ['tt_hotels' => ['name']], ['tt_cities' => ['cityName']], 'RIGHT');
30+
// Using custom method as callback. You can see the sample of CustomMethods class in samples.php
31+
$generatrixCRUD::read(30, ['tt_cities' => ['cityName']], ['tt_hotels' => ['name']], 'LEFT', ['CustomMethods', 'groupByFirstColumn']);
3032
// Create multiple rows
31-
$generatrixAPI::create(['tt_countries' => ['countryCode' => ['US', 'GB'], 'countryName' => ['United State', 'Great Britain']]]);
33+
$generatrixCRUD::create(['tt_countries' => ['countryCode' => ['US', 'GB'], 'countryName' => ['United State', 'Great Britain']]]);
3234
// Update specific row
33-
$generatrixAPI::update(237, ['tt_countries' => ['countryCode' => 'ES', 'countryName' => 'Spain']]);
35+
$generatrixCRUD::update(237, ['tt_countries' => ['countryCode' => 'ES', 'countryName' => 'Spain']]);
3436
// Delete specific row
35-
$generatrixAPI::delete(237, ['tt_countries']);
37+
$generatrixCRUD::delete(237, ['tt_countries']);
3638
```
3739

3840
------------

samples.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,52 @@
33
require_once 'src/GeneratrixDB.php';
44
require_once 'src/GeneratrixCRUD.php';
55

6-
$generatrixDB = new \iazaran\crudgeneratrix\GeneratrixDB('locations', 'DBUser', 'DBPassword', 'localhost');
7-
$generatrixAPI = new \iazaran\crudgeneratrix\GeneratrixCRUD($generatrixDB, 'JSON');
6+
class CustomMethods
7+
{
8+
/**
9+
* Sample class and method to use as callback
10+
*
11+
* @param ...$responses
12+
* @return array
13+
*/
14+
public static function groupByFirstColumn(...$responses): array
15+
{
16+
$tempResponse = [];
17+
foreach ($responses as $response) {
18+
$firstColumn = array_key_first($response);
19+
array_shift($response);
20+
$tempResponse[$firstColumn][] = $response;
21+
}
22+
23+
return $tempResponse;
24+
}
25+
}
26+
27+
// Creating instances and specify the result type like JSON or RAW array of data
28+
$generatrixDB = new \iazaran\crudgeneratrix\GeneratrixDB('locations', 'DBUser', 'DBPassword995!', 'localhost');
29+
$generatrixCRUD = new \iazaran\crudgeneratrix\GeneratrixCRUD($generatrixDB, 'JSON');
830

931
try {
10-
var_dump($generatrixAPI::information(['tt_countries' => ['countryCode', 'countryName']]));
11-
var_dump($generatrixAPI::read(30, ['tt_cities' => ['cityName']], ['tt_hotels' => ['name']], 'LEFT'));
12-
var_dump($generatrixAPI::read(30, ['tt_hotels' => ['name']], ['tt_cities' => ['cityName']], 'RIGHT'));
13-
var_dump($generatrixAPI::create(['tt_countries' => ['countryCode' => ['US', 'GB'], 'countryName' => ['United State', 'Great Britain']]]));
14-
var_dump($generatrixAPI::update(237, ['tt_countries' => ['countryCode' => 'ES', 'countryName' => 'Spain']]));
15-
var_dump($generatrixAPI::delete(237, ['tt_countries']));
32+
// Getting information about a table and some columns
33+
var_dump($generatrixCRUD::information(['tt_countries' => ['countryCode', 'countryName']]));
34+
35+
// Reading a specific row from a table and related table(s) and columns based on different relationship directions
36+
var_dump($generatrixCRUD::read(30, ['tt_cities' => ['cityName']], ['tt_hotels' => ['name']], 'LEFT'));
37+
var_dump($generatrixCRUD::read(30, ['tt_hotels' => ['name']], ['tt_cities' => ['cityName']], 'RIGHT'));
38+
39+
// Using custom method as callback
40+
// Without callback like: [{"cityName":"Al Ain","name":"Radisson Blu Hotel & Resort, Al Ain"},{"cityName":"Al Ain","name":"Danat Al Ain Resort"},{"cityName":"Al Ain","name":"Mercure Grand Jebel Hafeet Al Ain Hotel"}]
41+
// With callback like: {"cityName":[{"name":"Radisson Blu Hotel & Resort, Al Ain"},{"name":"Danat Al Ain Resort"},{"name":"Mercure Grand Jebel Hafeet Al Ain Hotel"}]}
42+
var_dump($generatrixCRUD::read(30, ['tt_cities' => ['cityName']], ['tt_hotels' => ['name']], 'LEFT', ['CustomMethods', 'groupByFirstColumn']));
43+
44+
// Create multiple rows
45+
var_dump($generatrixCRUD::create(['tt_countries' => ['countryCode' => ['US', 'GB'], 'countryName' => ['United State', 'Great Britain']]]));
46+
47+
// Update specific row
48+
var_dump($generatrixCRUD::update(237, ['tt_countries' => ['countryCode' => 'ES', 'countryName' => 'Spain']]));
49+
50+
// Delete specific row
51+
var_dump($generatrixCRUD::delete(237, ['tt_countries']));
1652
} catch (Exception $e) {
1753
var_dump($e->getMessage());
1854
}

src/GeneratrixCRUD.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ public static function information(array $table): array|string|bool
6565
* Handle CREATE request
6666
*
6767
* @param array $table
68+
* @param array $callback
6869
* @return bool|mysqli_result|string
6970
*/
70-
public static function create(array $table): mysqli_result|bool|string
71+
public static function create(array $table, array $callback = []): mysqli_result|bool|string
7172
{
7273
$tableName = array_key_first($table);
7374
$sql = "INSERT INTO $tableName";
@@ -95,6 +96,10 @@ public static function create(array $table): mysqli_result|bool|string
9596

9697
$result = self::$generatrixDB->dbConnection->query($sql);
9798

99+
if (count($callback) == 2 && is_callable($callback)) {
100+
$result = call_user_func_array($callback, (array)$result);
101+
}
102+
98103
return match (self::$responseType) {
99104
'JSON' => json_encode($result),
100105
default => $result,
@@ -108,9 +113,10 @@ public static function create(array $table): mysqli_result|bool|string
108113
* @param array $table
109114
* @param array $relationships
110115
* @param string $relationshipDirection
116+
* @param array $callback
111117
* @return array|false|string
112118
*/
113-
public static function read(int $id, array $table, array $relationships = [], string $relationshipDirection = 'LEFT'): bool|array|string
119+
public static function read(int $id, array $table, array $relationships = [], string $relationshipDirection = 'LEFT', array $callback = []): bool|array|string
114120
{
115121
$tableName = array_key_first($table);
116122

@@ -167,6 +173,10 @@ public static function read(int $id, array $table, array $relationships = [], st
167173
}
168174
}
169175

176+
if (count($callback) == 2 && is_callable($callback)) {
177+
$response = call_user_func_array($callback, $response);
178+
}
179+
170180
return match (self::$responseType) {
171181
'JSON' => json_encode($response),
172182
default => $response,
@@ -178,9 +188,10 @@ public static function read(int $id, array $table, array $relationships = [], st
178188
*
179189
* @param int $id
180190
* @param array $table
191+
* @param array $callback
181192
* @return bool|mysqli_result|string
182193
*/
183-
public static function update(int $id, array $table): mysqli_result|bool|string
194+
public static function update(int $id, array $table, array $callback = []): mysqli_result|bool|string
184195
{
185196
$tableName = array_key_first($table);
186197
$sql = "UPDATE $tableName";
@@ -194,6 +205,10 @@ public static function update(int $id, array $table): mysqli_result|bool|string
194205

195206
$result = self::$generatrixDB->dbConnection->query($sql);
196207

208+
if (count($callback) == 2 && is_callable($callback)) {
209+
$result = call_user_func_array($callback, (array)$result);
210+
}
211+
197212
return match (self::$responseType) {
198213
'JSON' => json_encode($result),
199214
default => $result,
@@ -205,14 +220,19 @@ public static function update(int $id, array $table): mysqli_result|bool|string
205220
*
206221
* @param int $id
207222
* @param array $table
223+
* @param array $callback
208224
* @return bool|mysqli_result|string
209225
*/
210-
public static function delete(int $id, array $table): mysqli_result|bool|string
226+
public static function delete(int $id, array $table, array $callback = []): mysqli_result|bool|string
211227
{
212228
$sql = "DELETE FROM $table[0] WHERE id = $id";
213229

214230
$result = self::$generatrixDB->dbConnection->query($sql);
215231

232+
if (count($callback) == 2 && is_callable($callback)) {
233+
$result = call_user_func_array($callback, (array)$result);
234+
}
235+
216236
return match (self::$responseType) {
217237
'JSON' => json_encode($result),
218238
default => $result,

0 commit comments

Comments
 (0)