|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace API\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use API\Helpers\DataManager as DataManager; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
10
|
|
|
|
|
11
|
|
|
class Query |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct($container) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->container = $container; |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function index(Request $request, Response $response, $page = 1) |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
// $query = $request->getParsedBodyParam('query', 'select * from products'); |
|
|
|
|
|
|
22
|
|
|
$query = $request->getParsedBodyParam('query', 'use senda'); |
|
|
|
|
|
|
23
|
|
|
$DataManager = new DataManager($query, $this->container, $page); |
|
24
|
|
|
|
|
25
|
|
|
if ($DataManager->isError()) { |
|
26
|
|
|
return $response->withJson($DataManager->getErrorData(), 200); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$data = []; |
|
30
|
|
|
|
|
31
|
|
|
if ($DataManager->isFetching()) { |
|
32
|
|
|
$data['fetching'] = true; |
|
33
|
|
|
$fetchedData = $DataManager->paginator->getCurrentItems(); |
|
34
|
|
|
$data['count'] = $DataManager->paginator->getTotalItem(); |
|
35
|
|
|
$data['currentPage'] = $DataManager->paginator->getCurrentPage(); |
|
36
|
|
|
$data['totalPages'] = $DataManager->paginator->getTotalPages(); |
|
37
|
|
|
$data['rowsPerPage'] = $DataManager->paginator->getRowsPerPage(); |
|
38
|
|
|
$data['message'] = $data['count'].' rows has been returned.'; |
|
39
|
|
|
$data['columns'] = isset($fetchedData[0]) ? array_keys($fetchedData[0]) : []; |
|
40
|
|
|
$data['rows'] = $fetchedData; |
|
41
|
|
|
} else { |
|
42
|
|
|
if ($DataManager->getCustomMessage() !== false) { |
|
43
|
|
|
$data['message'] = $DataManager->getCustomMessage(); |
|
44
|
|
|
} else { |
|
45
|
|
|
$data['message'] = 'the query has been successfully performed !'; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// sleep(5); |
|
|
|
|
|
|
50
|
|
|
// |
|
51
|
|
|
// $data = []; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
return $response->withJson($data, 200); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function page(Request $request, Response $response) |
|
57
|
|
|
{ |
|
58
|
|
|
$query = $request->getParsedBodyParam('query', 'select * from products'); |
|
|
|
|
|
|
59
|
|
|
$page = $request->getParsedBodyParam('page', 1); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$db = $this->container->db; |
|
|
|
|
|
|
62
|
|
|
$DataManager = new DataManager($query, $this->container, $page); |
|
63
|
|
|
$fetchedData = $DataManager->paginator->getCurrentItems(); |
|
64
|
|
|
$data['currentPage'] = $DataManager->paginator->getCurrentPage(); |
|
|
|
|
|
|
65
|
|
|
$data['rows'] = $fetchedData; |
|
66
|
|
|
|
|
67
|
|
|
// sleep(5); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
return $response->withJson($data, 200); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: