Skip to content

Commit c73a3a2

Browse files
authored
Merge pull request #8 from iazaran/single_api_method
Single api method
2 parents 5523e0d + 78be708 commit c73a3a2

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The `create`, `read`, `update` and `delete` methods can accept the different par
1313
- **Customizable search**
1414
The `search` method can be used for listing records based on some conditions. Conditions can be customize and join each others by different type of conditional operators. Relationships can be used like `read` method. Limitation and offset can be applied to limit records count and start index, that will be useful for pagination too.
1515
- **Can be used by a single endpoint in API**
16-
This package doesn't serve API features, but you can set an endpoint to accept those parameters and do CRUD features like GraphQL. (WIP: the single method to accept all parameters will be added.)
16+
This package doesn't serve API features, but you can set an endpoint to accept those parameters and do CRUD features like GraphQL. There is a method `api` that accepts all parameters and will implement any type of features of this package.
1717

1818
#### Run Web App:
1919
- There is a sample in here. You can create a DB and some tables and records. Make sure you set foreign keys too.
@@ -39,6 +39,8 @@ $generatrixCRUD::update(237, ['tt_countries' => ['countryCode' => 'ES', 'country
3939
$generatrixCRUD::delete(237, ['tt_countries']);
4040
// Search for multiple columns (AND, OR, XOR, ...) of target table (=, LIKE, NOT, ...) and list them ('AND' will be considered for joining conditions of conditions) You can add relationships like read method
4141
$generatrixCRUD::search(['OR' => ['=' => ['cityName' => 'dubai'], 'LIKE' => ['cityName' => 'old']]], ['tt_cities' => ['cityName']], [], '', 10, 5);
42+
// To use as a single method for all type of features. You can use any method name in here as `type`
43+
$generatrixCRUD::api('search', array $table, int $id = null, array $relationships = [], string $relationshipDirection = 'LEFT', array $search = null, int $offset = 0, int $limit = 1000, array $callback = []);
4244
```
4345

4446
------------

samples.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public static function groupByFirstColumn(...$responses): array
5252

5353
// Search for multiple columns (AND, OR, XOR, ...) of target table (=, LIKE, NOT, ...) and list them ('AND' will be considered for joining conditions of conditions) You can add relationships like read method
5454
var_dump($generatrixCRUD::search(['OR' => ['=' => ['cityName' => 'dubai'], 'LIKE' => ['cityName' => 'old']]], ['tt_cities' => ['cityName']], [], '', 10, 5));
55+
56+
// To use as a single method for all type of features. You can use any method name in here as `type`
57+
var_dump($generatrixCRUD::api('search', ['tt_cities' => ['cityName']], null, [], '', ['OR' => ['=' => ['cityName' => 'dubai'], 'LIKE' => ['cityName' => 'old']]], 10, 5, []));
5558
} catch (Exception $e) {
5659
var_dump($e->getMessage());
5760
}

src/GeneratrixCRUD.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,30 @@ public static function search(array $search, array $table, array $relationships
331331
default => $response,
332332
};
333333
}
334+
335+
/**
336+
* To use as a single method for all type of features
337+
*
338+
* @param string $type
339+
* @param array $table
340+
* @param int|null $id
341+
* @param array $relationships
342+
* @param string $relationshipDirection
343+
* @param array $search
344+
* @param int $offset
345+
* @param int $limit
346+
* @param array $callback
347+
* @return array|bool|mysqli_result|string
348+
*/
349+
public static function api(string $type, array $table, int $id = null, array $relationships = [], string $relationshipDirection = 'LEFT', array $search = [], int $offset = 0, int $limit = 1000, array $callback = []): mysqli_result|bool|array|string
350+
{
351+
return match ($type) {
352+
'information' => self::information($table),
353+
'create' => self::create($table, $callback),
354+
'read' => self::read($id, $table, $relationships, $relationshipDirection, $callback),
355+
'update' => self::update($id, $table, $callback),
356+
'delete' => self::delete($id, $table, $callback),
357+
'search' => self::search($search, $table, $relationships, $relationshipDirection, $offset, $limit, $callback),
358+
};
359+
}
334360
}

0 commit comments

Comments
 (0)