Skip to content

Commit ef4322b

Browse files
committed
Merge branch 'master' of github.com:mevdschee/php-crud-api
2 parents 78c030b + 62f8d2f commit ef4322b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This is a single file application! Upload "api.php" somewhere and enjoy!
4040
## Limitations
4141

4242
- Primary keys should either be auto-increment (from 1 to 2^53) or UUID
43+
- Column names must be strictly alphanumeric, hyphens/underscores are allowed
4344
- Composite primary or foreign keys are not supported
4445
- Complex filters (with both "and" & "or") are not supported
4546
- Complex writes (transactions) are not supported
@@ -180,7 +181,7 @@ Search is implemented with the "filter" parameter. You need to specify the colum
180181
- ge: greater or equal (number is higher than or equal to value)
181182
- gt: greater than (number is higher than value)
182183
- bt: between (number is between two comma separated values)
183-
- in: in (number is in comma separated list of values)
184+
- in: in (number or string is in comma separated list of values)
184185
- is: is null (field contains "NULL" value)
185186

186187
You can negate all filters by prepending a 'n' character, so that 'eq' becomes 'neq'.
@@ -604,7 +605,7 @@ You can call the ```php_crud_api_transform()``` function to structure the data h
604605
}
605606
```
606607

607-
This transform function is available for PHP and JavaScript in the files ```php_crud_api_transform.php``` and ```php_crud_api_transform.js``` in the "lib" folder.
608+
This transform function is available for PHP, JavaScript and Python in the files ```php_crud_api_transform.php```, ```php_crud_api_transform.js``` and ```php_crud_api_transform.py``` in the "lib" folder.
608609

609610
## Permissions
610611

lib/php_crud_api_transform.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def php_crud_api_transform(tables):
2+
3+
def get_objects(tables, table_name, where_index=None, match_value=None):
4+
objects = []
5+
for record in tables[table_name]['records']:
6+
if where_index == None or (record[where_index] == match_value):
7+
object = {}
8+
columns = tables[table_name]['columns']
9+
for column in columns:
10+
index = columns.index(column)
11+
object[column] = record[index]
12+
for relation, reltable in tables.items():
13+
for key, target in reltable.get('relations', {}).items():
14+
if target == table_name + '.' + column:
15+
relcols = reltable['columns']
16+
column_indices = {value: relcols.index(value) for value in relcols}
17+
object[relation] = get_objects(
18+
tables, relation, column_indices[key], record[index])
19+
objects.append(object)
20+
return objects
21+
22+
tree = {}
23+
for name, table in tables.items():
24+
if not 'relations' in table:
25+
tree[name] = get_objects(tables, name)
26+
if 'results' in table:
27+
tree['_results'] = table['results']
28+
return tree
29+
30+
31+
if __name__ == "__main__":
32+
input = {"posts": {"columns": ["id","user_id","category_id","content"],"records": [[1,1,1,"blogstarted"]]},"post_tags": {"relations": {"post_id": "posts.id"},"columns": ["id","post_id","tag_id"],"records": [[1,1,1],[2,1,2]]},"categories": {"relations": {"id": "posts.category_id"},"columns": ["id","name"],"records": [[1,"anouncement"]]},"tags": {"relations": {"id": "post_tags.tag_id"},"columns": ["id","name"],"records": [[1,"funny"],[2,"important"]]},"comments": {"relations": {"post_id": "posts.id"},"columns": ["id","post_id","message"],"records": [[1,1,"great"],[2,1,"fantastic"]]}}
33+
output = {"posts": [{"id": 1,"post_tags": [{"id": 1,"post_id": 1,"tag_id": 1,"tags": [{"id": 1,"name": "funny"}]},{"id": 2,"post_id": 1,"tag_id": 2,"tags": [{"id": 2,"name": "important"}]}],"comments": [{"id": 1,"post_id": 1,"message": "great"},{"id": 2,"post_id": 1,"message": "fantastic"}],"user_id": 1,"category_id": 1,"categories": [{"id": 1,"name": "anouncement"}],"content": "blogstarted"}]}
34+
35+
print(php_crud_api_transform(input) == output)

tests/Tests.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,13 @@ public function testFilterPostsNotIn()
391391
$test->expect('{"posts":[{"id":5,"user_id":1,"category_id":1,"content":"#1"},{"id":6,"user_id":1,"category_id":1,"content":"#2"}]}');
392392
}
393393

394+
public function testFilterCommentsStringIn()
395+
{
396+
$test = new Api($this);
397+
$test->get('/comments?filter=message,in,fantastic,thank you&transform=1');
398+
$test->expect('{"comments":[{"id":2,"post_id":1,"message":"fantastic"},{"id":3,"post_id":2,"message":"thank you"}]}');
399+
}
400+
394401
public function testFilterPostsBetween()
395402
{
396403
$test = new Api($this);

0 commit comments

Comments
 (0)