Преглед на файлове

Merge branch 'master' of github.com:mevdschee/php-crud-api

Maurits van der Schee преди 6 години
родител
ревизия
ef4322b6c0
променени са 3 файла, в които са добавени 45 реда и са изтрити 2 реда
  1. 3
    2
      README.md
  2. 35
    0
      lib/php_crud_api_transform.py
  3. 7
    0
      tests/Tests.php

+ 3
- 2
README.md Целия файл

@@ -40,6 +40,7 @@ This is a single file application! Upload "api.php" somewhere and enjoy!
40 40
 ## Limitations
41 41
 
42 42
   - 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
43 44
   - Composite primary or foreign keys are not supported
44 45
   - Complex filters (with both "and" & "or") are not supported
45 46
   - Complex writes (transactions) are not supported
@@ -180,7 +181,7 @@ Search is implemented with the "filter" parameter. You need to specify the colum
180 181
   - ge: greater or equal (number is higher than or equal to value)
181 182
   - gt: greater than (number is higher than value)
182 183
   - 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)
184 185
   - is: is null (field contains "NULL" value)
185 186
 
186 187
 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
604 605
 }
605 606
 ```
606 607
 
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.
608 609
 
609 610
 ## Permissions
610 611
 

+ 35
- 0
lib/php_crud_api_transform.py Целия файл

@@ -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)

+ 7
- 0
tests/Tests.php Целия файл

@@ -391,6 +391,13 @@ abstract class Tests extends TestBase
391 391
         $test->expect('{"posts":[{"id":5,"user_id":1,"category_id":1,"content":"#1"},{"id":6,"user_id":1,"category_id":1,"content":"#2"}]}');
392 392
     }
393 393
 
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
+
394 401
     public function testFilterPostsBetween()
395 402
     {
396 403
         $test = new Api($this);

Loading…
Отказ
Запис